MKNetWorkKit使用方法

1.下载MKNetworkKit主要是下载MKNetWorkKIt这个。

2.新建一个ARC的工程。

3.将MKNetWorkKit这个文件夹拖到你新建的工程中。添加:SystemConfiguration.framework,CFNetwork.framework,Security.framework。

4.在ViewControler里的viewDidLoad方法里写下如下代码:

GET请求的:

    MKNetworkEngine *engine = [[MKNetworkEngine alloc] initWithHostName:@"192.168.2.176:9502/api"
    customHeaderFields:nil];
    MKNetworkOperation *op = [engine operationWithPath:@"login.php?userid=admin&userpwd=123" params:nil httpMethod:@"GET"];
    [op onCompletion:^(MKNetworkOperation *operation){
        NSLog(@"request string: %@",[op responseString]);
    } 
    onError:^(NSError *error){
    
    }];
    [engine enqueueOperation:op];

POST请求:

    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    [dic setValue:@"admin" forKey:@"username"];
    [dic setValue:@"123" forKey:@"password"];
    
    MKNetworkOperation *op = [engine operationWithPath:@"user.do" params:dic httpMethod:@"POST"];
    
    [op onCompletion:^(MKNetworkOperation *operation) {
        NSLog(@"post response string :%@",[op responseString]);                
    } onError:^(NSError *error) {
        DLog(@"%@", error);
    }];
    
    [engine enqueueOperation:op];

注意的地方:在初始化 MKNetworkEngine的时候,不要加上"http://" 会自动帮你加上的。




你可能感兴趣的:(MKNetWorkKit使用方法)