이 코드를 사용하여 내 앱에서 사용자 트위터 계정으로 트윗을 보냅니다. 트위터 기기 설정에서 하나 이상의 계정이 선택 될 때까지 아무런 문제가 없습니다. 설정에서 모든 계정을 삭제하면 내 트윗이 실패합니다."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를 매개 변수로 사용하여 로그인 요청을하는 것입니다. 이것을 할 수있는 방법이 있습니까? 아니면 더 좋은 방법이 있습니까?