简体   繁体   中英

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.

Once control passes out of your app, there is no way to programmatically bring the user back. They would need to manually come back. That being said, look through the Twitter api that apple provides since the iPhone can log into Twitter without their app. You should be able to accomplish what you want without them leaving your app to get into the Twitter app or site.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM