• Stars
    star
    470
  • Rank 93,399 (Top 2 %)
  • Language
    Objective-C
  • License
    MIT License
  • Created over 9 years ago
  • Updated over 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

WHCNetWorkKit 是http网络请求开源库(支持GET/POST 文件上传 后台文件下载 UIButton UIImageView 控件设置网络图片 网络数据工具json/xml 转模型类对象 网络状态监听)

WHCNetWorkKit 网络操作开源库

###咨询QQ:712641411
###开发作者:吴海超

###目前封装最好使用最简单的文件下载(支持后台下载)iOS网络开源库 该版本进行了增强包括如下功能: ###GET/POST网络请求/多文件上传/后台文件下载/网络状态监控/UIButton,UIImageView 设置网络图片等功能模块。 ###封装网络常用工具类json/xml 转模型类对象 json/xml 解析 ###文件下载模块代理可以自由替换代理或者回调块具体详情请参看自带demo

###安装集成方式

platform :ios, '7.0'
pod "WHCNetWorkKit", "~> 0.0.3"

##运行效果

###网络状态监听 Use Example

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
    [[WHC_HttpManager shared] registerNetworkStatusMoniterEvent];
    return YES;
}

/// 在其他地方可以这样[WHC_HttpManager shared].networkStatus获取当前网络状态
   (该网络库每次进行请求都自动处理了网络状态不需要用户进行判断)
switch ([WHC_HttpManager shared].networkStatus) {
    case NotReachable:{
        [[[UIAlertView alloc]initWithTitle:nil
        message:@"当前网络不可用请检查网络设置"
        delegate:nil cancelButtonTitle:@"确定"
        otherButtonTitles:nil, nil] show];
    }
    break;
    case ReachableViaWiFi:
        NSLog(@"====当前网络状态为Wifi=======");
    break;
    case ReachableViaWWAN:
        NSLog(@"====当前网络状态为3G=======");
    break;
}

###GET请求 Use Example

[[WHC_HttpManager shared] get:@"http://www.baidu.com/"
                  didFinished:^(WHC_BaseOperation *operation,
                                                NSData *data,
                                                NSError *error,
                                                BOOL isSuccess) {
     //处理data数据
}];

###POST 请求 Use Example

[[WHC_HttpManager shared] post:@"http://www.baidu.com"
                         param:@"whc"
                   didFinished:^(WHC_BaseOperation *operation,
                                                 NSData *data, 
                                               NSError *error,
                                               BOOL isSuccess) {
        //处理data数据
}];

###多文件上传 Use Example

//上传5个文件
for (int i = 0; i < 5; i++) {
    UIImage * image = [UIImage imageNamed:@"whc"];
    NSData * imageData = UIImageJPEGRepresentation(image, 0.5);
    [[WHC_HttpManager shared] addUploadFileData:imageData withFileName:[NSString stringWithFormat:@"image%d",i] mimeType:@"image/jpep" forKey:@"file"];
    //最后一个参数key必须和服务端对应
}
[[WHC_HttpManager shared] upload:@"http://www.baidu.com"
                           param:@"param" didFinished:^(WHC_BaseOperation *operation,
                                                                            NSData *data,
                                                                            NSError *error,
                                                                            BOOL isSuccess) {
        //处理上传结果数据
}];

###普通文件下载 Use Example

WHC_DownloadOperation * downloadTask = nil;
downloadTask = [[WHC_HttpManager shared] download:kWHC_DefaultDownloadUrl
                                         savePath:[WHC_DownloadObject videoDirectory]
                                     saveFileName:fileName
                                         response:^(WHC_BaseOperation *operation, NSError *error, BOOL isOK) {
                                        if (isOK) {
                                            [weakSelf.view toast:@"已经添加到下载队列"];
                                            WHC_DownloadOperation * downloadOperation = (WHC_DownloadOperation*)operation;
                                            WHC_DownloadObject * downloadObject = [WHC_DownloadObject new];
                                            downloadObject.fileName = downloadOperation.saveFileName;
                                            downloadObject.downloadPath = downloadOperation.strUrl;
                                            downloadObject.downloadState = WHCDownloading;
                                            downloadObject.currentDownloadLenght = downloadOperation.recvDataLenght;
                                            downloadObject.totalLenght = downloadOperation.fileTotalLenght;
                                            [downloadObject writeDiskCache];
                                        }else {
                                            [weakSelf.view toast:error.userInfo[NSLocalizedDescriptionKey]];
                                        }
                                    } process:^(WHC_BaseOperation *operation, uint64_t recvLength, uint64_t totalLength, NSString *speed) {
                                            NSLog(@"recvLength = %llu totalLength = %llu speed = %@",recvLength , totalLength , speed);
                                    } didFinished:^(WHC_BaseOperation *operation, NSData *data, NSError *error, BOOL isSuccess) {
                                        if (isSuccess) {
                                            [weakSelf.view toast:@"下载成功"];
                                            [weakSelf saveDownloadStateOperation:(WHC_DownloadOperation *)operation];
                                        }else {
                                            [weakSelf.view toast:error.userInfo[NSLocalizedDescriptionKey]];
                                        if (error != nil &&
                                            error.code == WHCCancelDownloadError) {
                                            [weakSelf saveDownloadStateOperation:(WHC_DownloadOperation *)operation];
                                        }
                                    }
}];

###后台文件下载 Use Example

[[WHC_SessionDownloadManager shared] setBundleIdentifier:@"com.WHC.WHCNetWorkKit.backgroundsession"];
WHC_DownloadSessionTask * downloadTask = [[WHC_SessionDownloadManager shared]
                                                                    download:kWHC_DefaultDownloadUrl
                                                                    savePath:[WHC_DownloadObject videoDirectory]
                                                                saveFileName:fileName
                                                                    response:^(WHC_BaseOperation *operation, NSError *error, BOOL isOK) {
                                                                    [weakSelf.view toast:@"已经添加到下载队列"];
                                                                    WHC_DownloadOperation * downloadOperation = (WHC_DownloadOperation*)operation;
                                                                    WHC_DownloadObject * downloadObject = [WHC_DownloadObject new];
                                                                    downloadObject.fileName = downloadOperation.saveFileName;
                                                                    downloadObject.downloadPath = downloadOperation.strUrl;
                                                                    downloadObject.downloadState = WHCDownloading;
                                                                    downloadObject.currentDownloadLenght = downloadOperation.recvDataLenght;
                                                                    downloadObject.totalLenght = downloadOperation.fileTotalLenght;
                                                                    [downloadObject writeDiskCache];
                                                                } process:^(WHC_BaseOperation *operation, uint64_t recvLength, uint64_t totalLength, NSString *speed) {
                                                                    NSLog(@"recvLength = %llu , totalLength = %llu , speed = %@",recvLength , totalLength , speed);
                                                                } didFinished:^(WHC_BaseOperation *operation, NSData *data, NSError *error, BOOL isSuccess) {
                                                                    if (isSuccess) {
                                                                        [weakSelf.view toast:@"下载成功"];
                                                                        [weakSelf saveDownloadStateOperation:(WHC_DownloadOperation *)operation];
                                                                    }else {
                                                                        [weakSelf.view toast:error.userInfo[NSLocalizedDescriptionKey]];
                                                                        if (error != nil &&
                                                                            error.code == WHCCancelDownloadError) {
                                                                            [weakSelf saveDownloadStateOperation:(WHC_DownloadOperation *)operation];
                                                                        }
                                                                    }
}];

###UIButton设置网络图片 Use Example

UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
[button whc_setBackgroundImageWithURL:@"http://www.baidu.com" forState:UIControlStateNormal];
[button whc_setBackgroundImageWithURL:@"http://www.baidu.com" forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"whc"]];
[button whc_setImageWithUrl:@"http://www.baidu.com" forState:UIControlStateNormal];
[button whc_setImageWithUrl:@"http://www.baidu.com" forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"whc"]];

####UIImageView设置网络图片 Use Example

UIImageView * imageView = [UIImageView new];
[imageView whc_setImageWithUrl:@"http://www.baidu.com"];
[imageView whc_setImageWithUrl:@"http://www.baidu.com" placeholderImage:[UIImage imageNamed:@"whc"]];

###Json 转模型类 Use Example ###支持无限json嵌套解析转换 (开源MAC工具WHC_DataModelFactory 自动把json或者xml字符串生成模型类.m和.h文件 ###省去手工创建模型类繁琐避免出错 开源链接:https://github.com/netyouli/WHC_DataModelFactory)

typedef enum {
    SexMale,
    SexFemale
} sex;

