iOS NSURLConnection基础网络请求封装

来源:互联网 发布:从mac导入iphone的照片 编辑:程序博客网 时间:2024/04/27 19:16

现在NSURLConnection在开发中会使用的越来越少,iOS9已经将NSURLConnection废弃,现在最低版本一般适配iOS7,所以也可以使用。

用法:

FSKRequest *request = [[FSKRequest alloc]initWithUrl:@"http://www.cnblogs.com/kakaluote123/articles/5426923.html" withResponseBlock:^(BOOL isFinsh, id result, NSString *type) {        if (isFinsh == YES) {            NSLog(@"请求成功");        }else{            NSLog(@"请求失败");        }        NSLog(@"%@",type);        NSLog(@"%@",result);    }];

这里写图片描述

内容:

////  FSKRequest.h//  FSKNetWorking////  Created by 冯士魁 on 2017/2/20.//  Copyright © 2017年 xoxo_x. All rights reserved.//#import <Foundation/Foundation.h>#import <UIKit/UIKit.h>typedef void(^FSKRequestBlock)(BOOL isFinsh,id result, NSString *type);@interface FSKRequest : NSObject<NSURLConnectionDataDelegate>@property (nonatomic,copy) FSKRequestBlock responseBlock;/** * NSURLConnection 请求对象 */@property (nonatomic,strong) NSURLConnection *conn;/** *  可变数据容器(接收到数据的容器) */@property (nonatomic,strong) NSMutableData *mData;/** *  返回的数据的格式 */@property (nonatomic,strong) NSArray *array;@property (nonatomic,strong) NSDictionary *dic;@property (nonatomic,strong) UIImage *image;//数据的格式 1:array 2:dic 3:image@property (nonatomic,copy) NSString *type;- (instancetype)initWithUrl:(NSString *)url  withResponseBlock:(FSKRequestBlock)block;@end

——————————————————————————

////  FSKRequest.m//  FSKNetWorking////  Created by 冯士魁 on 2017/2/20.//  Copyright © 2017年 xoxo_x. All rights reserved.//#import "FSKRequest.h"@implementation FSKRequest- (instancetype)initWithUrl:(NSString *)url  withResponseBlock:(FSKRequestBlock)block{    self = [super init];    if (self) {        //        _responseBlock = block;        _mData = [NSMutableData dataWithCapacity:0];        NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];        _conn = [NSURLConnection connectionWithRequest:req delegate:self];    }    return self;}#pragma mark - NSURLConnectionDataDelegate- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{    //请求失败    if (_responseBlock) {        _responseBlock(NO,error,_type);    }}- (void)connectionDidFinishLoading:(NSURLConnection *)connection{    //处理数据    [self jsonData];}#pragma mark - jsonData- (void)jsonData{    NSError *error = nil;    id jsonData = [NSJSONSerialization JSONObjectWithData:_mData options:NSJSONReadingMutableContainers error:&error];    NSLog(@"%@,%@",jsonData,error);    if(error != nil){        _type = @"0";        _responseBlock(NO,error,_type);    }else if ([jsonData isKindOfClass:[NSArray class]]) {        _type = @"1";        _array = jsonData;        if (_responseBlock) {            _responseBlock(YES,jsonData,_type);        }    }else if ([jsonData isKindOfClass:[NSDictionary class]]){        _type = @"2";        _dic = jsonData;        if (_responseBlock) {            _responseBlock(YES,jsonData,_type);        }    }else{        _type = @"3";        _image = [UIImage imageWithData:_mData];        if (_responseBlock) {            _responseBlock(YES,_image,_type);        }    }}- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{    [_mData appendData:data];}- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{}@end
0 0
原创粉丝点击