重定向到Twitter登录的设置屏幕

时间:2014-01-08 13:05:21

标签: ios objective-c xcode twitter

在Twitter中,如果用户已在设置屏幕中的Twitter帐户中登录它将允许发布。否则它将显示警报为“没有Twitter帐户”,带有2个选项 “设置”“取消”。如果Cancel被点击,它将关闭提醒并拒绝发布到Twitter。如果Settings被点击,则不会重定向到“设置”屏幕。 我也用过

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=TWITTER"]];

但是没有运气。据我所知,所有人都说从iOS 5.1它不会工作。但我看到一些应用程序仍然重定向到iOS7中的设置屏幕。是否可以在iOS7中重定向。 提前致谢。 enter image description here

1 个答案:

答案 0 :(得分:5)

您可以在登录按钮的操作中使用以下代码:

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"];
             TwitterIdStr = [[NSString alloc] initWithString:userID];
             FbIdStr = [[NSString alloc] init];
             NSLog(@"%@",userID);
             NSString *networkCheck = [[NSUserDefaults standardUserDefaults] valueForKey:@"isNetWorkAvailable"];
             if ([networkCheck isEqualToString:@"NotConnected"])
             {
                 // not connected
                 dispatch_async(dispatch_get_main_queue(), ^{
                     // Display/dismiss your alert
                     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No network connection" message:@"You must be connected to the internet to proceed." delegate:nil cancelButtonTitle:@"OK"otherButtonTitles:nil];
                     [alert show];
                 });

             } else
             {
                 fNameStr = [[NSString alloc] init];
                 lNameStr = [[NSString alloc] init];
                 emailStr = [[NSString alloc] init];

                 [self startProgressViewAgain];
             }

         }
     }];
}
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];
}