@interface User : NSObject
@property (copy, nonatomic) NSString *name;
@property (copy, nonatomic) NSString *icon;
@property (strong, nonatomic) NSNumber * age;
@property (strong, nonatomic) NSNumber * height;
@property (strong, nonatomic) NSNumber *money;
@property (strong, nonatomic) sex  *sex;
@end

/***********************************************/


NSDictionary *dict = @{
@"name" : @"Jack",
@"icon" : @"lufy.png",
@"age" : @20,
@"height" : @"1.55",
@"money" : @100.9,
@"sex" : @(SexFemale)
};
// JSON -> User
User * user = [WHC_DataModel dataModelWithDictionary:dict className:[User class]];

NSLog(@"name=%@, icon=%@, age=%@, height=%@, money=%@, sex=%@",
user.name, user.icon, user.age, user.height, user.money, user.sex);
// name=Jack, icon=lufy.png, age=20, height=1.550000, money=100.9, sex=1

###XML 转模型类 Use Example

NSString  * xml = @"<?xml version=\"1.0\" encoding=\"utf-8\"?>\
<ebMobileStartupInqRq >\
<REQHDR>\
<TrnNum>INHB2015042900000001</TrnNum>\
<TrnCode>1957747793</TrnCode>\
</REQHDR>\
<REQBDY>\
<OS>iPhone</OS>\
<App>CC</App>\
<IconVersion></IconVersion>\
</REQBDY>\
</ebMobileStartupInqRq>";

@interface REQBDY :NSObject
@property (nonatomic , copy) NSString              * OS;
@property (nonatomic , copy) NSString              * App;
@property (nonatomic , copy) NSString              * IconVersion;

@end

@interface REQHDR :NSObject
@property (nonatomic , copy) NSString              * TrnCode;
@property (nonatomic , copy) NSString              * TrnNum;

@end

@interface ebMobileStartupInqRq :NSObject
@property (nonatomic , strong) REQBDY              * REQBDY;
@property (nonatomic , strong) REQHDR              * REQHDR;

@end

@interface User :NSObject
@property (nonatomic , strong) ebMobileStartupInqRq              * ebMobileStartupInqRq;

@end


///上面模型类生成是用开源MAC工具WHC_DataModelFactory 自动生成 
///开源地址:https://github.com/netyouli/WHC_DataModelFactory
///首先用开源WHC_XMLParser 把xml转换为字典对象 然后用WHC_DataModel转模型对象
NSDictionary * xmlDictionary = [WHC_XMLParser dictionaryForXMLString:xml];
User * user = [WHC_DataModel dataModelWithDictionary:xmlDictionary
                                           className:[User class]];
NSLog(@"%@",ebMobile);

字典转换Json字符串 Use Example

NSDictionary * jsonDictionary = @{@"whc":@"吴海超"};
NSString * json = [WHC_Json jsonWithDictionary:jsonDictionary];

Json字符串/JsonData转NSDictionary对象 Use Example

NSString * json = @"{"whc":"吴海超"}";
NSDictionary * jsonDictionary = [WHC_Json dictionaryWithJson:json];
jsonDictionary = [WHC_Json dictionaryWithJsonData:[json
                dataUsingEncoding:NSUTF8StringEncoding]];

Json字符串/JsonData 转NSArray对象 Use Example

NSString * json = @""WHC":{{"android":"资深android开发者"} , {"iOS":"资深iOS开发者"}}";
NSArray * jsonArray = [WHC_Json arrayWithJson:json];
jsonArray = [WHC_Json arrayWithJsonData:[json
                    dataUsingEncoding:NSUTF8StringEncoding]];

NSDictionary/NSArray对象转 Xml字符串

NSDictionary * REQHDR = @{@"TrnNum":@"INHB2015042900000001",@"TrnCode":@"1957747793"};
NSDictionary * REQBDY = @{@"OS":@"iPhone",@"App":@"CC",@"IconVersion":@""};
NSDictionary * ebMobileStartupInqR = @{@"REQHDR":REQHDR,@"REQBDY":REQBDY};
NSDictionary * xmlDic = @{@"ebMobileStartupInqRq":ebMobileStartupInqR};
//use one
NSString  * xmlStringOne = [WHC_Xml xmlWithDictionary:xmlDic];
//use two
NSString  * xmlStringTwo = [WHC_Xml xmlWithDictionary:xmlDic 
            rootAttribute:@"xmlns = \"http://ns.chinatrust.com.tw/XSD/CTCB/ESB/Message/BSMF/ebMobileStartupInqRq/01\""];

