Skip to content

Instantly share code, notes, and snippets.

@timd
Created July 1, 2013 10:33
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save timd/5899845 to your computer and use it in GitHub Desktop.
Save timd/5899845 to your computer and use it in GitHub Desktop.
A basic Salesforce OAuth login process using the Salesforce iOS SDK, implemented as a single standalone view controller
//
// SFAuthDemoViewController.m
//
// Created by Tim Duckett on 19/06/2013.
// Copyright (c) 2013 Charismatic Megafauna Ltd. All rights reserved.
//
#import "SFAuthDemoViewController.h"
#import "SFAccountManager.h"
#import "SFRestAPI.h"
#import "SFRestAPI+Blocks.h"
static NSString *const kOAuthConsumerKey = @"<your OAuth consumer key>";
//static NSString *const OAuthRedirectURI = @"https://login.salesforce.com/services/oauth2/success";
static NSString *const kOAuthRedirectURI = @"https://salesforce.com/services/oauth2/success";
static NSString *const kOAuthLoginDomain = @"salesforce.com";
static NSString *const kUserAccountIdentifier = @"user";
@interface SFAuthDemoViewController () <SFOAuthCoordinatorDelegate>
@property (nonatomic, strong) SFOAuthCoordinator *coordinator;
@end
@implementation SFAuthDemoViewController
#pragma mark -
#pragma mark View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self setupCoordinator];
[self setupAccountManager];
}
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
}
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark -
#pragma mark SFAccountManager configuration
-(void)setupAccountManager {
[SFAccountManager setLoginHost:kOAuthLoginDomain];
[SFAccountManager setClientId:kOAuthConsumerKey];
[SFAccountManager setRedirectUri:kOAuthRedirectURI];
[SFAccountManager setCurrentAccountIdentifier:kUserAccountIdentifier];
}
- (void)setupCoordinator {
// create a new coordinator if one doesn't already exist
if (!self.coordinator) {
NSString *appName = [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString*)kCFBundleNameKey];
NSString *loginDomain = kOAuthLoginDomain;
NSString *accountIdentifier = kUserAccountIdentifier;
NSString *fullKeychainIdentifier = [NSString stringWithFormat:@"%@-%@-%@",appName,accountIdentifier,loginDomain];
SFOAuthCredentials *creds = [[SFOAuthCredentials alloc] initWithIdentifier:fullKeychainIdentifier
clientId:kOAuthConsumerKey
encrypted:NO];
[creds setDomain:loginDomain];
[creds setRedirectUri:kOAuthRedirectURI];
self.coordinator = [[SFOAuthCoordinator alloc] initWithCredentials:creds];
[self.coordinator setScopes:[NSSet setWithObjects:@"web",@"api",nil]];
[self.coordinator setDelegate:self];
}
}
#pragma mark -
#pragma mark SFOAuthDelegate methods
-(void) oauthCoordinator:(SFOAuthCoordinator *)coordinator didBeginAuthenticationWithView:(UIWebView *)view {
[[self view] addSubview:view];
[view setFrame:CGRectMake(0,0,self.view.frame.size.width, self.view.frame.size.height)];
}
- (void)oauthCoordinatorDidAuthenticate:(SFOAuthCoordinator *)coordinator {
[coordinator.view removeFromSuperview];
[[SFRestAPI sharedInstance] setCoordinator:coordinator];
[SFAccountManager sharedInstance].credentials = coordinator.credentials;
}
-(void)oauthCoordinator:(SFOAuthCoordinator *)coordinator didFailWithError:(NSError *)error authInfo:(SFOAuthInfo *)info {
[coordinator.view removeFromSuperview];
NSLog(@"error = %@", error);
NSLog(@"authInfo = %@", info);
}
#pragma mark -
#pragma mark Interaction methods
- (IBAction)didTapCallApiButton:(id)sender {
[[SFRestAPI sharedInstance] performSOQLQuery:@"SELECT id FROM Account" failBlock:^(NSError *error) {
NSLog(@"Error = %@", error);
} completeBlock:^(NSDictionary *dict) {
NSLog(@"%@", dict);
}];
}
- (IBAction)didTapLogoutButton:(id)sender {
[self.coordinator revokeAuthentication];
}
-(IBAction)didTapLoginButton:(id)sender {
[self.coordinator authenticate];
}
-(IBAction)didTapNextButton:(id)sender {
AZFSecondViewController *secondViewController = [[AZFSecondViewController alloc] initWithNibName:@"AZFSecondViewController" bundle:nil];
[self.navigationController pushViewController:secondViewController animated:YES];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment