iOS:发布到Twitter会导致错误 - 从主线程以外的线程获取Web锁定

时间:2013-04-17 19:32:38

标签: ios cocoa-touch twitter slrequest

我正在制作一个应用程序,我想发布到Twitter(没有模态视图)。我对Social.framework很新,所以我遇到了一些问题。更具体地说,我得到错误:

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.

而且我不确定是什么导致了这个错误。

我的Twitter方法如下:

- (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];
}

很像这里的例子:https://dev.twitter.com/docs/ios/posting-images-using-twrequest

任何人都可以看到代码有任何问题,还是可能导致错误的其他问题?

注意:似乎没有一个NSLog被调用。

0 个答案:

没有答案