【问题标题】:What is wrong with Twitter API on iOs 8?iOS 8 上的 Twitter API 有什么问题?
【发布时间】:2015-02-12 10:19:27
【问题描述】:

我想在 Objetive-C 中创建一个动作,在 Twitter 帐户中立即“关注”,但是当我启动我的应用程序时它崩溃了。谁能告诉我怎么了?

它返回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);

                }];
            }
        }
    }];
}

【问题讨论】:

  • 什么是崩溃堆栈?
  • 返回thread 1 exc_bad_access
  • 你需要在调试器中运行它。当它崩溃时,在调试器控制台中输入 bt 并将输出粘贴到此处。
  • 作为旁注......您使用[tempDict setValue:@"user" forKey:@"UserName"];而不是tempDict[@"user"] = @"UserName"的任何原因?
  • @AshleyMills 我认为是一样的,不是吗?

标签: ios objective-c twitter


【解决方案1】:

您的推特网址不正确。应该是

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

【讨论】:

    【解决方案2】:

    这就是问题所在...
    1. 首先,由于新的 Twitter API 版本 1.1,正确的 URL 是:https://api.twitter.com/1.1/friendships/create.json
    2. 用户名的'for key'值必须是“screen_name”,如下片段:

    NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] init];
    [tempDict setValue:@"UserNameOnTwitter" forKey:@"screen_name"];
    [tempDict setValue:@"true" forKey:@"follow"];
    
    1. “requestForServiceType”的值必须是 SLServiceTypeTwitter,如下所示:

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

    所以整个区块将是:

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

    还是谢谢大家。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-06
      • 2011-05-17
      • 1970-01-01
      • 1970-01-01
      • 2015-11-12
      • 2016-06-15
      • 1970-01-01
      相关资源
      最近更新 更多