3

I'm currently trying to hack in some functionality to download attachment data for an iOS Salesforce application. I'm going to do that using a direct HTTP request, but for that to work I need to pull the session ID and server (e.g. "na1.salesforce.com") from the API. Has anyone been able to do this yet?

2

Many people did this.

This would require you to basic understanding of Salesforce REST API and OAuth flow. Here is the link to get some details about Understanding Authentication. In this link Salesforce OAuth flow is provided.

Alternatively Salesforce has its own SDK SalesforceMobileSDK-iOS. This will do all authentication process for you. In link read its introduction and some useful documentation has been provided explaining the methods to use the SDK.

When you successfully complete the authentication process, in return salesforce provide session id (access token), its instance URL and other useful information by which you can do further operations.

0

Thanks to regal for helping out!

The way to do this is as follows:

First, login using a SFOAuthCoordinator. The keychain identifier just needs to be a unique identifier for your app.

// Set up the authorization credentials
NSString *appName = [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString*)kCFBundleNameKey];
NSString *fullKeychainIdentifier = [NSString stringWithFormat:@"%@-%@",appName,kUserAccountIdentifier];

SFOAuthCredentials *credentials = [[SFOAuthCredentials alloc] initWithIdentifier:fullKeychainIdentifier
                                                                        clientId:RemoteAccessConsumerKey
                                                                       encrypted:NO];
[credentials setRedirectUri:OAuthRedirectURI];

// Set up the authorization coordinator
sfoCoordinator = [[SFOAuthCoordinator alloc] initWithCredentials:credentials];
[sfoCoordinator setScopes:[NSSet setWithObjects:@"api",nil]];
[sfoCoordinator setDelegate:self];

// Set up the account manager

[SFAccountManager setClientId:RemoteAccessConsumerKey];
[SFAccountManager setRedirectUri:OAuthRedirectURI];
[SFAccountManager setCurrentAccountIdentifier:kUserAccountIdentifier];

[sfoCoordinator authenticate];

Then, implement the following three protocol methods to handle the login:

-(void) oauthCoordinator:(SFOAuthCoordinator *)coordinator didBeginAuthenticationWithView:(UIWebView *)view {
}

-(void)oauthCoordinator:(SFOAuthCoordinator *)coordinator didFailWithError:(NSError *)error authInfo:(SFOAuthInfo *)info {
}

- (void)oauthCoordinatorDidAuthenticate:(SFOAuthCoordinator *)coordinator {
    // Set the access tokens
    sfAccessToken = coordinator.credentials.accessToken; // This is the token you pass with your HTTP request 
    baseURL = coordinator.credentials.instanceUrl; // This is the domain you'll send your call to

}

Assuming you've already retrieved the path of the attachment from your SOQL call, you can now construct a request using the data that you stored in the DidAuthenticate method above. I used AFNetworking just cause it's awesome.

// Set up shared http client
[[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES];
sharedClient = [[AFHTTPClient alloc] initWithBaseURL:baseURL]; // baseURL populated from credentials.instanceURL

// Construct the request (note that the method is GET)
NSMutableURLRequest *request = [sharedClient requestWithMethod:@"GET" path:path parameters:nil];

// Add the authorization header. Prepend "Bearer " to the access token you got in the 
// DidAuthenticate method.
NSString *authHeader = [NSString stringWithFormat:@"Bearer %@",sfAccessToken];
[request addValue:authHeader forHTTPHeaderField:@"Authorization"];

// Build the operation
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

// Set the completion and failure blocks
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    // Do something with the responseObject
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    // Process your error here
}];

// Begin the operation
[operation start];

And voila! Attachment data received.

0

Swift

Those working with the 7.0+ SDK, token and endpoint details can be accessed like so:

AuthHelper.loginIfRequired {
  //oAuth Token
  UserAccountManager.shared.currentUserAccount?.credentials.credential
  //API Base URL
  UserAccountManager.shared.currentUserAccount?.credentials.instanceUrl
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.