Objective-C


iPhone and Objective-C24 Apr 2009 03:11 am

I’m developing an iPhone app which has a built-in quiz which runs using JavaScript within a UIWebView. After a user clicks a Check Answers button, I have to use JavaScript to determine if all the answers are correct, so I can save this result for later.

Here is the relevant JavaScript:

function checkAnswers() {
  // Do stuff to see if all answers were correct
  ...

  // Send all correct status back to Objective-C
  window.location = "/allCorrect/" + allCorrect;
}

In Objective-C, you then need to set up a UIWebViewDelegate to intercept whenever a new URL is to be loaded into the UIWebView. Then you need to call shouldStartLoadWithRequest and if you called your fake URL, then it shouldn’t load, but execute the code you need to run in Objective-C instead. Here is the relevant Objective-C code:

- (BOOL)webView:(UIWebView *)webView
        shouldStartLoadWithRequest:(NSURLRequest *)request
        navigationType:(UIWebViewNavigationType)navigationType {

  if ( [request.mainDocumentURL.relativePath
        isEqualToString:@"/allCorrect/false"] ) {
    NSLog( @"Nope, that is not right!" );
    return false;
  }

  if ( [request.mainDocumentURL.relativePath
        isEqualToString:@"/allCorrect/true"] ) {
    NSLog( @"You got them all!" );
    return false;
  }

  return true;
}
iPhone and Objective-C08 Mar 2009 04:55 pm

Many times you need to save some user preferences or session data that will load automatically upon the next launch of your iPhone app. Unfortunately this is quite a bit harder than it looks. Here are simple steps on how to save a string from your application. Just change the variable type to save more information!

Let’s assume you want to save the user’s account number.

Add the following to AppDelegate.h like you would normally create a mutable string:

NSMutableString *accountName;
@property (nonatomic, retain) NSMutableString *accountName;

Add the following to the top of AppDelegate.m. I put this between my import and implementation statements.

NSString *kAccountName = @"AccountName";

Add the following to applicationDidFinishLoading in AppDelegate.m:

NSMutableString *tempMutableAccountName = [[[NSUserDefaults standardUserDefaults] objectForKey:kAccountName] mutableCopy];
self.accountName = tempMutableAccountName;
[tempMutableAccountName release];

Add the following to applicationWillTerminate in AppDelegate.m:

[[NSUserDefaults standardUserDefaults] setObject:accountName forKey:kAccountName];

In the application itself, you will need to add the following code where you want to set this value to prepare for saving on application termination:

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.accountNumber = x;

To grab this value from the saved variable, add these lines:

MobilePOSAppDelegate *appDelegate = (MobilePOSAppDelegate *)[[UIApplication sharedApplication] delegate];
x = appDelegate.accountNumber;

Now you can save data from your app so it can be loaded automatically next time!