【问题标题】:Twitter integration issue with ACAccountStore (iOS 5)与 ACAccountStore (iOS 5) 的 Twitter 集成问题
【发布时间】:2013-02-21 02:19:59
【问题描述】:

当我使用 iOS 6.0 运行以下代码时,它可以正常工作

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'

不知道这个奇怪的崩溃日志..

请告诉我,如何摆脱这个..

【问题讨论】:

标签: iphone ipad ios5 twitter acaccountstore


【解决方案1】:

使用以下方法:

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

   if (granted) {

            //Your code
            }
        }
   }];

【讨论】:

    【解决方案2】:

    为此尝试更新:

    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
    }
    

    【讨论】:

      【解决方案3】:

      这有点晚了,但您收到该错误的原因是 requestAccessToAccountsWithType:options:completion: 是 iOS 6 中的新功能。

      在 iOS 5 及更早版本中,请改用 requestAccessToAccountsWithType:withCompletionHandler 方法(此方法在 iOS 6 中已弃用)

      查看文档:https://developer.apple.com/library/ios/documentation/Accounts/Reference/ACAccountStoreClassRef/Reference/Reference.html#//apple_ref/doc/uid/TP40011021-CH1-SW12

      【讨论】:

        【解决方案4】:

        感谢@CReaTuS,我想进一步澄清这一点,请注意,在 iOS6 的情况下,我们发出 SLRequest,而在 iOS5 中,我们必须使用 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
        
             });
         }
        

        编码愉快:)

        【讨论】:

        • SLRequest *followRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:[NSURL URLWithString:@"api.twitter.com/1.1/friendships/create.json"]parameters:tempDict]; 这个方法也适用于 ios 6.0 或更高版本,建议替代方法
        • @ViruMax 在后面的部分仔细看,请求(特定于iOS5)--> TWRequest *followRequest = [[TWRequest alloc] initWithURL:[NSURL URLWithString:@"api.twitter.com/1/friendships/create.json"]parameters:tempDict requestMethod :TWRequestMethodPOST];
        猜你喜欢
        • 2012-02-25
        • 2012-09-06
        • 1970-01-01
        • 2012-07-04
        • 1970-01-01
        • 2011-12-05
        • 2013-03-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多