2013-09-06 1 views
0

이 코드를 사용하여 내 앱에서 사용자 트위터 계정으로 트윗을 보냅니다. 트위터 기기 설정에서 하나 이상의 계정이 선택 될 때까지 아무런 문제가 없습니다. 설정에서 모든 계정을 삭제하면 내 트윗이 실패합니다."iPhone 설정"에 계정이 설정되지 않은 채로 보내기 Tweet

ACAccountStore *accountStore=[[ACAccountStore alloc] init]; 
    ACAccountType *twitterType = 
    [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 = [accountStore accountsWithAccountType:twitterType]; 
     NSURL *url = [NSURL URLWithString:@"https://api.twitter.com" 
         @"/1.1/statuses/update_with_media.json"]; 
     NSDictionary *params = @{@"status" : @"status"}; 
     SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeTwitter 
               requestMethod:SLRequestMethodPOST 
                  URL:url 
                parameters:params]; 
     NSData *imageData = UIImageJPEGRepresentation(Photo, 1.f); 
     [request addMultipartData:imageData 
          withName:@"media[]" 
           type:@"image/jpeg" 
          filename:@"image.jpg"]; 
     [request setAccount:[accounts lastObject]]; 
     [request performRequestWithHandler:requestHandler]; 
     } 
     else { 
     NSLog(@"[ERROR] An error occurred while asking for user authorization: %@", 
       [error localizedDescription]); 
     } 
    }; 

    [accountStore requestAccessToAccountsWithType:twitterType 
               options:NULL 
              completion:accountStoreHandler]; 

트위터 기기 설정에 계정이없는 경우 트윗을 보낼 수있는 방법이 있습니까? 어쩌면 완벽한 해결책은 API를 호출하고 username과 password를 매개 변수로 사용하여 로그인 요청을하는 것입니다. 이것을 할 수있는 방법이 있습니까? 아니면 더 좋은 방법이 있습니까?

답변

0

당신이하려는 것은 당신이 계정 프레임 워크를 사용하여 트윗을 보내려고한다는 것입니다. 일반 트위터 API를 사용하여 트윗을 전송하는 코드를 작성하지 않았습니다.

계정 프레임 워크를 사용하여 트윗을 보낼 수있는 경우 먼저 한 가지를 확인하십시오. 다음 정상의 OAuth 인증

를 진행하지 않을 경우이 조건의 OAuth API를 사용하여 다음 포스트 달콤한 실패 할 경우

if ([TWTweetComposeViewController canSendTweet]) 
{ 
    TWTweetComposeViewController *tweetSheet = 
     [[TWTweetComposeViewController alloc] init]; 
    [tweetSheet setInitialText:@"Initial Tweet Text!"]; 
    [self presentModalViewController:tweetSheet animated:YES]; 
} 

를 사용하여 트위터 계정의 유효성을 검사 할 수 있습니다. 가장 간단한 방법은이 컨트롤을 사용하는 것입니다.

https://github.com/malcommac/DMTwitterOAuth

관련 문제