2013-06-05 3 views
1

Я пытаюсь реализовать «Follow Us on Twitter» в моем приложении iOS. Вот мой код. Но он дает ошибку «Не удалось подготовить URL-запрос». Пожалуйста помоги!Twitter SLRequest performRequestWithHandler - Не удалось подготовить URL-запрос

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

ответ

4

шагов То есть, как я сделал это!

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 
     } 
    }]; 
+0

я заменил свой код с твоим и по-прежнему получать тот же :( – a4arpan

+0

работал. Я не использовал ACAccounts правильно – a4arpan

+0

@ a4arpan это то, что я думал, но я был ленив, чтобы прочитать код :) –