2011-11-04 55 views
3

我想點擊添加一個按鈕,允許用戶在iOS 5中登錄他們的Twitter帳戶,但我希望他們的twitter處理能夠填充到UITextField中。我有代碼確保他們的iPhone運行iOS 5並且他們的帳戶已啓用。我如何保留他們的手柄?保留Twitter中的用戶名iOS5 iPhone

回答

4
//get Twitter username and store it  
    ACAccountStore *accountStore = [[ACAccountStore alloc] init]; 

    ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter]; 

    [accountStore requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) { 
     if(granted) { 
      NSArray *accountsArray = [accountStore accountsWithAccountType:accountType]; 

      ACAccount *twitterAccount = [accountsArray objectAtIndex:0]; 
      ; 
        [[NSUserDefaults standardUserDefaults] setValue:twitterAccount.username forKey:@"twitterHandle"]; 
     } 

    //get the username later... 
    [textField setText:[[NSUserDefaults standardUserDefaults] valueForKey:@"twitterHandle"]]; 
+0

你valueForKey應該是twitterHandle。 – jrtc27

+0

@ jrtc27謝謝 –

+0

這個代碼有幾個問題,但是一旦你改變了一些東西,它就會很好用(例如standDefaults = standardUserDefaults) – Eric