Search code examples
iosiphonetwittersttwitter

Redirect from twitter native app to iOS app after twitter login success


I have to login to my iOS app using Twitter account.
CASE 1: If Twitter app is present in iPhone I need to login to twitter app and after login success , i need to redirect back to my app.
CASE 2: If twitter app is not present in iPhone , then I need to login to twitter app through the default browser.

I am using STTwitter framework. Case 2 is working fine for me.
The below code is used for opening twitter app in browser

self.twitter = [STTwitterAPI twitterAPIWithOAuthConsumerKey:kCONSUMER_KEY
                                             consumerSecret:kCONSUMER_SECREAT];



[_twitter postTokenRequest:^(NSURL *url, NSString *oauthToken) {
    NSLog(@"-- url: %@", url);
    NSLog(@"-- oauthToken: %@", oauthToken);

    [[UIApplication sharedApplication] openURL:url];

} authenticateInsteadOfAuthorize:NO
                forceLogin:@(YES)
                screenName:nil
             oauthCallback:@"testapp://twitter_access_tokens/"
                errorBlock:^(NSError *error) {
                    NSLog(@"-- error: %@", error);

                }];

To open twitter app, the below code can be used

    NSURL *twitterURL = [NSURL URLWithString:@"twitter://"];

    if ([[UIApplication sharedApplication] canOpenURL:twitterURL]) {
        [[UIApplication sharedApplication] openURL:twitterURL];
    }

If twitter app is present in iPhone , the above lines of code can open the twitter app, but how to redirect back to my app after login success in twitter.

Please anyone help me out.


Solution

  • Twitter.app uses the Twitter accounts defined in iOS Settings.

    Also, accounts can be defined in iOS Settings even if Twitter.app is not installed.

    So, I think that in case 1 you don't need to open Twitter.app.

    You can simply instantiate STTwitterAPI with the first account available:

    + (instancetype)twitterAPIOSWithFirstAccount;
    

    or provide a specific account if you prefer to:

    + (instancetype)twitterAPIOSWithAccount:(ACAccount *)account;