NSLog(@"xmlStringOne = %@",xmlStringOne);
//xmlStringOne =     NSString  * xml = @"<?xml version=\"1.0\" encoding=\"utf-8\"?>\
<ebMobileStartupInqRq >\
<REQHDR>\
<TrnNum>INHB2015042900000001</TrnNum>\
<TrnCode>1957747793</TrnCode>\
</REQHDR>\
<REQBDY>\
<OS>iPhone</OS>\
<App>CC</App>\
<IconVersion></IconVersion>\
</REQBDY>\
</ebMobileStartupInqRq>";


NSLog(@"xmlStringTwo = %@",xmlStringTwo);
//xmlStringTwo =     NSString  * xml = @"<?xml version=\"1.0\" encoding=\"utf-8\"?>\
<ebMobileStartupInqRq xmlns=\"http://ns.chinatrust.com.tw/XSD/CTCB/ESB/Message/BSMF/ebMobileStartupInqRq/01\">\
<REQHDR>\
<TrnNum>INHB2015042900000001</TrnNum>\
<TrnCode>1957747793</TrnCode>\
</REQHDR>\
<REQBDY>\
<OS>iPhone</OS>\
<App>CC</App>\
<IconVersion></IconVersion>\
</REQBDY>\
</ebMobileStartupInqRq>";

License

WHCNetWorkKit is released under the MIT license. See LICENSE for details.

More Repositories

1

WHC_DataModelFactory

Mac上iOS开发辅助工具,快速把json/xml数据转换生成对应模型类属性,省去麻烦手动创建,提高开发效率。Mac iOS development aid, quickly put the json/XML data transformation generates the corresponding model class attribute, save trouble created manually, improve the development efficiency.
Objective-C
1,183
star
2

WHC_AutoLayoutKit

iOS and Mac OS X platforms currently in use the fastest the simplest development to build the UI layout automatically open source library, strong dynamic layout constraint handling capacity,iOS/Mac OS X平台上使用简单动态布局约束处理能力
Objective-C
864
star
3

WHC_ModelSqliteKit

专业的ORM数据库操作开源库,线程安全,高性能模型对象存储Sqlite开源库,真正实现一行代码操作数据库,让数据库存储变得简单 Professional database storage solutions, thread safe, high-performance model object storage Sqlite open source library, realize one line of code database operation, simple database storage
Objective-C
616
star
4

WHC_Scan

高效强大扫描分析iOS和Android项目里没有使用的类Mac开源工具,清理项目垃圾类,让项目结构干净清爽,升级维护得心应手. Efficient and powerful scanning analysis iOS and Android project no classes used in Mac open source tools, cleaning rubbish class project, make project structure clean and relaxed, upgrade maintenance
Swift
429
star
5

WHC_KeyboardManager

IOS lightweight keyboard manager, use simple and powerful, the keyboard will never block input controls. iOS轻量级的键盘管理器,使用简单功能强大,键盘再也不会挡住输入控件
Objective-C
307
star
6

WHC_ScanUnreferenceImageTool

扫描项目里没有使用的图片工具,删除没有引用的图片以减小打包体积 Scanning project does not use images in tool, delete without reference images to reduce the packaging volume
Swift
284
star
7

WHC_CollectionViewFramework

高仿支付宝可拖拽排序编辑动画效果集合视图
Swift
264
star
8

WHC_Model

iOS平台高效转换引擎json->model,model->json,model->Dictionary,支持模型类继承其他模型类,支持指定路径转换,不区分json的key和模型属性名称大小写,自动处理json中null
Objective-C
246
star
9

react-native-whcapp

react-native + redux whcapp Provide learning advice for learning react-native development Support: Android 5+ iOS 8.0+,一个完整的react-native app 并且带有完整的数据交互实现
JavaScript
216
star
10

WHC_-ContainerView

高仿qq未读消息拖拽和网易新闻viewpager特效集成
Objective-C
131
star
11

whc_wechat_image_edit

开源一个完整微信小程序app,已经上线了名称《节日头像生成》 主要功能:给微信头像进行编辑添加节日标签, 节日海报的制作生成
JavaScript
120
star
12

WHC_ReaderKit

轻量级小说阅读架构(简单,高效)
Swift
85
star
13

