4

When I run below code with iOS 6.0, Its working

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
             }
         });

     }];

and When I run this code with iOS 5.0 or 5.1, It crashes with following output,

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

Don't know about this weird crash log..

Please tell me, How to get rid of this..

Mehul Mistri
  • 15,037
  • 14
  • 70
  • 94
  • I am having the same issue, how did u manage to get rid of it..! Please help..! – JgdGuy Aug 22 '13 at 12:40
  • There is a good WWDC 2012 presentation about this: http://adcdownload.apple.com/wwdc_2012/wwdc_2012_session_pdfs/session_306__integrating_with_facebook_twitter_and_sina_weibo.pdf – iwasrobbed Sep 18 '13 at 02:36

4 Answers4

5

Use below method:

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

   if (granted) {

            //Your code
            }
        }
   }];
Ashley Mills
  • 50,474
  • 16
  • 129
  • 160
yen
  • 772
  • 3
  • 10
1

Try update for this:

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
}
CReaTuS
  • 2,593
  • 1
  • 19
  • 31
1

This is a bit late, but the reason you're getting that error is that requestAccessToAccountsWithType:options:completion: is new in iOS 6.

In iOS 5 and earlier use the requestAccessToAccountsWithType:withCompletionHandler method instead (this method is deprecated in iOS 6)

See the docs: https://developer.apple.com/library/ios/documentation/Accounts/Reference/ACAccountStoreClassRef/Reference/Reference.html#//apple_ref/doc/uid/TP40011021-CH1-SW12

Jeremy Wiebe
  • 3,894
  • 22
  • 31
1

Thanks @CReaTuS, I want to clarify this up to some more bit, Note that in case of iOS6 we make SLRequest where in iOS5 we have to perform request using TWRequest. See below-

 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

     });
 }

Happy coding :)

Mohd Asim
  • 2,171
  • 1
  • 18
  • 15
  • SLRequest *followRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:[NSURL URLWithString:@"https://api.twitter.com/1.1/friendships/create.json"] parameters:tempDict]; this method is also available for ios 6.0 or later, do suggest alternative method – ViruMax Feb 18 '14 at 13:14
  • @ViruMax look carefully in later segment, the request(specific to iOS5)--> TWRequest *followRequest = [[TWRequest alloc] initWithURL:[NSURL URLWithString:@"https://api.twitter.com/1/friendships/create.json"] parameters:tempDict requestMethod:TWRequestMethodPOST]; – Mohd Asim Feb 20 '14 at 07:23