2013-03-06 32 views
4

當我運行下面的代碼與iOS 6.0,它的工作Twitter的整合問題與ACAccountStore(iOS 5中)

ACAccountStore *account = [[ACAccountStore alloc] init]; 
ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter]; 

[account requestAccessToAccountsWithType:accountType options:nil 
            completion:^(BOOL granted, NSError *error) 
    { 
     dispatch_async(dispatch_get_main_queue(), ^{ 

      if (granted) 
      { 
       //MY CODE 
      } 
     }); 

    }]; 

,當我運行此代碼與iOS 5.0或5.1,它崩潰與下面的輸出,

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', 
reason: '-[ACAccountStore requestAccessToAccountsWithType:options:completion:]: 
unrecognized selector sent to instance 0x68a57c0' 

不知道有這個奇怪的崩潰日誌..

請告訴我,如何擺脫這種..

+0

,並如何ü設法擺脫它..!請幫忙..! – JgdGuy 2013-08-22 12:40:00

+0

這是一個很好的WWDC 2012演示一下:http://adcdownload.apple.com/wwdc_2012/wwdc_2012_session_pdfs/session_306__integrating_with_facebook_twitter_and_sina_weibo.pdf – iwasrobbed 2013-09-18 02:36:08

回答

5

使用以下方法:

[account requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) 
{ 

    if (granted) { 

      //Your code 
      } 
     } 
    }]; 
+1

bhura給予了正確的答案 – matthewb 2013-10-18 04:39:52

1

此嘗試更新:

ACAccountStore *account = [[ACAccountStore alloc] init]; 
ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter]; 

// iOS 6 
if ([account respondsToSelector:@selector(requestAccessToAccountsWithType: options: completion:)]) 
{ 
[account requestAccessToAccountsWithType:accountType options:nil 
            completion:^(BOOL granted, NSError *error) 
    { 
     dispatch_async(dispatch_get_main_queue(), ^{ 

      if (granted) 
      { 
       //MY CODE 
      } 
     }); 

    }]; 
} 

// iOS 5 
else if ([account respondsToSelector:@selector(requestAccessToAccountsWithType: withCompletionHandler:)]) 
{ 
[account requestAccessToAccountsWithType:accountType 
            withCompletionHandler:^(BOOL granted, NSError *error) 
    { 
     dispatch_async(dispatch_get_main_queue(), ^{ 

      if (granted) 
      { 
       //MY CODE 
      } 
     }); 

    }]; 
} 
else 
{ 
// iOS 4 or less 
} 
1

感謝@CReaTuS,我想澄清一點點,請注意,在iOS6的情況下,我們在iOS5中製作SLRequest,我們必須使用TWRequest執行請求。見如下─

ACAccountStore *accountStore = [[ACAccountStore alloc] init]; 
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter]; 

if ([accountStore respondsToSelector:@selector(requestAccessToAccountsWithType: options: completion:)]) 
{ 
[accountStore requestAccessToAccountsWithType:accountType options:nil 
completion:^(BOOL granted, NSError *error) 
{ 
dispatch_async(dispatch_get_main_queue(), ^{ 

if (granted) 
{ 
    // Get the list of Twitter accounts. 
    NSArray *accountsArray = [accountStore accountsWithAccountType:accountType]; 

    // For the sake of brevity, we'll assume there is only one Twitter account present. 
    // You would ideally ask the user which account they want to tweet from, if there is more than one Twitter account present. 
    if ([accountsArray count] > 0) { 
     // Grab the initial Twitter account to tweet from. 
     ACAccount *twitterAccount = [accountsArray objectAtIndex:0]; 

     NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] init]; 
     [tempDict setValue:@"Twitter_Name" forKey:@"screen_name"]; 
     [tempDict setValue:@"true" forKey:@"follow"]; 

     //Code specific to iOS6 or later 

     SLRequest *followRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:[NSURL URLWithString:@"https://api.twitter.com/1.1/friendships/create.json"] parameters:tempDict]; 

     // To unfollow hit URL-https://api.twitter.com/1.1/friendships/destroy.json 

     [followRequest setAccount:twitterAccount]; 
     [followRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) { 
      NSString *output = [NSString stringWithFormat:@"HTTP response status: %i", [urlResponse statusCode]]; 
      NSLog(@"%@", output); 
      if (error) { 
       dispatch_async(dispatch_get_main_queue(), ^{ 
        //Update UI to show follow request failed 

       }); 
      } 
      else { 
       dispatch_async(dispatch_get_main_queue(), ^{ 
        //Update UI to show success 


       }); 
      } 
     }]; 
    } 


} 
}); 

}]; 
} 
else if ([accountStore respondsToSelector:@selector(requestAccessToAccountsWithType: withCompletionHandler:)]) 
{ 
[accountStore requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) 
{ 
dispatch_async(dispatch_get_main_queue(), ^{ 

if (granted) 
{   
    // Get the list of Twitter accounts. 
    NSArray *accountsArray = [accountStore accountsWithAccountType:accountType]; 

    // For the sake of brevity, we'll assume there is only one Twitter account present. 
    // You would ideally ask the user which account they want to tweet from, if there is more than one Twitter account present. 
    if ([accountsArray count] > 0) { 
     // Grab the initial Twitter account to tweet from. 
     ACAccount *twitterAccount = [accountsArray objectAtIndex:0]; 

     NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] init]; 
     [tempDict setValue:@"Twitter_Name" forKey:@"screen_name"]; 
     [tempDict setValue:@"true" forKey:@"follow"]; 

     //Code specific to iOS5 

     TWRequest *followRequest = [[TWRequest alloc] initWithURL:[NSURL URLWithString:@"https://api.twitter.com/1/friendships/create.json"] 
                 parameters:tempDict 
                requestMethod:TWRequestMethodPOST]; 


     [followRequest setAccount:twitterAccount]; 
     [followRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) { 
      NSString *output = [NSString stringWithFormat:@"HTTP response status: %i", [urlResponse statusCode]]; 
      NSLog(@"%@", output); 
      if (error) { 
       dispatch_async(dispatch_get_main_queue(), ^{ 
        //Update UI to show follow request failed 

       }); 
      } 
      else { 
       dispatch_async(dispatch_get_main_queue(), ^{ 
        //Update UI to show success 
       }); 
      } 
     }]; 
    } 


} 
}); 

}]; 
} 
else 
{ 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     //Update UI to show follow request completely failed 

    }); 
} 

編碼愉快:)

我有同樣的問題
+0

SLRequest * followRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST網址: [NSURL URLWithString:@「https://api.twitter.com/1.1/friendships/create.json」] parameters:tempDict]; 此方法也可用於ios 6.0或更高版本,建議替代方法 – ViruMax 2014-02-18 13:14:16

+0

@ViruMax仔細查看後面的段,請求(特定於iOS5) - > TWRequest * followRequest = [[TWRequest alloc] initWithURL:[NSURL URLWithString: @「https://api.twitter.com/1/friendships/create.json」]參數:tempDict requestMethod:TWRequestMethodPOST]; – 2014-02-20 07:23:37