SexyJson

SexyJson is Swift5.+ json parse open source library quickly and easily, perfect supporting class and struct model, fully oriented protocol architecture, support iOS and MAC OS X
Swift
67
star
14

WHC_PageViewKit

iOS平台轻量级的PageView组件,其中TitleBar拥有30多种UI样式
Swift
60
star
15

react-native-whc-banner

A react native module to banner auto play loop component, it works on iOS and Android
JavaScript
59
star
16

WHC_Layout

Swift iOS and Mac OS X platforms currently in use the fastest the simplest development to build the UI layout automatically open source library, strong dynamic layout constraint handling capacity,iOS/Mac OS X平台上目前使用最简单开发构建UI速度最快的自动布局开源库,强悍的动态布局约束处理能力
Swift
59
star
17

WHC_GestureUnlockScreenDemo

屏幕解锁(android类型手势拖拽解锁和iphone数字键盘解锁两种类型)采用动态渐变的色彩绘制整个UI,对于想学习动态和渐变绘图的开发者很有帮助。
Objective-C
51
star
18

whc_flutter_app

flutter whcapp Provide learning advice for learning flutter development Support: Android 5+ iOS 8.0+,一个完整的flutter app 并且带有完整的数据交互实现
Dart
51
star
19

WHC_Debuger

IOS Debuger super convenient development auxiliary debugger, in a team or maintenance projects according to the UI quickly locate unknown to the Class files, and from the quick fix related bugs .iOS Debuger超方便开发辅助调试器,在团队开发或者维护未知项目根据UI快速定位到Class文件,而从快速解决相关bug
Objective-C
37
star
20

WHC_PullAndUpRefreshDemo

高仿QQ下拉水滴刷新效果
Objective-C
36
star
21

react-native-whc-loading

A react native module to show loading ui, it works on iOS and Android.
JavaScript
32
star
22

react-native-whc-calendar

A react native module to show calendar, it works on iOS and Android. 跨平台日历组件支持iOS、Android
JavaScript
28
star
23

WHC_QRCodeScan

封装系统和ZXingObjc 二维码扫描,相册图片二维码识别,二维码生成,swift版和oc版
Objective-C
27
star
24

whc-json-to-class

javascript版本json自动转换生成对应语言模型类The whc-json-to-class is the javascript plug-in that automatically converts the json string to the corresponding language model class
JavaScript
25
star
25

FunWave

FunWave is Swift4.+ simple fun wave open source component
Swift
24
star
26

react-native-whc-toast

A react native module to show toast alert, it works on iOS and Android.
JavaScript
20
star
27

WHC_PhotoCameraChoicePictureDemo

iOS从相册和相机选择图片进行封装
Objective-C
19
star
28

Flutter-NotificationCenter

A lightweight, easy-to-use notification component library that corresponds to Dart supports post notification listening for notifications and notification removal
Dart
19
star
29

WHC_NavigationControllerKit

Lightweight gesture to return framework (simple, efficient)轻量级手势返回框架(简单,高效)
Objective-C
18
star
30

react-whc-notification

A react、vue、react native module to post and observer notification, it works on React 、React-Native Vue、H5. (支持一对多发送通知消息、主要解决react、react-native、vue、h5夸组件页面之间的通信状态管理等),该组件可以用来替代Redux,Vuex进行状态管理
JavaScript
18
star
31

WHC_BannerKit

Lightweight image shuffling components (efficient, simple)轻量级的本地以及网络自动轮播器(简单,高效)
Objective-C
14
star
32

WHC_TetrisGame

oc版俄罗斯方块游戏
Objective-C
13
star
33

WHC_XMLParser

自动解析xml为字典对象
Objective-C
12
star
34

react-native-whc-grid

A react native module to show grid view, it works on iOS and Android
JavaScript
12
star
35

store

这是带有过期时间的h5数据存储组件支持sessionStorage,localStorage
JavaScript
9
star
36

WHC_DragBadgeViewDemo

仿QQ未读消息badge拖拽水滴效果
Objective-C
5
star
37

WHC_XML

自动把字典转换为xml数据
Objective-C
4
star
38

qqFrame

高仿QQ侧滑菜单框架
Objective-C
4
star
39

qqSpaceFrame

高仿QQ空间侧滑菜单高斯模糊效果
Objective-C
3
star
40

WHC_Json

自动把字典转换为json
Objective-C
3
star