今天写的是一个在微博上发文章的代码,封装了一个简单的方法,必须发图片和文字的一个接口。
1.在微博上注册一个开发者账号,点击API接口。
2.这里面有很多的接口,今天只讲一个。就是微博接口中的写入接口 statuses/upload 上传图片并发布一条微博。
3.点击文档下面的API文档后面的“接口测试工具”,获取一个 Access Token:一个身份证明 这是一个关键的KEY。
好了开始代码部分:
1.创建一个类
创建方法:
#import <Foundation/Foundation.h>
//block 传值,可以把自己想要的东西传回去
typedef void(^completeBlock)(NSData *_Nullable data,NSURLResponse * _Nullable response,NSError *_Nullable error);
@interface ManagerWeibo : NSObject
//上传图片和文字的方法
-(void)postUploadWeiboApi:(NSString *_Nullable)urlApi withContent:(NSString *_Nullable)content withImage:(NSString *_Nullable)image complete:(_Nullable completeBlock)block;
@end
2.方法实现
//
// ManagerWeibo.m
// UpLoadWeibo-demo
//
// Created by mac on 16/8/12.
// Copyright © 2016年 mac. All rights reserved.
//
#import "ManagerWeibo.h"
//获取Access_Toke,这里只是一个样板,并不能用
#define Access_Token @"2.006Z_qTFYCZGtBa09d247a83AzdrWDD"
//一个标识,可自己修改
#define boundary @"sentWeiboID"
@implementation ManagerWeibo
-(void) postUploadWeiboApi:(NSString *)urlApi withContent:(NSString *)content withImage:(NSString *)image complete:(completeBlock)block{
//异步线程中调用
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSURL *url = [NSURL URLWithString:urlApi];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
//请求方式
request.HTTPMethod = @"POST";
//请求头
[request setValue:[NSString stringWithFormat:@"multipart/form-data; charset=utf-8;boundary=%@",boundary] forHTTPHeaderField:@"Content-Type"];
//请求体
NSString *path = [[NSBundle mainBundle] pathForResource:image ofType:nil];
NSData *acceptData =[self buildBodyData:content withImagePath:path];
NSURLSession *session = [NSURLSession sharedSession];
//上传数据任务
NSURLSessionUploadTask *task= [session uploadTaskWithRequest:request fromData:acceptData completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
//回调回去
block(data,response,error);
}];
[task resume];
});
}
//请求体方法:
- (NSData *)buildBodyData:(NSString *)content withImagePath:(NSString *)path{
NSMutableString *bodyStr = [NSMutableString string];
/*
上传数据的格式:
--AaB03x
Content-Disposition: form-data; name="key1"
value1
--AaB03x
Content-disposition: form-data; name="key2"
value2
--AaB03x
Content-disposition: form-data; name="key3"; filename="file"
Content-Type: application/octet-stream
图片数据...
--AaB03x--
*/
//PS:谨遵格式。这里我只写了几个必须要填的KEY
//1.access_token
[bodyStr appendFormat:@"--%@\r\n",boundary]; // \r回到行首 \n换行
[bodyStr appendFormat:@"Content-Disposition: form-data; name=\"access_token\"\r\n\r\n"];
[bodyStr appendFormat:@"%@\r\n",Access_Token];
//2.status
[bodyStr appendFormat:@"--%@\r\n",boundary];
[bodyStr appendFormat:@"Content-Disposition: form-data; name=\"status\"\r\n\r\n"];
[bodyStr appendFormat:@"%@\r\n",content];
//3.pic
[bodyStr appendFormat:@"--%@\r\n",boundary];
[bodyStr appendFormat:@"Content-Disposition: form-data; name=\"pic\";filename=\"file\"\r\n"];
[bodyStr appendFormat:@"Content-Type: application/octet-stream\r\n\r\n"];
NSMutableData *bodyData = [NSMutableData data];
//(1)startData
NSData *startData = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
[bodyData appendData:startData];
//(2)pic
NSData *picdata =[NSData dataWithContentsOfFile:path];
[bodyData appendData:picdata];
//(3)--Str--
NSString *endStr = [NSString stringWithFormat:@"\r\n--%@--\r\n",boundary];
NSData *endData = [endStr dataUsingEncoding:NSUTF8StringEncoding];
[bodyData appendData:endData];
return bodyData;
}
@end
调用方法:
//
// ViewController.m
// UpLoadWeibo-demo
//
// Created by mac on 16/8/12.
// Copyright © 2016年 mac. All rights reserved.
//
#import "ViewController.h"
#import "ManagerWeibo.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
ManagerWeibo *manager = [[ManagerWeibo alloc] init];
//获取API接口,发表的内容和图片
[manager postUploadWeiboApi:@"https://api.weibo.com/2/statuses/upload.json" withContent:@"我是一个大大的神秘的有趣的码农" withImage:@"刘诗诗.jpg" complete:^(NSData *data, NSURLResponse *response, NSError *error) {
NSLog(@"%@",response);
}];
}
@end
最后在你的的微博上就能看到你写的文章了!!