Skip to content
Create a gist now

Instantly share code, notes, and snippets.

Embed URL


Subversion checkout URL

You can clone with
Download ZIP
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 = @"";
static NSString *const kOAuthRedirectURI = @"";
static NSString *const kOAuthLoginDomain = @"";
static NSString *const kUserAccountIdentifier = @"user";
@interface SFAuthDemoViewController () <SFOAuthCoordinatorDelegate>
@property (nonatomic, strong) SFOAuthCoordinator *coordinator;
@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
[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];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.