[iOS微博项目-3.2]-发送微博

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

github:https://github.com/hellovoidworld/HVWWeibo

A.使用微博API发送微博

1.需求

学习发送微博API

发送文字微博

发送带有图片的微博

2.思路

直接按照微博API的文档指示使用

这里测试上传带图片微博

3.实现

在“发微博”界面,点击右上角发送就调用API

1//HVWComposeViewController.m2/** 发送微博*/3- (void) sendWeibo {4if(self.composeView.text.length ==0) {5[MBPRogressHUD showError:@"你好像忘记了内容..."];6return;7}89[MBProgressHUD showMessage:@"发送微博中..."];1011if(self.imageDisplayView.images.count) {//发送的时带图片的微博12[self sendWeiboWithTextNImage];13}else{//发送的是纯文字微博14[self sendWeiboWithText];15}16}1718/** 发送文字微博*/19- (void) sendWeiboWithText {20//创建http操作管理者21AFHTTPRequestOperationManager *manager =[AFHTTPRequestOperationManager manager];2223//设置参数24NSMutableDictionary *param =[NSMutableDictionary dictionary];25//访问令牌26HVWAccountInfo *accountInfo =[HVWAccountInfoTool accountInfo];27param[@"access_token"] =accountInfo.access_token;28//微博文本29param[@"status"] =self.composeView.text;3031//发送请求32[manager POST:@"https://api.weibo.com/2/statuses/update.json"parameters:param success:^(AFHTTPRequestOperation *operation,idresponSEObject) {33[MBProgressHUD hideHUD];34[MBProgressHUD showSuccess:@"发送成功!"];35} failure:^(AFHTTPRequestOperation *operation, NSError *error) {36HVWLog(@"发送微博失败, error:%@", error);37[MBProgressHUD hideHUD];38[MBProgressHUD showError:@"发送失败!error"];39}];40}4142/** 发送图文微博*/43- (void) sendWeiboWithTextNImage {44if(self.imageDisplayView.images.count ==0) {45[MBProgressHUD hideHUD];46[MBProgressHUD showError:@"懵了,找不到图儿!"];47return;48}4950//创建http操作管理者51AFHTTPRequestOperationManager *manager =[AFHTTPRequestOperationManager manager];5253//设置参数54NSMutableDictionary *param =[NSMutableDictionary dictionary];55//访问令牌56HVWAccountInfo *accountInfo =[HVWAccountInfoTool accountInfo];57param[@"access_token"] =accountInfo.access_token;58//微博文本59param[@"status"] =self.composeView.text;6061//发送请求62[manager POST:@"https://upload.api.weibo.com/2/statuses/upload.json"parameters:param constructingBodyWithBlock:^(id<AFMultipartFormData>formData) {6364//发送的图片数据,其实现在开放的API只允许上传一张图片65UIImage *image =[self.imageDisplayView.images firstObject];66if(image) {67NSData *imageData =UIImagePNGRepresentation(image);68[formData appendPartWithFileData:imageData name:@"pic"fileName:@"statusPic"mimeType:@"image/png"];69}70} success:^(AFHTTPRequestOperation *operation,idresponseObject) {71[MBProgressHUD hideHUD];72[MBProgressHUD showSuccess:@"发送成功!"];73} failure:^(AFHTTPRequestOperation *operation, NSError *error) {74HVWLog(@"发送微博失败, error:%@", error);75[MBProgressHUD hideHUD];76[MBProgressHUD showError:@"发送失败!"];77}];78}

发送成功:

#mark:

发送微博失败的原因有:

1.没有文本内容

2.使用发送纯文本API,却上传图片

3.使用发送图文API,却没有上传图片

注意:新浪开放的API一次只能上传一张图片,选择多张的时候会使用最后一张

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航