1

I'm making an app where I want to post to twitter (without a modal view). I'm quite new to the Social.framework so I'm having some issues. More specifically I get the error:

Obtaining the web lock from a thread other than the main thread or the web thread. UIKit should not be called from a secondary thread.

And I'm not sure what's causing this error.

My Twitter method looks like this:

- (void)postToTwitter
{
    ACAccountType *twitterType = [self.accountStore   accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];

    SLRequestHandler requestHandler =
    ^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
        if (responseData) {
            NSInteger statusCode = urlResponse.statusCode;
            if (statusCode >= 200 && statusCode < 300) {
                NSDictionary *postResponseData =
                [NSJSONSerialization JSONObjectWithData:responseData
                                            options:NSJSONReadingMutableContainers
                                              error:NULL];
                NSLog(@"[SUCCESS!] Created Tweet with ID: %@", postResponseData[@"id_str"]);
            }
            else {
                NSLog(@"[ERROR] Server responded: status code %d %@", statusCode,
                  [NSHTTPURLResponse localizedStringForStatusCode:statusCode]);
            }
        }
        else {
            NSLog(@"[ERROR] An error occurred while posting: %@", [error localizedDescription]);
        }
    };

    ACAccountStoreRequestAccessCompletionHandler accountStoreHandler =
    ^(BOOL granted, NSError *error) {
        if (granted) {
            NSArray *accounts = [self.accountStore accountsWithAccountType:twitterType];
            NSURL *url = [NSURL URLWithString:@"https://api.twitter.com"
                      @"/1.1/statuses/update_with_media.json"];
            NSDictionary *params = @{@"status" : self.postTextField.text};
            SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeTwitter
                                                requestMethod:SLRequestMethodPOST
                                                          URL:url
                                                   parameters:params];

            [request setAccount:[accounts lastObject]];
            [request performRequestWithHandler:requestHandler];
        }
        else {
            NSLog(@"[ERROR] An error occurred while asking for user authorization: %@",
              [error localizedDescription]);
        }
    };

    [self.accountStore requestAccessToAccountsWithType:twitterType
                                           options:NULL
                                        completion:accountStoreHandler];
}

Much like the example from here: https://dev.twitter.com/docs/ios/posting-images-using-twrequest

Can anyone see any issues with the code, or could it be something else that is causing the error?

Note: It seems like none of the NSLogs is getting called.

Anders
  • 2,903
  • 7
  • 58
  • 114
  • Out of curiosity, from which thread are you trying to post to Twitter? The error you are receiving sounds like you are trying to update UI elements from a background thread (ie. not from the main thread). – gberginc Apr 18 '13 at 03:33
  • @gberginc Should be the main thread. I have also tried `perforSelectorOnMainThread...`, but the same error. – Anders Apr 18 '13 at 18:50
  • Unfortunately, I cannot reproduce your problem. I integrated entire method but the only problem I had was with the `update_with_media` and I received 403 at first (which is as specified in the documentation). Therefore, have you tried any other Twitter API methods like `GET update` and `POST user_timeline`? I suggest you add breakpoints in both of your handlers to see what gets called. – gberginc Apr 19 '13 at 03:47
  • Thanks. Changing the URL seemed to do the trick. I still get the error, but it posts to Twitter at least! – Anders Apr 20 '13 at 07:38

0 Answers0