BigK
BigK

Reputation: 43

What is wrong with Twitter API on iOs 8?

I want to create an action in Objetive-C that makes an instant "follow" in a Twitter Acount, but when I launch my app it crashes. Could anyone tell me what is wrong?

It returns thread 1 exc_bad_access.

- (IBAction)Twitter:(id)sender {

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

    [accountStore requestAccessToAccountsWithType:accountType options:nil
                                       completion:^(BOOL granted, NSError *error)  {
        if(granted) {

            NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];

            if ([accountsArray count] > 0) {

                ACAccount *twitterAccount = [accountsArray objectAtIndex:0];

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

                NSURL *URLTwitter = [NSURL URLWithString:@"https://api.twitter.com/1.1/friendships/create.format"];

                SLRequest *postRequest = [SLRequest requestForServiceType:@"Twitter" requestMethod:SLRequestMethodPOST URL:URLTwitter parameters:tempDict];

                [postRequest setAccount:twitterAccount];

                [postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
                    NSString *output = [NSString stringWithFormat:@"HTTP response status: %li", [urlResponse statusCode]];
                    NSLog(@"%@", output);

                }];
            }
        }
    }];
}

Upvotes: 1

Views: 355

Answers (2)

BigK
BigK

Reputation: 43

Here is what was wrong...
1. Firstly, since the new Twitter API version 1.1 the correct URL is: https://api.twitter.com/1.1/friendships/create.json.
2. The 'for key' value for the user name must to be "screen_name", and it will be like the following fragment:

NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] init];
[tempDict setValue:@"UserNameOnTwitter" forKey:@"screen_name"];
[tempDict setValue:@"true" forKey:@"follow"];
  1. The value for 'requestForServiceType' must to be SLServiceTypeTwitter as the following:

    SLRequest *postRequest = [SLRequest requestForServiceType: SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:URLTwitter parameters:tempDict];

So the whole block will be:

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

[accountStore requestAccessToAccountsWithType:accountType options:nil
                                   completion:^(BOOL granted, NSError *error)  {
    if(granted) {

        NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];

        if ([accountsArray count] > 0) {

            ACAccount *twitterAccount = [accountsArray objectAtIndex:0];

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

            NSURL *URLTwitter = [NSURL URLWithString:@"https://api.twitter.com/1.1/friendships/create.json"];

            SLRequest *postRequest = [SLRequest requestForServiceType: SLServiceTypeTwitter  requestMethod:SLRequestMethodPOST URL:URLTwitter parameters:tempDict];

            [postRequest setAccount:twitterAccount];

            [postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
                NSString *output = [NSString stringWithFormat:@"HTTP response status: %li", [urlResponse statusCode]];
                NSLog(@"%@", output);
            }];
        }
    }
}];

Thanks anyway everyone.

Upvotes: 0

inorganik
inorganik

Reputation: 25525

Your twitter url is incorrect. It should be

https://api.twitter.com/1.1/friendships/create.json

Upvotes: 1

Related Questions