简体   繁体   中英

iOS Twitter Integration

I want to integrate twitter in my application, mostly that will use for login purpose, I want to implement it like following case

  1. Check if there are any twitter account already linked with phone use that with reverse authentication
  2. If no account linked, Use some OAuth library for twitter and make authentication with that

Anyone have idea about any existing library/framework doing the same ? Thanks

PS : Minimum supported iOS version would be iOS6

You can use below code: (If no account is found from settings, then this will take your application to settings screen of device):

- (IBAction)loginWithTwitterBtn:(id)sender
{
    if ([TWTweetComposeViewController canSendTweet])
    {
        //yes user is logged in
        accountStore = [[ACAccountStore alloc] init];
        ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];

        // Request access from the user to use their Twitter accounts.
        [accountStore requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error)
         {
             // Did user allow us access?
             if (granted == YES)
             {
                 // Populate array with all available Twitter accounts
                 NSArray *arrayOfAccounts = [accountStore accountsWithAccountType:accountType];

                 ACAccount *acct = [arrayOfAccounts objectAtIndex:0];

                 // Set the cell text to the username of the twitter account
                 NSString *userID = [[acct valueForKey:@"properties"] valueForKey:@"user_id"];

             }
         }];
    }
    else
    {
        //show tweeet login prompt to user to login
        TWTweetComposeViewController *viewController = [[TWTweetComposeViewController alloc] init];

        //hide the tweet screen
        viewController.view.hidden = YES;

        //fire tweetComposeView to show "No Twitter Accounts" alert view on iOS5.1
        viewController.completionHandler = ^(TWTweetComposeViewControllerResult result) {
            if (result == TWTweetComposeViewControllerResultCancelled) {
                [self dismissModalViewControllerAnimated:NO];
            }
        };
        [self presentModalViewController:viewController animated:NO];

        //hide the keyboard
        [viewController.view endEditing:YES];
    }
}

//*******************

https://github.com/aryansbtloe/LoginViaSocialMedias

This is an example project for adding login via facebook,twitter,google plus and four square support to your ios application.

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