2014-07-22 1 views
3

나는 문제가 단순 로그인 아이폰 OS quick start에서 다음 해결책을 오는 데 :Twitter 용 Firebase iOS Simple Login 예가 있습니까?

장치가 두 개 이상의 트위터 계정을 연결해야하는 것이 가능하기 때문에

, 당신이 제공해야합니다 어떤 계정을 로그인 할 것인지 결정하는 데 사용할 수있는 블록. "[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"]; 

     }); 
    } 

}]; 

. 게시

:

이 있어야 하나 이상이어야 트위터 계정은 아이폰 장치에 추가.

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은 UIActionertheet 대신 UIActionertheet를 사용하지 않을 것입니다. – dlanmaar

관련 문제