Archive

Archive for the ‘iPhone Dev’ Category

Simple iPhone Tutorial: Password Management using the keychain by using SFHFKeychainUtils

May 23rd, 2010 14 comments

Keychain is really a pain to use, and I didn’t want to spend the time to figure it all out, so I went looking for a framework that would do the dirty work for me. I found SFHFKeychainUtils and it has been super slick! In this tutorial, I’ll show you how to leverage SFHFKeychainUtils in order to save a username/password to keychain and also retrieve the password given the username. Just follow these simple steps:

1. Get SFHFKeychainUtils

I found it on GitHub at http://github.com/ldandersen/scifihifi-iphone/tree/master/security/ – just get the SFHFKeychainUtils.h and SFHFKeychainUtils.m files and add them to your project.

2. Import it

In the implementation file where you want to save or retrieve the password, be sure you import the SFHF Keychain Utils header file as shown below:

#import “LoginViewController.h”
#import “SFHFKeychainUtils.h”
@implementation LoginViewController

3. Save a Username/Password

xCode SFHFKeychainUtils iPhone Keychain Example





Here you can see how simple it is to add the username/password to the iPhone’s keychain. The storeUsername:andPassword:forServiceName:updateExisting:error method will take care of all the work for us if we give it the correct parameters. I am getting the username and password from two UITextFields in the app called usernameField and passwordField, respectively. The Service Name can be anything you want, as long as you remember it as you’ll need to enter that same string value in order to retrieve the password (see step 4).

4. Retrieve the Password from Keychain

xCode SFHFKeychainUtils iPhone Keychain Example




As you can see, it’s not any more difficult to retrieve the password from keychain using SFHFKeychainUtils. I specified my username “Gorgando” and the same service name we used before “myApp”. The password that corresponds to this username and service name in the keychain will be returned as an NSString.

So don’t be afraid to use the keychain! It is the most secure way to store passwords in an iPhone application. Both plists and the Settings.Bundle are very insecure ways to store passwords because the passwords are stored in plaintext, visible to anyone who accesses them. Let me know if you have any questions – good luck!

About Brad

Brad is a tech professional and hobbiest. He's a husband, father, and a Mormon. Connect with Brad on Google+

Share

Simple Example-Tutorial: REGEX (Regular Expressions) in iPhone app to find and replace a value in NSString

May 8th, 2010 1 comment

Since there is no native ability in Objective C in the Cocoa Framework to use REGEX (Regular Expressions), you need to use a framework. I found that RegexKitLite was the perfect solution for my app. You can use it in 3 simple steps:

1. Add the RegexKit Framework to your app’s project in XCode

You can download the source at http://regexkit.sourceforge.net/#Downloads. Then just add the RegexKitLite.h and RegexKitLite.m files to your project. Also, you’ll need to add the libicucore.dylib Framework to your project. This can be done by right clicking on the Frameworks folder in xCode, selecting Add > Add Existing Framework and then selecting to add libicucore.dylib. Be sure to import the RegexKitLite.h file wherever you plan to use it: #import “RegexKitLite.h”.

2. Create an NSString with the Regular Expression

For example I needed a regular expression that would find all the HTML tags in an NSString and get rid of them by replacing them with an empty string “”. It looks like this:

NSString *myregex = @”<[^>]*>”; //regex to remove any html tag

3. Use the StringByReplacingOccurencesOfRegex method to replace your NSString

I think the best way to explain this is to just show it:

NSString *description = @”<html><body><p>This is the description! </p></body></html>”;
description = [description stringByReplacingOccurrencesOfRegex:myregex withString:@""];

The final description variable will just say “This is the description! ” as the tags are stripped out. All you have to do is plug in your regex NSString variable from step 2 where I have “myregex” and plug in whatever string you want to replace it with where I have @”" (an empty string to delete the occurrence rather than replace it).

Hope this is helpful- good luck!

About Brad

Brad is a tech professional and hobbiest. He's a husband, father, and a Mormon. Connect with Brad on Google+

Share
Categories: iPhone Dev Tags:

iPhone App Crashes on Device but Runs Fine in Simulator

March 27th, 2010 1 comment

This was weird today. My iPhone app was crashing when I ran it, and it crashed for some other people (but not for everyone). This was right after a new JSON was being loaded by the application. So I got into xCode and ran it on my emulator. It worked fine. I hooked up xCode to my iPod Touch and ran the application to the iPod. Sure enough it was crashing like crazy. I spent 3 hours trying to fix a phantom memory problem. I still haven’t fixed it, but I learned one thing. Restart your device, and the problem is “solved”. I put that in quotes because I’m sure it will rear its ugly head again if not identified and fixed correctly, but that was a temporary fix that tells me it must be some kind of memory issue. I’ll post later (or comment) if I discover the true root of this.

About Brad

Brad is a tech professional and hobbiest. He's a husband, father, and a Mormon. Connect with Brad on Google+

Share
Categories: iPhone Dev Tags: , ,