Yog*_*ton 1 twitter ios twrequest acaccount
我正在尝试使用TWRequest对象发布图像.这是我的代码:
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
NSData *pngImage = UIImagePNGRepresentation(image);
NSData *tweetText = [userText copy];
[accountStore requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) {
if (granted) {
NSLog(@"Twitter is good to go");
NSArray *twitterAccounts = [accountStore accountsWithAccountType:accountType];
if (twitterAccounts.count) {
ACAccount *twitterAccount = [twitterAccounts objectAtIndex:0];
NSURL *url = [NSURL URLWithString:@"https://api.twitter.com/1.1/statuses/update_with_media.json"];
TWRequest *request = [[TWRequest alloc] initWithURL:url parameters:nil requestMethod:TWRequestMethodPOST];
[request addMultiPartData:pngImage withName:@"media" type:@"image/png"];
[request addMultiPartData:tweetText withName:@"text" type:@"text/plain"];
[request setAccount:twitterAccount];
[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
if (responseData)
{
NSError *error = nil;
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&error];
if (jsonArray) {
for(NSDictionary *item in jsonArray) {
NSLog(@"Item: %@", item);
}
}
else
{
NSLog(@"Error %@ with user info %@.", error, error.userInfo);
}
}
}];
}
} else {
NSLog(@"The user does not grant us permission to access its Twitter account(s).");
}
}];
Run Code Online (Sandbox Code Playgroud)
每当程序到达该行时,[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
我都会收到以下错误:
[__NSCFString字节]:无法识别的选择发送到实例0x1d8f4250 *终止应用程序由于未捕获的异常'NSInvalidArgumentException',原因: ' - [__ NSCFString字节]:无法识别的选择发送到实例0x1d8f4250'*第一掷调用堆栈:(0x33ec02a3 0x3bb5a97f 0x33ec3e07 0x33ec2531 0x33e19f68 0x3473b90b 0x35c416fd 0x35c417a3 0x35c4065f 0x35c3fa6b 0x35c40ab9 0x5adc5 0x3bf7211f 0x3bf7fdcb 0x3bf80259 0x3bf803b9 0x3bfa6a11 0x3bfa68a4)libc ++ abi.dylib:terminate调用抛出异常(lldb)
问题在于tweetText
.您正在复制,NSString
但将其分配给NSData
对象.
改变这个:
NSData *tweetText = [userText copy];
Run Code Online (Sandbox Code Playgroud)
对此:
NSData *tweetText = [userText dataUsingEncoding:NSUTF8StringEncoding];
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
6486 次 |
最近记录: |