2014-07-22 66 views
3

我有想出一個解決方案,從簡單登錄iOS版quick start以下煩惱:是否有適用於Twitter的Firebase iOS簡單登錄示例?

因爲它是可能的設備有一個以上的Twitter帳戶連接,您需要提供可用於確定登錄哪個帳戶的塊。使用自己的代碼替換下面的「[yourApp selectUserName:usernames]」以從用戶名列表中進行選擇。

這是提供的代碼:

[authClient loginToTwitterAppWithId:@"YOUR_CONSUMER_KEY" 
multipleAccountsHandler:^int(NSArray *usernames) { 

// If you do not wish to authenticate with any of these usernames, return NSNotFound. 
return [yourApp selectUserName:usernames]; 
} withCompletionBlock:^(NSError *error, FAUser *user) { 
    if (error != nil) { 
     // There was an error authenticating 
    } else { 
     // We have an authenticated Twitter user 
    } 
}]; 

會一個UIActionSheet,允許用戶挑選使用哪個帳戶是最好的?這將如何完成?

回答

0

我發現了一個很好的例子,說明如何使用Awesome Chat的代碼做到這一點。它使用UIActionSheet就像我試圖。

//------------------------------------------------------------------------------------------------------------------------------------------------- 
- (IBAction)actionTwitter:(id)sender 
//------------------------------------------------------------------------------------------------------------------------------------------------- 
{ 
    [ProgressHUD show:@"In progress..." Interaction:NO]; 
    //--------------------------------------------------------------------------------------------------------------------------------------------- 
    selected = 0; 
    //--------------------------------------------------------------------------------------------------------------------------------------------- 
    ACAccountStore *account = [[ACAccountStore alloc] init]; 
    ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter]; 
    //--------------------------------------------------------------------------------------------------------------------------------------------- 
    [account requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error) 
    { 
     if (granted) 
     { 
      accounts = [account accountsWithAccountType:accountType]; 
      //------------------------------------------------------------------------------------------------------------------------------------- 
      if ([accounts count] == 0) 
       [self performSelectorOnMainThread:@selector(showError:) withObject:@"No Twitter account was found" waitUntilDone:NO]; 
      //------------------------------------------------------------------------------------------------------------------------------------- 
      if ([accounts count] == 1) [self performSelectorOnMainThread:@selector(loginTwitter) withObject:nil waitUntilDone:NO]; 
      if ([accounts count] >= 2) [self performSelectorOnMainThread:@selector(selectTwitter) withObject:nil waitUntilDone:NO]; 
     } 
     else [self performSelectorOnMainThread:@selector(showError:) withObject:@"Access to Twitter account was not granted" waitUntilDone:NO]; 
    }]; 
} 

//------------------------------------------------------------------------------------------------------------------------------------------------- 
- (void)selectTwitter 
//------------------------------------------------------------------------------------------------------------------------------------------------- 
{ 
    UIActionSheet *action = [[UIActionSheet alloc] initWithTitle:@"Choose Twitter account" delegate:self cancelButtonTitle:nil 
              destructiveButtonTitle:nil otherButtonTitles:nil]; 
    //--------------------------------------------------------------------------------------------------------------------------------------------- 
    for (NSInteger i=0; i<[accounts count]; i++) 
    { 
     ACAccount *account = [accounts objectAtIndex:i]; 
     [action addButtonWithTitle:account.username]; 
    } 
    //--------------------------------------------------------------------------------------------------------------------------------------------- 
    [action addButtonWithTitle:@"Cancel"]; 
    action.cancelButtonIndex = accounts.count; 
    [action showInView:self.view]; 
} 

//------------------------------------------------------------------------------------------------------------------------------------------------- 
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 
//------------------------------------------------------------------------------------------------------------------------------------------------- 
{ 
    if (buttonIndex != actionSheet.cancelButtonIndex) 
    { 
     selected = buttonIndex; 
     [self loginTwitter]; 
    } 
    else [ProgressHUD dismiss]; 
} 

//------------------------------------------------------------------------------------------------------------------------------------------------- 
- (void)loginTwitter 
//------------------------------------------------------------------------------------------------------------------------------------------------- 
{ 
    Firebase *ref = [[Firebase alloc] initWithUrl:FIREBASE]; 
    FirebaseSimpleLogin *authClient = [[FirebaseSimpleLogin alloc] initWithRef:ref]; 
    //--------------------------------------------------------------------------------------------------------------------------------------------- 
    [authClient loginToTwitterAppWithId:TWITTER_KEY multipleAccountsHandler:^int(NSArray *usernames) 
    { 
     return (int) selected; 
    } 
    withCompletionBlock:^(NSError *error, FAUser *user) 
    { 
     if (error == nil) 
     { 
      if (user != nil) [delegate didFinishLogin:ParseUserData(user.thirdPartyUserData)]; 
      [self dismissViewControllerAnimated:YES completion:^{ [ProgressHUD dismiss]; }]; 
     } 
     else 
     { 
      NSString *message = [error.userInfo valueForKey:@"NSLocalizedDescription"]; 
      [self performSelectorOnMainThread:@selector(showError:) withObject:message waitUntilDone:NO]; 
     } 
    }]; 
} 
1
#import <Twitter/Twitter.h> 
#import <Accounts/Accounts.h> 

對於登錄:

ACAccountStore *accountStore = [[ACAccountStore alloc] init]; 

// Create an account type that ensures Twitter accounts are retrieved. 
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter]; 
[accountStore requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error) { 


    // Request access from the user to use their Twitter accounts. 
    // [accountStore requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) { 

    //NSLog(@"%@",error); 
    if(granted) { 

     // Get the list of Twitter accounts. 
     NSArray *accountsArray = [accountStore accountsWithAccountType:accountType]; 
     //NSLog(@"%@",accountsArray); 
     twitterAccountsArray=[accountsArray mutableCopy]; 



     if ([twitterAccountsArray count] > 0){ 
      sheet = [[UIActionSheet alloc] initWithTitle:@"Choose an Account" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil]; 
      for (ACAccount *acct in twitterAccountsArray) { 
       [sheet addButtonWithTitle:acct.username]; 
      } 




     } 

    } 

    else{ 
     dispatch_async(dispatch_get_main_queue(), ^{ 
      //NSLog(@"%@",error); 

      if (![error.localizedDescription isEqual:[NSNull null]] &&[error.localizedDescription isEqualToString:@"No access plugin was found that supports the account type com.apple.twitter"]) { 



      } 
      else 


      [Utility showAlertWithString:@"We could not find any Twitter account on the device"]; 

     }); 
    } 

}]; 

它會打開一個動作片,這將顯示您的設備具有帳戶列表。

張貼:

必須有至少一個有效的Twitter帳戶加入您的iPhone設備上。

if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) 
{ 

    NSString *[email protected]"Text to be posted"; 


    SLComposeViewController *tweetSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter]; 

    tweetSheet.completionHandler = ^(SLComposeViewControllerResult result) { 


     switch(result) { 
       // This means the user cancelled without sending the Tweet 
     case SLComposeViewControllerResultCancelled:{ 

     } 
     break; 

     case SLComposeViewControllerResultDone:{ 



    } 
       break; 
    } 

    }; 
    [tweetSheet setInitialText:initialText]; 
    if (initialText) { 


     [self presentViewController:tweetSheet animated:YES completion:nil]; 

    } 

} 
+0

不幸的是,它似乎不適用於最新的iOS 8測試版。 Apple已棄用UIActionSheet以支持UIAlertViewController。 – dlanmaar

相關問題