iOS之网络请求框架YTKHttp的使用

github地址:https://github.com/yuantiku/YTKNetwork

pod 'AFNetworking'
  pod 'YTKNetwork'
  pod 'YYModel'
  pod 'Toast'

参考原理解析:http://www.cocoachina.com/ios/20170720/19942.html

https://www.cnblogs.com/guohai-stronger/p/9194519.html

错误集合https://www.jianshu.com/p/30dd2f665933

基础教程:https://github.com/yuantiku/YTKNetwork/blob/master/Docs/BasicGuide_cn.md

高级教程:https://github.com/yuantiku/YTKNetwork/blob/master/Docs/ProGuide_cn.md

 

*************简单的使用:

#import 
#import 
@interface LYBYTKHttpManager : YTKRequest
-(id)initWithsDict:(NSDictionary *)dict;

@end

**************
#import "LYBYTKHttpManager.h"
typedef NS_ENUM(NSUInteger, ServerType) {
    kSeverTypeDev,     // 开发服务器地址
    kSeverTypeTest,     //测试服务器地址
    kSeverTypeRelease   //发布版服务器地址
};
@interface LYBYTKHttpManager()
@property(nonatomic,copy)NSDictionary *dict;

@end
@implementation LYBYTKHttpManager
//-(void)configHttps {
//
//    // 获取证书
//    NSString *cerPath = [[NSBundle mainBundle] pathForResource:@"ssl_content" ofType:@"pem"];//证书的路径
//    NSData *certData = [NSData dataWithContentsOfFile:cerPath];
//
//    // 配置安全模式
//    YTKNetworkConfig *config = [YTKNetworkConfig sharedConfig];
//    //    config.cdnUrl = @"";
//
//    // 验证公钥和证书的其他信息
//    AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
//
//    // 允许自建证书
//    securityPolicy.allowInvalidCertificates = YES;
//
//    // 校验域名信息
//    securityPolicy.validatesDomainName = YES;
//
//    // 添加服务器证书,单向验证;  可采用双证书 双向验证;
//    securityPolicy.pinnedCertificates = [NSSet setWithObject:certData];
//
//    [config setSecurityPolicy:securityPolicy];
//}
-(NSString *)config:(NSInteger)sever{
   
    YTKNetworkAgent *agent = [YTKNetworkAgent sharedAgent];
    [agent setValue:[NSSet setWithObjects:@"application/json", @"text/plain", @"text/javascript", @"text/json",@"text/html",@"text/css", nil] forKeyPath:@"_manager.responseSerializer.acceptableContentTypes"];
    
    YTKNetworkConfig *config = [YTKNetworkConfig sharedConfig];
    NSString *msg = @"";
    switch (sever) {
        case kSeverTypeDev:     // 开发服务器地址
            config.baseUrl = @"http://192.168.88.9:8081";
            msg = [NSString stringWithFormat:@"开发网%@",config.baseUrl];
            break;
        case kSeverTypeTest:     //测试服务器地址
            config.baseUrl = @"http://192.168.88.9:8081";
            msg = [NSString stringWithFormat:@"测试网%@",config.baseUrl];
            break;
        case kSeverTypeRelease:   //发布版服务器地址
            config.baseUrl = @"http://192.168.88.9:8081";
            msg = [NSString stringWithFormat:@"外网%@",config.baseUrl];
            break;
        default:
            break;
    }
//    [self configHttps];
    NSLog(@"\\n\\n%@\\n\\n",msg);
    return msg;
}


//实利化0
-(id)initWithsDict:(NSDictionary *)dict{
    self = [super init];
    if (self) {
    self.dict=dict;
    [self config:0];
    }
    return self;
}
//3
- (NSString *)requestUrl
{
    return @"/admin/login/login";
}
//2
- (YTKRequestMethod)requestMethod
{
    return YTKRequestMethodPOST;
}
//4
- (id)requestArgument
{
    
    return self.dict;
}
//验证返回的数据格式
-(id)jsonValidator {
    return @{
             @"status": [NSString class],
             @"msg": [NSString class]
             };
       }

////使用cdn加速--1
//-(BOOL)useCDN {
//    return YES;
//}
//-(YTKRequestSerializerType)requestSerializerType{
//
//    return YTKRequestSerializerTypeJSON;
//}
//
//-(YTKResponseSerializerType)responseSerializerType{
//
//    return YTKResponseSerializerTypeJSON;
//}


@end


**********
#import 
#import "LYBBaseVC.h"
@interface OneViewController : LYBBaseVC

@end

************
#import "OneViewController.h"
#import "LYBYTKHttpManager.h"

@interface OneViewController()


@end
@implementation OneViewController
-(void)viewDidLoad{
    [super viewDidLoad];
    self.navigationItem.title=@"one";
    [self initbankimage];
   
}
-(void)initbankimage{
    NSDictionary *params=@{
                           @"username":@"lambo",
                           @"password":@"123456",
                           };
   
    //方法一:block回调实现
    LYBYTKHttpManager *request = [[LYBYTKHttpManager alloc]initWithsDict:params];
    [request startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
        NSLog(@"succeed---%@",request.responseString);
    } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:request.responseData options:NSJSONReadingAllowFragments error:nil];
        NSLog(@"failure--%@--%@",request.responseString,dict);
    }];
    
    //方法二:使用代理实现
//    LYBYTKHttpManager *api = [[LYBYTKHttpManager alloc]initWithsDict:params];
//    api.delegate = self;
//    [api start];
}



- (void)requestFinished:(YTKBaseRequest *)request {
    
    NSLog(@"----- succeed ---- %@", request.responseString);
    
   
}

- (void)requestFailed:(YTKBaseRequest *)request {
    
   NSLog(@"failed---%@", [[NSString alloc] initWithData:request.responseData encoding:NSUTF8StringEncoding]);
    
}


@end

 

你可能感兴趣的:(iOS/oc)