Twitter SLRequest performRequestWithHandler - 无法准备 URL 请求 [英] Twitter SLRequest performRequestWithHandler - Could not prepare the URL request

查看:68
本文介绍了Twitter SLRequest performRequestWithHandler - 无法准备 URL 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的 iOS 应用中实现在 Twitter 上关注我们".这是我的代码.但它给出错误无法准备 URL 请求".请帮忙!

I am trying to implement "Follow Us On Twitter" in my iOS app. Here is my code. But it give error "Could not prepare the URL request". Please help!

ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountType = [accountStore
                          accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[accountStore
 requestAccessToAccountsWithType:accountType
 options:NULL
 completion:^(BOOL granted, NSError *error) {
     if (granted) {
         NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] init];
         [tempDict setValue:@"a4arpan" forKey:@"screen_name"];
         [tempDict setValue:@"true" forKey:@"follow"];
         SLRequest *postRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter
                                                     requestMethod:SLRequestMethodPOST
                                                               URL:[NSURL URLWithString:@"https://api.twitter.com/1.1/friendships/create.json"]
                                                        parameters:tempDict];

         ACAccount * twitterAccount = [[ACAccount alloc] initWithAccountType:accountType];
         twitterAccount.username = twitterUsername;
         [postRequest setAccount:twitterAccount];
         [postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {

             if (responseData) {
                 if (urlResponse.statusCode >= 200 && urlResponse.statusCode < 300) {
                     NSError *jsonError;
                     NSDictionary *timelineData =
                     [NSJSONSerialization
                      JSONObjectWithData:responseData
                      options:NSJSONReadingAllowFragments error:&jsonError];

                     if (timelineData) {
                         NSLog(@"Timeline Response: %@\n", timelineData);
                     }
                     else {
                         // Our JSON deserialization went awry
                         NSLog(@"JSON Error: %@", [jsonError localizedDescription]);
                     }

                     if ([urlResponse statusCode] == 200) {
                         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Follow us successfull" message:nil delegate:nil cancelButtonTitle:@"Thanx" otherButtonTitles:nil, nil];
                         [alert show];
                     }
                     else {
                         if ([tpAppMode isEqualToString:@"sandbox"])
                             NSLog(@"%@", [error localizedDescription]);

                         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Follow us Failed" message:nil delegate:nil cancelButtonTitle:@"Thanx" otherButtonTitles:nil, nil];
                         [alert show];
                     }
                 }
                 else {
                     // The server did not respond successfully... were we rate-limited?
                     NSLog(@"The response status code is %d", urlResponse.statusCode);
                 }
             }
             else {
                 NSString *output = [NSString stringWithFormat:@"HTTP response status: %@ %@", [error localizedDescription], [error localizedFailureReason]];
                 NSLog(@"%@", output);
             }
         }];
     }
     else {
         if ([tpAppMode isEqualToString:@"sandbox"])
             NSLog(@"%@", [error localizedDescription]);

         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Follow us Failed" message:nil delegate:nil cancelButtonTitle:@"Thanx" otherButtonTitles:nil, nil];
         [alert show];
     }
 }];

我已按照 Twitter Dev 中提到的所有步骤进行操作

I have followed all the steps mentioned on Twitter Dev

推荐答案

我就是这样做的!

SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:[NSURL URLWithString:@"https://api.twitter.com/1.1/friendships/create.json"] parameters:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"AnatoliyGatt", @"true", nil] forKeys:[NSArray arrayWithObjects:@"screen_name", @"follow", nil]]];
    [request setAccount:[[self twitterAccounts] lastObject]];
    [request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
        if(responseData) {
            NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
            if(responseDictionary) {
                // Probably everything gone fine
            }
        } else {
            // responseDictionary is nil
        }
    }];

这篇关于Twitter SLRequest performRequestWithHandler - 无法准备 URL 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