Reputation:
I config my app to use the login by twitter feature. But I cannot turn back to my app after authorize app success on safari. I saw another question with the same problem on stack overflow use UrlShemes but this not work with me(I don't have enough reputation to add comments to ask them into the accept answer).So please let me know: If I can turn back to my app after authorize successful (like Facebook API can do)? If this is possible, so how to do this?
Here is my code:
self.twitter = [STTwitterAPI twitterAPIWithOAuthConsumerKey:Twitter_API_KEY
consumerSecret:Twitter_API_SECRET];
[_twitter postTokenRequest:^(NSURL *url, NSString *oauthToken) {
NSLog(@"-- url: %@", url);
NSLog(@"-- oauthToken: %@", oauthToken);
[[UIApplication sharedApplication] openURL:url];
} authenticateInsteadOfAuthorize:NO
forceLogin:@(YES)
screenName:nil
oauthCallback:@"sutrixsnowball://"
errorBlock:^(NSError *error) {
NSLog(@"-- error: %@", error);
}];// The "sutrixsnowball" is URL Schemes item into plist file
It just open the web page on safari like the image below (It's not a real url when I copy, so I post the image):
Upvotes: 1
Views: 1310
Reputation: 24247
Note For those still using STTwitter I would recommend switching over to Twitter's official SDK found here
You need to make sure that your oauthCallback is a scheme listed in your Apps plist. You could also use a cleaner reverse domain host scheme just to be super sure you never conflict with any other Apps.
So your App plist would look something like this:
Where I'm using com.twitteroauthcallback.sutrixsnowball as the URL scheme.
The you need to handle this callback in your app delegate. Something like this:
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
if ([[[url scheme] lowercaseString] isEqualToString:@"com.twitteroauthcallback.sutrixsnowball"] == NO) {
return NO;
}
NSMutableDictionary *twitterParamsDictionary = [NSMutableDictionary dictionary];
NSArray *queryComponents = [[url query] componentsSeparatedByString:@"&"];
for(NSString *s in queryComponents) {
NSArray *pair = [s componentsSeparatedByString:@"="];
if([pair count] != 2) continue;
NSString *key = pair[0];
NSString *value = pair[1];
twitterParamsDictionary[key] = value;
}
NSString *token = twitterParamsDictionary[@"oauth_token"];
NSString *verifier = twitterParamsDictionary[@"oauth_verifier"];
if (token && verifier) {
[self setOAuthToken:token oauthVerifier:verifier];
return YES;
}
return NO;
}
- (void)setOAuthToken:(NSString *)token oauthVerifier:(NSString *)verifier {
__weak typeof (self)weakself = self;
[self.twitterAPI postAccessTokenRequestWithPIN:verifier
successBlock:^(NSString *oauthToken, NSString *oauthTokenSecret, NSString *userID, NSString *screenName) {
[weakself.twitterAPI getUserInformationFor:screenName
successBlock:^(NSDictionary *user) {
///we have an account
} errorBlock:^(NSError *error) {
/// error
}];
} errorBlock:^(NSError *error) {
///error
}];
}
You will notice that the success block for the postAccessTokenRequestWithPin:
conveniently passes back a token and a secret. Looking at STTwitterAPI you will see the following constructor:
+ (instancetype)twitterAPIWithOAuthConsumerKey:(NSString *)consumerKey
consumerSecret:(NSString *)consumerSecret
oauthToken:(NSString *)oauthToken
oauthTokenSecret:(NSString *)oauthTokenSecret
So if you keep those values somewhere on the device you can login to twitter on next startup:
/// Next app launch
STTwitterAPI *twitterAPI = [STTwitterAPI twitterAPIWithOAuthConsumerKey:consumerKey consumerSecret:consumerSecret oauthToken:oauthToken oauthTokenSecret:oauthTokenSecret];
[twitterAPI verifyCredentialsWithSuccessBlock:^(NSString *username) {
/// we still good to go
} errorBlock:^(NSError *error) {
/// token has expired. User needs to login again
}];
It turns out than in order to work STTwitter requires that the oauthCallback look something like this
oauthCallback:@"<YOUR_URL_SCHEME>://twitter_access_tokens/"
Upvotes: 3