我正在使用此代码将推文从我的应用程序发送到用户 Twitter 帐户。在 Twitter 设备设置中至少选择一个帐户之前,我没有问题。如果我从设置中删除所有帐户,我的推文将失败。
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 并使用用户名和密码作为参数进行登录请求。有办法做到这一点吗?还是有更好的方法?