public
Created

A basic Salesforce OAuth login process using the Salesforce iOS SDK, implemented as a single standalone view controller

  • Download Gist
SFOAuthProcess
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
//
// 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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.