美文网首页
获得&发布微博

获得&发布微博

作者: nothing_c | 来源:发表于2016-10-28 01:44 被阅读18次

    #import"ViewController.h"

    #import"AFNetworking.h"

    @interface ViewController()

    @property (nonatomic,strong) UITextField *textFiled;

    @property (nonatomic,strong) UIButton *button;

    @property (nonatomic,strong) UIImageView *imagaView;

    @property (nonatomic,strong) UIProgressView *progressView;

    @end

    @implementation ViewController

    #pragma mark --- Life Cycle

    - (void)viewDidLoad {

    [super viewDidLoad];

    self.view.backgroundColor= [UIColor whiteColor];

    //懒加载。set调用

    [self.view addSubview:self.textFiled];

    [self.view addSubview:self.button];

    [self.view addSubview:self.imagaView];

    [self.viewa ddSubview:self.progressView];

    }

    #pragma mark --- setUI(get)

    - (UITextField *)textFiled {

    //判断存在与否防止run一次创建一次

    if(!_textFiled) {

    _textFiled= [[UITextField alloc] initWithFrame:CGRectMake(20, 10, [[UIScreen mainScreen] bounds].size.width- 40, 50)];

    _textFiled.backgroundColor= [UIColor greenColor];

    _textFiled.alpha= 0.5;

    _textFiled.placeholder=@"请输入发布内容";

    }

    return _textFiled;

    }

    - (UIButton *)button {

    if(!_button) {

    _button= [[UIButton alloc] initWithFrame:CGRectMake([[UIScreen mainScreen] bounds].size.width/2 - 30, 550, 60, 40)];

    _button.backgroundColor= [UIColor blueColor];

    _button.alpha= 0.5;

    [_button setTitle:@"发布" forState:UIControlStateNormal];

    [_button addTarget:selfaction:@selector(sendAction) forControlEvents:UIControlEventTouchUpInside];

    }

    return _button;

    }

    - (UIImageView *)imagaView {

    if(!_imagaView) {

    _imagaView= [[UIImageView alloc] initWithFrame:CGRectMake(20, 80, [[UIScreen mainScreen] bounds].size.width- 40, 400)];

    _imagaView.backgroundColor= [UIColor redColor];

    _imagaView.alpha= 0.5;

    _imagaView.image= [UIImage imageNamed:@"13"];

    }

    return _imagaView;

    }

    - (UIProgressView *) progressView {

    if(!_progressView) {

    _progressView= [[UIProgressView alloc] initWithFrame:CGRectMake(20, 530, [[UIScreen mainScreen]bounds].size.width- 40, 20)];

    _progressView.tintColor= [UIColor orangeColor];

    _progressView.progress= 0.0;

    }

    return _progressView;

    }

    #pragma mark --- Button Action

    - (void)sendAction {

    //[self getSelfWeiboMessage];

    //[self sendOneMessageToWeibo];

    [self sendOnePictureToWeibo];

    }

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent*)event {

    [self.view endEditing:YES];

    }

    - (void)sendOnePictureToWeibo {

    //获得网络管理

    AFHTTPSessionManager *manage = [AFHTTPSessionManager manager];

    //取出之前存储的access_token

    NSString *token = [[NSUserDefaults standardUserDefaults] objectForKey:@"access_token"];

    //路径

    NSString *url =@"https://upload.api.weibo.com/2/statuses/upload.json";

    //封装参数

    NSDictionary *parma = @{@"access_token":token,@"status":_textFiled.text};

    //连接上传数据必须用constructingBodyWithBlock

    [manage POST:url parameters:parma constructingBodyWithBlock:^(id_NonnullformData) {

    //pic:系统名字@"name.JPEG":.JPEG图片格式@"image/jpeg":图片格式/jpeg

    //[formData appendPartWithFileData:UIImageJPEGRepresentation(self.imagaView.image, .1) name:@"pic" fileName:@"name.JPEG" mimeType:@"image/jpeg"];

    //png格式的图片上传会更慢

    [formData appendPartWithFileData:UIImagePNGRepresentation(self.imagaView.image) name:@"pic" fileName:@" name.PNG" mimeType:@"image/png"];

    //视屏上传失败提示请求时间过长

    //资源束

    //NSString *filePath = [[NSBundle mainBundle] pathForResource:@"ok" ofType:@"mov"];

    //转化为二进制流

    //NSData *data = [NSData dataWithContentsOfFile:filePath];

    //上传

    //[formData appendPartWithFileData:data name:@"mov" fileName:@"ok.mov" mimeType:@"ok/mov"];

    }progress:^(NSProgress*_NonnulluploadProgress) {

    //进度条//completedUnitCount已经下载的字节数totalUnitCount:总共要下载的字节数

    NSLog(@"progress ---> %f",(float)uploadProgress.completedUnitCount/uploadProgress.totalUnitCount);

    dispatch_async(dispatch_get_main_queue(), ^{

    self.progressView.progress= (float)uploadProgress.completedUnitCount / uploadProgress.totalUnitCount;

    });

    }success:^(NSURLSessionDataTask*_Nonnulltask,id_NullableresponseObject) {

    NSLog(@"responseObject ---> %@",responseObject);

    }failure:^(NSURLSessionDataTask *_Nullabletask,NSError *_Nonnullerror) {

    NSLog(@"error ---> %@",error);

    }];

    }

    - (void)sendOneMessageTOWeibo {

    AFHTTPSessionManager *manage = [AFHTTPSessionManager manager];

    //取出之前存储的access_token

    NSString *token = [[NSUserDefaults standardUserDefaults] objectForKey:@"access_token"];

    NSString *url =@"https://api.weibo.com/2/statuses/update.json";

    NSDictionary *parma = @{@"access_token":token, @"status":_textFiled.text};

    [manage POST:url parameters:parma progress:^(NSProgress *_NonnulluploadProgress) {

    NSLog(@"progress ---> %f",(float)uploadProgress.completedUnitCount / uploadProgress.totalUnitCount);

    dispatch_async(dispatch_get_main_queue(), ^{

    self.progressView.progress= (float)uploadProgress.completedUnitCount / uploadProgress.totalUnitCount;

    });

    }success:^(NSURLSessionDataTask*_Nonnulltask,id_NullableresponseObject) {

    NSLog(@"responseObject ---> %@",responseObject);

    }failure:^(NSURLSessionDataTask *_Nullabletask,NSError *_Nonnullerror) {

    NSLog(@"error ---> %@",error);

    }];

    }

    - (void)getSelfWeiboMessage {

    AFHTTPSessionManager *manage = [AFHTTPSessionManager manager];

    //取出之前存储的access_token

    NSString *token = [[NSUserDefaultsstandardUserDefaults]objectForKey:@"access_token"];

    NSString *url =@"https://api.weibo.com/2/statuses/friends_timeline.json";

    NSDictionary *parma = @{@"access_token":token};

    [manage GET:urlparameters:parmaprogress:^(NSProgress*_NonnulldownloadProgress) {

    NSLog(@"progress ---> %f",(float)downloadProgress.completedUnitCount / downloadProgress.totalUnitCount);

    dispatch_async(dispatch_get_main_queue(), ^{

    self.progressView.progress= (float)downloadProgress.completedUnitCount / downloadProgress.totalUnitCount;

    });

    }success:^(NSURLSessionDataTask*_Nonnulltask,id_NullableresponseObject) {

    NSLog(@"responseObject ---> %@",responseObject);

    }failure:^(NSURLSessionDataTask*_Nullabletask,NSError*_Nonnullerror) {

    NSLog(@"error ---> %@",error);

    }];

    }

    相关文章

      网友评论

          本文标题:获得&发布微博

          本文链接:https://www.haomeiwen.com/subject/ejwsuttx.html