国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

[iOS微博項目-3.2]-發送微博

2019-11-14 19:33:24
字體:
來源:轉載
供稿:網友
 
A.使用微博API發送微博
1.需求
學習發送微博API
發送文字微博
發送帶有圖片的微博
 
2.思路
直接按照微博API的文檔指示使用
Image(120)
 
這里測試上傳帶圖片微博
Image(121)
 
3.實現
在“發微博”界面,點擊右上角發送就調用API
 
Image(122)
 
 1 //  HVWComposeViewController.m 2 /** 發送微博 */ 3 - (void) sendWeibo { 4     if (self.composeView.text.length == 0) { 5         [MBPRogressHUD showError:@"你好像忘記了內容..."]; 6         return; 7     } 8     9     [MBProgressHUD showMessage:@"發送微博中..."];10    11     if (self.imageDisplayView.images.count) { // 發送的時帶圖片的微博12         [self sendWeiboWithTextNImage];13     } else { // 發送的是純文字微博14         [self sendWeiboWithText];15     }16 }17 18 /** 發送文字微博 */19 - (void) sendWeiboWithText {20     // 創建http操作管理者21     AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];22    23     // 設置參數24     NSMutableDictionary *param = [NSMutableDictionary dictionary];25     // 訪問令牌26     HVWAccountInfo *accountInfo = [HVWAccountInfoTool accountInfo];27     param[@"access_token"] = accountInfo.access_token;28     // 微博文本29     param[@"status"] = self.composeView.text;30    31     // 發送請求32     [manager POST:@"https://api.weibo.com/2/statuses/update.json" parameters:param success:^(AFHTTPRequestOperation *operation, id responSEObject) {33         [MBProgressHUD hideHUD];34         [MBProgressHUD showSuccess:@"發送成功!"];35     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {36         HVWLog(@"發送微博失敗, error:%@", error);37         [MBProgressHUD hideHUD];38         [MBProgressHUD showError:@"發送失敗!error"];39     }];40 }41 42 /** 發送圖文微博 */43 - (void) sendWeiboWithTextNImage {44     if (self.imageDisplayView.images.count == 0) {45         [MBProgressHUD hideHUD];46         [MBProgressHUD showError:@"懵了,找不到圖兒!"];47         return;48     }49    50     // 創建http操作管理者51     AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];52    53     // 設置參數54     NSMutableDictionary *param = [NSMutableDictionary dictionary];55     // 訪問令牌56     HVWAccountInfo *accountInfo = [HVWAccountInfoTool accountInfo];57     param[@"access_token"] = accountInfo.access_token;58     // 微博文本59     param[@"status"] = self.composeView.text;60    61     // 發送請求62     [manager POST:@"https://upload.api.weibo.com/2/statuses/upload.json" parameters:param constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {63        64         // 發送的圖片數據,其實現在開放的API只允許上傳一張圖片65         UIImage *image  = [self.imageDisplayView.images firstObject];66         if (image) {67             NSData *imageData = UIImagePNGRepresentation(image);68             [formData appendPartWithFileData:imageData name:@"pic" fileName:@"statusPic" mimeType:@"image/png"];69         }70     } success:^(AFHTTPRequestOperation *operation, id responseObject) {71         [MBProgressHUD hideHUD];72         [MBProgressHUD showSuccess:@"發送成功!"];73     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {74         HVWLog(@"發送微博失敗, error:%@", error);75         [MBProgressHUD hideHUD];76         [MBProgressHUD showError:@"發送失敗!"];77     }];78 }

 

 
發送成功:
Image(123)
 
 
#mark:
發送微博失敗的原因有:
1.沒有文本內容
2.使用發送純文本API,卻上傳圖片
3.使用發送圖文API,卻沒有上傳圖片
 
注意:新浪開放的API一次只能上傳一張圖片,選擇多張的時候會使用最后一張
 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 上杭县| 咸丰县| 密云县| 桂林市| 大渡口区| 隆回县| 宁远县| 孟连| 汤原县| 枝江市| 枣庄市| 尼木县| 仲巴县| 汝城县| 平顺县| 霍城县| 五莲县| 广昌县| 鹿泉市| 剑川县| 鹤山市| 江津市| 祥云县| 临潭县| 礼泉县| 丰镇市| 年辖:市辖区| 雷州市| 左贡县| 刚察县| 印江| 成都市| 泰和县| 城步| 潍坊市| 三门峡市| 萝北县| 临沂市| 凌海市| 石景山区| 新竹县|