SFUserAccount is always nil

163 Views Asked by At

I have updated my salesforcesdk-ios to new version 5.3.0. After logging in successfully, I set all these values in this delegate -oauthCoordinatorDidAuthenticate:authInfo: like this,

- (void) oauthCoordinatorDidAuthenticate: (SFOAuthCoordinator *) coordinator authInfo:(SFOAuthInfo *)info {

    [SFAuthenticationManager sharedManager].coordinator = coordinator;
    [[SFAuthenticationManager sharedManager] coordinator].credentials = coordinator.credentials;
    [[SFUserAccountManager sharedInstance] applyCredentials:coordinator.credentials];
}

When I try validating for the refreshtoken currentUser is nil in - (void)send:(SFRestRequest *)request delegate:(id)delegate shouldRetry:(BOOL)shouldRetry and always loads salesforce login page instead of sending request to Salesforce.

// If there are no demonstrable auth credentials, login before sending. SFUserAccount *user = [SFUserAccountManager sharedInstance].currentUser;

user is always nil. How to set the useraccount correctly? Thanks!

2

There are 2 best solutions below

0
On

First, I think you could check the delegate oauthCoordinatorDidAuthenticate:authInfo: is worked. like:

- (void) oauthCoordinatorDidAuthenticate: (SFOAuthCoordinator *) coordinator authInfo:(SFOAuthInfo *)info {
print("delegate is worked")
[SFAuthenticationManager sharedManager].coordinator = coordinator;
[[SFAuthenticationManager sharedManager] coordinator].credentials = coordinator.credentials;
[[SFUserAccountManager sharedInstance] applyCredentials:coordinator.credentials];

}

It could cause user is nil, when the delegate is not worked.

0
On

I found the answer. They are expecting to set currentUser explicitly in the newer version of iOS sdk. I have a custom login screen with toggle to switch between sandbox and production. I am setting user account now in this method like this -

   [[SFAuthenticationManager sharedManager] loginWithCompletion:^(SFOAuthInfo *authInfo,SFUserAccount *userAccount) {
        [SFSDKCoreLogger i:[self class] format:@"Authentication (%@) succeeded.  Launch completed.", authInfo.authTypeDescription];
        [SFUserAccountManager sharedInstance].currentUser = userAccount;
        [SFSecurityLockout setupTimer];
        [SFSecurityLockout startActivityMonitoring];
        s_loggedIn = YES;
    } failure:^(SFOAuthInfo *authInfo, NSError *authError) {
        [SFSDKCoreLogger e:[self class] format:@"Authentication (%@) failed: %@.", (authInfo.authType == SFOAuthTypeUserAgent ? @"User Agent" : @"Refresh"), [authError localizedDescription]];
    }];