首先导入头文件#import "Reachability.h"(注意:这个头文件的类在网上搜"iOS判断网络"就能搜到了)
还有头文件#import <CommonCrypto/CommonDigest.h>
/** 实现block 回调的处理数据方法. */
-
(void)networkingWithURL:(NSString *)urlString block:(void (^)(id))block {
BOOL isExistenceNetwork = YES;
Reachability *reach = [Reachability reachabilityForInternetConnection];
switch ([reach currentReachabilityStatus]) {
case NotReachable:{ isExistenceNetwork = NO; NSLog(@"没网"); break; } case ReachableViaWiFi:{ isExistenceNetwork = YES; break; } case ReachableViaWWAN:{ isExistenceNetwork = YES; break; }
}
// 将链接的中文部分专成ascii 码形式.
NSString *codeString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];if (isExistenceNetwork) {
NSURL *url = [NSURL URLWithString:codeString]; // 开通道. NSURLSession *session = [NSURLSession sharedSession]; // 添加任务. NSURLSessionDataTask *task = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { // 返回主线程. dispatch_async(dispatch_get_main_queue(), ^{ // 判断有数据的时候才执行下面代码. if (data) { NSString *defaut = @"default/com.hackemist.SDWebImageCache.default/"; NSString *behind = @".data"; NSString *libPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; NSString* path =[libPath stringByAppendingPathComponent:[defaut stringByAppendingFormat:@"%@%@",[NetworkingHandler md5HexDigest:codeString],behind]]; [data writeToFile:path atomically:YES]; // JSON 解析数据. id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; // block 回调. block(result); } }); }]; // 开始任务. [task resume];
} else {
NSLog(@"没有网络(断网)"); NSString *defaut = @"default/com.hackemist.SDWebImageCache.default/"; NSString *behind = @".data"; NSString *libPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; NSString* path =[libPath stringByAppendingPathComponent:[defaut stringByAppendingFormat:@"%@%@",[NetworkingHandler md5HexDigest:codeString],behind]]; NSMutableData *data = [NSMutableData dataWithContentsOfFile:path]; // NSLog(@"%@", data); if (data == nil) { return; } else { id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; block(result); }
}
}
/** 实现Delegate 回调的处理数据方法. */
-
(void)networkingWithURL:(NSString *)urlString delegate:(id<NetworkingHandlerDelegate>)delegate {
BOOL isExistenceNetwork = YES;
Reachability *reach = [Reachability reachabilityForInternetConnection];switch ([reach currentReachabilityStatus]) {
case NotReachable:{ isExistenceNetwork = NO; NSLog(@"没网"); break; } case ReachableViaWiFi:{ isExistenceNetwork = YES; break; } case ReachableViaWWAN:{ isExistenceNetwork = YES; break; }
}
// 将链接的中文部分专成ascii 码形式.
NSString *codeString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];if (isExistenceNetwork) {
NSURL *url = [NSURL URLWithString:codeString]; // 开通道. NSURLSession *session = [NSURLSession sharedSession]; // 添加任务. NSURLSessionDataTask *task = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { // 返回主线程. dispatch_async(dispatch_get_main_queue(), ^{ // 判断有数据的时候才执行下面代码. if (data) { NSString *defaut = @"default/com.hackemist.SDWebImageCache.default/"; NSString *behind = @".data"; NSString *libPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; NSString* path =[libPath stringByAppendingPathComponent:[defaut stringByAppendingFormat:@"%@%@",[NetworkingHandler md5HexDigest:codeString], behind]]; [data writeToFile:path atomically:YES]; // JSON 解析数据. id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; // delegate 回调. if ([delegate respondsToSelector:@selector(handleDataOverWithResult:)]) { [delegate handleDataOverWithResult:result]; } } }); }]; // 开始任务. [task resume];
} else {
NSLog(@"没有网络(断网)"); NSString *defaut = @"default/com.hackemist.SDWebImageCache.default/"; NSString *behind = @".data"; NSString *libPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; NSString* path =[libPath stringByAppendingPathComponent:[defaut stringByAppendingFormat:@"%@%@",[NetworkingHandler md5HexDigest:codeString], behind]]; NSMutableData *data = [NSMutableData dataWithContentsOfFile:path]; if (data == nil) { return; } else { NSError *errorJOSON = nil; id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&errorJOSON]; if ([delegate respondsToSelector:@selector(handleDataOverWithResult:)]) { [delegate handleDataOverWithResult:result]; } }
}
}
pragma mark ** md5加密
-
(NSString )md5HexDigest:(NSString)input {
const char* str = [input UTF8String];
unsigned char result[CC_MD5_DIGEST_LENGTH];
CC_MD5(str, (unsigned int)strlen(str), result);
NSMutableString *hash = [NSMutableString string];
for (int i = 0; i < 16; i++) {
[hash appendFormat:@"%02X", result[i]];
}
NSString *mdfiveString = [hash lowercaseString];
return mdfiveString;
}