How to open salesforce login screen within the app - ios

I am using salesforce oauth connected app setup. When I launch the app it redirects me to login in the safari in app browser. How to control this? Do I need to use any salesforce library like zksforce.
I followed these steps to create a project: https://developer.salesforce.com/docs/atlas.en-us.mobile_sdk.meta/mobile_sdk/ios_new_force_project.htm
Please suggest me which is the right way to display login screen in a native app.

There is no way to open login screen native -as far as I know :)-
You should set properties first as :
[SalesforceSDKManager sharedManager].connectedAppId = RemoteAccessConsumerKey;
[SalesforceSDKManager sharedManager].connectedAppCallbackUri = OAuthRedirectURI;
[SalesforceSDKManager sharedManager].authScopes = #[ #"web", #"api" ];
you can set callback properties if necessary :
[SalesforceSDKManager sharedManager].postLaunchAction = ^(SFSDKLaunchAction launchActionList) {
};
[SalesforceSDKManager sharedManager].launchErrorAction = ^(NSError *error, SFSDKLaunchAction launchActionList) {
};
And Then you should call :
[[SalesforceSDKManager sharedManager] launch];

Related

Upload Youtube video with "fixed" token in Google APIs Client Library for Objective-C for REST

I'm trying to create an app that upload video on a specified channel, without prompt a login page. I'll try to explain better what i need.
I'm using Google APIs Client Library for Objective-C for REST, with this library i can use a "standard" upload flow :
user record a video -> he press an upload button -> Safari open the login google page -> user login in his own account and give permission to the app -> Safari redirect back to the ios app -> the upload process begin -> the video will be uploaded on the personal user channel.
Instead this is the desired workflow of my ios app:
user record a video -> he press an upload button -> the video will be uploaded in the app's youtube channel.
The only help i had find is this article ,it explain a way to obtain a refreshable app token to upload video in an app channel. It is exactly what i need. Anyway this is a web example, it uploads videos that are in a server. My videos are in the phone, so i think that i have to modify the flow of this article in this way:
obtain token the first time by login as channel owner -> create a token.txt and save it in my server -> create a page called get_token.php that print the content of token.txt and refresh it if the token expire.
With this flow in my app i need this other flow:
user record a video -> press an upload button -> i made a call to get_token.php and retrive the actual token -> i made a call by library with the token retrived to upload the video on the app's youtube channel.
Here i have found some problems, this is my authentication methods :
#pragma mark - Sign In
- (void)authNoCodeExchange {
[self verifyConfig];
NSURL *issuer = [NSURL URLWithString:kIssuer];
[self logMessage:#"Fetching configuration for issuer: %#", issuer];
// discovers endpoints
[OIDAuthorizationService discoverServiceConfigurationForIssuer:issuer
completion:^(OIDServiceConfiguration *_Nullable configuration, NSError *_Nullable error) {
if (!configuration) {
[self logMessage:#"Error retrieving discovery document: %#", [error localizedDescription]];
return;
}
[self logMessage:#"Got configuration: %#", configuration];
if (!kClientID) {
[self doClientRegistration:configuration
callback:^(OIDServiceConfiguration *configuration,
OIDRegistrationResponse *registrationResponse) {
[self doAuthWithoutCodeExchange:configuration
clientID:registrationResponse.clientID
clientSecret:registrationResponse.clientSecret];
}];
} else {
[self doAuthWithoutCodeExchangeCri:configuration clientID:kClientID clientSecret:nil];
}
}];
}
/////////////////
- (void)doAuthWithoutCodeExchangeCri:(OIDServiceConfiguration *)configuration
clientID:(NSString *)clientID
clientSecret:(NSString *)clientSecret {
NSURL *redirectURI = [NSURL URLWithString:kRedirectURI];
OIDTokenRequest *tokenExchangeRequest =
[_authState.lastAuthorizationResponse tokenExchangeRequest];
[OIDAuthorizationService performTokenRequest:tokenExchangeRequest
callback:^(OIDTokenResponse *_Nullable tokenResponse,
NSError *_Nullable error) {
if (!tokenResponse) {
[self logMessage:#"Token exchange error: %#", [error localizedDescription]];
} else {
[self logMessage:#"Received token response with accessToken: %#", tokenResponse.accessToken];
}
[_authState updateWithTokenResponse:tokenResponse error:error];
GTMAppAuthFetcherAuthorization *gtmAuthorization =
[[GTMAppAuthFetcherAuthorization alloc] initWithAuthState:authState];
// Sets the authorizer on the GTLRYouTubeService object so API calls will be authenticated.
self.youTubeService.authorizer = gtmAuthorization;
// Serializes authorization to keychain in GTMAppAuth format.
[GTMAppAuthFetcherAuthorization saveAuthorization:gtmAuthorization
toKeychainForName:kGTMAppAuthKeychainItemName];
[self uploadVideoFile];
}];
}
i have also something like:
NSString * RetrivedToken = #"ya29xxxxxxx3nJxxxxxxx6qqQ-FxxxxxxxdGH";
How i can modify those methods to accept my retrivedtoken instead the one they retrive from the standard auth workflow?
This isn't really an answer but its going to be to big for a comment.
So you want to create an app that will allow others to upload to your youtube channel. Normally i would say use a service account which would allow you to do this much easier. However the YouTube api does not support service accounts.
You are going to need to authenticate the app once save your refresh token then embed this refresh token in your app.
Why this is not a good idea with a mobile app.
Refresh tokens can expire so if it does expire or break then your going to have to authenticate the app again and embed the new refresh token into your app and release a new version to your users.
An alternative would be to set up a web service with the refresh token on that and have your app access the web service for upload. Then if the refresh token breaks you will only have to fix it on your web service.
I have done this before its messy but there is really no other way of doing it.
You can see from your code
if (!tokenResponse) {
[self logMessage:#"Token exchange error: %#", [error localizedDescription]];
} else {
[self logMessage:#"Received token response with accessToken: %#", tokenResponse.accessToken];
}
your are using OIDTokenResponse object tokenResponse in your further code.
First make an object of Class OIDTokenResponse like *oidTokenResponse and assign it your access token from your server like
oidTokenResponse.accessToken=YOUR_TOKEN_FROM_SERVER;
and then use
[_authState updateWithTokenResponse:oidTokenResponse error:error];
In other way you can also do that and you won't need to change your code much you can do it with one line of code but i'm not sure you are allow to change to access token here but you can try it.
Leave the other code and just add one more line in your else
if (!tokenResponse) {
[self logMessage:#"Token exchange error: %#", [error localizedDescription]];
} else {
[self logMessage:#"Received token response with accessToken: %#", tokenResponse.accessToken];
tokenResponse.accessToken=YOUR_TOKEN_FROM_SERVER;
}

Venmo iOS sdk crashing application, when Venmo returning back to app after login to Venmo from safari

I am using Venmo iOS sdk in my application, i have done pod file installation and all. I have Venmo app id and secret key, using same i am able to login to venmo from the sample app provided by Venmo, but if i use same app id and secret key in my application then it is getting crashed in "startLoading" method in "VENURLProtocol.m" class. Getting '[NSURL queryDictionary]: unrecognized selector sent to instance' this error.
Below is the method in which my app is crashing.This method gets executed after clicking on Allow button on Venmo login page. (in safari)
` -(void)startLoading {
NSString *host = [self.request.URL host];
NSLog(#"self.request.URL = %#",self.request.URL);
NSDictionary *queryDictionary = [self.request.URL queryDictionary];//Application is crashing here
if ([host isEqualToString:#"oauth"]) {
NSString *oAuthErrorCode = [queryDictionary valueForKey:#"error"];
if (oAuthErrorCode) {
NSString *oAuthErrorMessage = queryDictionary[#"message"] ?: #"";
NSError *oAuthError = [NSError errorWithDomain:VenmoSDKDomain
code:VENSDKErrorTransactionFailed
description:oAuthErrorMessage
recoverySuggestion:#"Please try again."];
if ([Venmo sharedInstance].OAuthCompletionHandler) {
[Venmo sharedInstance].OAuthCompletionHandler(NO, oAuthError);
}
`
Help me if anyone has used the Venmo iOS sdk and faced this kind of error.
Thanks in advance.

Cordova iOS with Spotify iOS SDK - Trigger Auth

I'm just developing Web Apps based on Cordova, but I have a problem: I want to include Spotify in a new App.
Spotify has the iOS SDK (beta) with a beginner Tutorial. That worked fine (On App load Start the Auth).
Now I would like to implement that in my WebApp using Cordova.exec(); (Not on load - I would like to Auth on Button Click (Triggered by JavaScript).
I've generated a Cordova Plugin for that - that worked. And i can trigger a Method via Cordova.exec();.
This Method get triggered:
- (BOOL)startSpotifyAuth:(CDVInvokedUrlCommand*)command {
// Create SPTAuth instance; create login URL and open it
NSURL *loginURL = [[SPTAuth defaultInstance] loginURLForClientId:kClientId declaredRedirectURL:[NSURL URLWithString:kCallbackURL] scopes:#[#"login"]];
// Opening a URL in Safari close to application launch may trigger an iOS bug, so we wait a bit before doing so.
// [UIApplication performSelector:#selector(openURL:) withObject:loginURL afterDelay:0.1];
NSLog(#"*** GOT THIS IN DEBUG CONSOLE ***");
// Ask SPTAuth if the URL given is a Spotify authentication callback
if ([[SPTAuth defaultInstance] canHandleURL:loginURL withDeclaredRedirectURL:[NSURL URLWithString:kCallbackURL]]) {
NSLog(#"*** GOT THIS - NOT - IN DEBUG CONSOLE ***");
// Call the token swap service to get a logged in session
[[SPTAuth defaultInstance] handleAuthCallbackWithTriggeredAuthURL:loginURL tokenSwapServiceEndpointAtURL:[NSURL URLWithString:kTokenSwapURL] callback:^(NSError *error, SPTSession *session)
{
if (error != nil) {
NSLog(#"*** Auth error: %#", error);
return;
}
// Call the -playUsingSession: method to play a track
[self playUsingSession:session];
}];
return YES;
}
return NO;
}
As you can see by the Debug Outputs: I did not get inside the if(). But I don't know why: The loginURL looks correct.
You're using the wrong URL in your if statement. At that point, you need to validate the URL that gets handed to your application after the user has been bounced out to Safari for authentication, NOT the one you generate using SPAuth.
Are you still having issues with your project? Maybe my Spotify iOS SDK plugin can help. I just published the first version to the plugin registry.
You can install the plugin via the cordova command line client: cordova plugin add com.timflapper.spotify.
Add the ios platform if you haven't already done so: cordova platform add ios.
The following code is a simple example of how to authenticate with Spotify and play a single track:
var session, player;
var urlScheme = 'your-custom-url-scheme';
var clientId = 'your-own-client-id';
function onDeviceReady() {
spotify.authenticate(urlScheme, clientId, 'token', authDone);
}
function authDone(error, sess) {
if (error) return console.log("ERROR!", error);
console.log(sess);
session = sess;
player = spotify.createAudioPlayer(clientId);
player.login(session, function(error) {
if (error) return console.log(error);
player.play('spotify:track:2DlfLPbXH5ncf56Nytxd4w', function(error) {
if (error) return console.log(error);
});
});
}
document.addEventListener('deviceready', onDeviceReady, false);

How to logout a user using Box V2 iOS SDK

In the V1 iOS SDK it was possible to logout the current user as follows:
[Box logoutWithCallbacks:^(id <BoxOperationCallbacks> on)
{
on.after(^(BoxCallbackResponse response)
{
});
}];
How is it done using the V2 SDK?
Here's what I do:
BoxSDK *sdk = ... // a reference to the BoxSDK for the user
sdk.OAuth2Session.accessToken = #"INVALID_TOKEN";
sdk.OAuth2Session.refreshToken = #"INVALID_TOKEN";
That's it. The next time you try to do anything with the user's session you will be forced to log in again.
Update - you can see this in action in the Box sample app at https://github.com/box/box-ios-sdk-sample-app. Look at the BoxFolderViewController.m file.
This is how to logout correctly
[BoxSDK sharedSDK].OAuth2Session.accessToken = nil;
[BoxSDK sharedSDK].OAuth2Session.refreshToken = nil;
This is how it's done with the latest SDK from https://github.com/box/box-ios-sdk
[BOXContentClient logOutAll];
swift version answer for logout box sdk session
*
boxClient.destroy { (res) in
}
boxClient = nil

iOS Facebook Native Share Dialog : initial text

I'm using Facebook Native Share Dialog in my iOS app. I give the initial text, but when dialog pop up, it add the post URL to my initial text. How can I solve this problem? Here the code.
BOOL displayedNativeDialog =
[FBNativeDialogs
presentShareDialogModallyFrom:self
initialText:#"Say something about this..."
image:[UIImage imageWithData:imageData]
url:[NSURL URLWithString:activityUrl]
handler:^(FBNativeDialogResult result, NSError *error) {
if (error) {
} else {
if (result == FBNativeDialogResultSucceeded) {
} else {
}
}
}];
If you're sharing a URL (link share) and the image is inside that link, then just set the image parameter to nil. Then the post URL will not be added to the initial text. If you provide an image, it's as if you're sharing a photo and adding the link (and text) as photo caption info.
So based on what you're trying to do, select the option that works for you, i.e. is it a link share or a photo share.

Resources