Latest 6.1.2
Homepage https://github.com/Daford/YFNetworkingKit
License MIT
Platforms ios 7.0, requires ARC
Frameworks Security
Authors

https://github.com/Daford/YFNetworkingKit.git

Author: Daford
Date: 2016/10/24

测试URL

#define BaseURLString @"http://c.hiphotos.baidu.com/" 
#define ImageSubPath @"image/pic/item/a9d3fd1f4134970ada1ced7996cad1c8a7865d7d.jpg"

1.网络状态

- (BOOL)testReachability {
    [[YFNetworkObserver sharedObserver] startObserver];
    BOOL ret = [[YFNetworkObserver sharedObserver] reachability];
    if (ret) {
        NSLog(@" [YF] : %s, Reachability", __FUNCTION__);
    }
    NSArray *networkStatuses = @[@"NotReachable", @"WiFi", @"WWAN"];
    YFNetworkStatus status = [[YFNetworkObserver sharedObserver] currentReachabilityStatus];
    NSLog(@" [YF] : %s, networkStatus: %@", __FUNCTION__, networkStatuses[status]);
    if (status == YFReachableViaWWAN) {
        NSArray *WWANAccessTypes = @[@"2G", @"3G", @"4G", @"未知"];
        YFWWANAccessType type = [[YFNetworkObserver sharedObserver] currentWWANType];
        if (type == YFWWANTypeUnknown) {
            NSLog(@" [YF] : %s, WWANAccessType: %@", __FUNCTION__, WWANAccessTypes.lastObject);
        } else {
            NSLog(@" [YF] : %s, WWANAccessType: %@", __FUNCTION__, WWANAccessTypes[type]);
        }
    }
    return ret;
}

2.使用代理

NSString *URLString = [BaseURLString stringByAppendingString:ImageSubPath]; 
_networking = [YFNetworking networking]; 
_networking.delegate = self; 
_networking.timeoutInterval = 20.0; 
_networking.allowsCellularAccess = YES; 
[_networking GET:URLString parameters:nil]; 

- (void)requestDidFinishLoading:(YFNetworking *)nkObject {
    NSLog(@" [YF] : %s, URL: %@", __FUNCTION__, nkObject.URL);
    if (nkObject.data) {
        dispatch_async(dispatch_get_main_queue(), ^{
            UIImage *image = [UIImage imageWithData:nkObject.data];
            NSLog(@" [YF] : image: %@", image.description);
        });
    }
}

- (void)request:(YFNetworking *)nkObject didFailWithError:(NSError *)error {
    NSLog(@" [YF] : %s, URL: %@", __FUNCTION__, nkObject.URL);
    if (error) {
        NSLog(@" [YF] : error: %@", error.localizedDescription);
    }
}

3.使用Block

YFNetworking *networking = [YFNetworking networking]; 
networking.baseURL = [NSURL URLWithString:BaseURLString]; 
networking.timeoutInterval = 20.0; 
networking.acceptLanguage = @"en"; 
[networking GET:ImageSubPath parameters:nil completionHandler:^(NSURLResponse * _Nullable response, id  _Nullable responseObject, NSError * _Nullable error) { 
    NSLog(@" [YF] : Block URL: %@", response.URL); 
    if (responseObject) { 
        UIImage *image = [UIImage imageWithData:responseObject]; 
        NSLog(@" [YF] : Block image: %@", image.description); 
    } 
}];

4.常用参数处理

NSString *parameters = [NSString string]; 
parameters = [parameters appendFirstValue:@(1) forKey:@"id"] 
parameters = [parameters appendOtherValue:@"萧芳芳" forKey:@"name"]; 
parameters = [parameters appendOtherValue:URLString forKey:@"weibosite"]; 
NSLog(@" [YF] : parameters: %@", parameters);

NSDictionary *paramDict = [NSMutableDictionary dictionary]; 
[paramDict setValue:@(1) forKey:@"id"]; 
[paramDict setValue:[@"萧芳芳" URLEncodeString] forKey:@"name"]; 
[paramDict setValue:[URLString URLDecodeString] forKey:@"weibosite"];
NSLog(@" [YF] : paramDict: %@", paramDict); 

NSString *name = @"%E8%90%A7%E8%8A%B3%E8%8A%B3"; 
NSLog(@" [YF] : name: %@", [name URLDecodeString]);

Latest podspec

{
    "name": "YFNetworkingKit",
    "version": "6.1.2",
    "summary": "u57fau4e8eURLSessionu5c01u88c5u7684u7f51u7edcu8bf7u6c42.",
    "homepage": "https://github.com/Daford/YFNetworkingKit",
    "license": {
        "type": "MIT",
        "file": "FILE_LICENSE"
    },
    "authors": {
        "Daford": "[email protected]"
    },
    "platforms": {
        "ios": "7.0"
    },
    "source": {
        "git": "https://github.com/Daford/YFNetworkingKit.git",
        "tag": "6.1.2"
    },
    "source_files": "YFNetworking/**/*.{h,m}",
    "public_header_files": [
        "YFNetworking/**/*.h",
        "YFNetworkingKit/**/*.h"
    ],
    "frameworks": "Security",
    "requires_arc": true
}

Pin It on Pinterest

Share This