Objective-C code samples for common Force.com and iOS use cases

SimpleApp Tomorrow is the developing custom mobile Force.com apps for the iPhone and iPad webinar. (Don't forget to register!) One of things I had mentioned previously is that I wrote another sample app designed to offer a very simple introduction to iOS development and the Force.com platform.

This new sample app, designed for the iPhone, demonstrates some of the typical force.com functions you will likely encounter when developing your own apps. Here are a few of the primary requirements, and some code to help you get started.

1. Showing the OAuth Login screen

Add a call similar to [self showLogin] within your didFinishLoadingWithOptions method of your delegate to have the login show immediately upon app startup:

- (void)showLogin
oAuthViewController.modalPresentationStyle = UIModalPresentationFormSheet;
[beginningViewController presentModalViewController:oAuthViewController animated:YES];

2. Handle oAuth callback

Once the user has authenticated, the platform is going to perform a callback which you will need to handle:

- (void)loginOAuth:(ZKOAuthViewController *)oAuthViewController error:(NSError *)error
if ([oAuthViewController accessToken] && !error)
[[ZKServerSwitchboard switchboard] setApiUrlFromOAuthInstanceUrl:[oAuthViewController instanceUrl]];
[[ZKServerSwitchboard switchboard] setSessionId:[oAuthViewController accessToken]];
[[ZKServerSwitchboard switchboard] setOAuthRefreshToken:[oAuthViewController refreshToken]];
BeginnerForcedotcomAppDelegate *app = [[UIApplication sharedApplication] delegate];
[self getAccounts];  //let's query for data
[app hideLogin];  //let's hide the login screen

3. Query data in the Force.com database

Here is a simple query to retrieve records and configure a selector, queryResults, to handle the response:

- (void)getAccounts
NSString *queryString =
@"Select Id, Name, BillingState, Phone From Account order by Name limit 100";
[[ZKServerSwitchboard switchboard] query:queryString target:self
selector:@selector(queryResult:error:context:) context:nil];

4. Handle the data response.

A typical use case is to present the results in a tableView:

- (void)queryResult:(ZKQueryResult *)result error:(NSError *)error context:(id)context
if (result && !error)
self.dataRows = [NSMutableArray arrayWithArray:[result records]];
[self.view addSubview:tableView];
[tableView reloadData];
else if (error)
[self receivedErrorFromAPICall: error];

Of course, these code snippets may be hard to follow everything going on if you are not used to Objective-C syntax, but they do demonstrate a pretty common use flow you are likely to encounter as you start building custom iOS apps.

During tomorrow's webinar I'll share some more hints and tips if you are looking at building more mobile apps. Don't forget, Mike Leach from Facebook will also be joining me to offer some insight on how Facebook is approaching mobile development. And with something like 250 million mobile users, Facebook certainly has a pretty big stake in the future of mobile development.

tagged , , , , , , , , , , , , Bookmark the permalink. Trackbacks are closed, but you can post a comment.
  • jm

    HI Quinton, thanks for the entry.
    I fetched the data for this query:
    Select Id, Name, Username, Email, Phone, Division, FullPhotoUrl from User
    Do you know how you would get the photo from this? I attempted the following, but it gave me a null object…(the url is fine though)
    UIImage * img = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[(ZKSObject*)[records objectAtIndex:indexPath.row] fieldValue:@”FullPhotoUrl”]]]];