4

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'

この奇妙なクラッシュログについては知りません..

教えてください、これを取り除く方法..

4

4 に答える 4

5

以下の方法を使用します。

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

   if (granted) {

            //Your code
            }
        }
   }];
于 2013-03-06T12:44:31.473 に答える
1

@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

     });
 }

ハッピーコーディング:)

于 2014-02-03T08:53:54.477 に答える
1

これは少し遅れていますが、このエラーが発生する理由は、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

于 2013-09-05T16:51:02.950 に答える