標籤:
1、把AFNetworking的檔案拖放到項目中(注意不同的版本方法不一樣,本曆程基於版本2013);
2、使用#import "AFNetworking.h"命令把AFNetworking.h=包含進來;
3、完整代碼:
1 // 2 // ViewController.m 3 // 網路請求 4 // 5 // Created by mac on 16/5/12. 6 // Copyright © 2016年 mzw. All rights reserved. 7 // 8 9 #import "ViewController.h"10 #import "AFNetworking.h"11 12 @interface ViewController ()13 14 @end15 16 @implementation ViewController17 18 - (void)viewDidLoad {19 [super viewDidLoad];20 21 // 1、先建立一個字串,把基url和介面url串連起來22 NSString *urlstring = [@"http://tl.bearapp.cn" stringByAppendingString:@"/api/index/zdmc.php"];23 // 2、使用上面建立的字串產生一個url24 NSURL *url = [NSURL URLWithString:urlstring];25 // 3、建立要求管理對象26 AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc]initWithBaseURL:url];//如果此處使用manager類方法初始化,就要在GET命令後面傳入url27 // 4、建立參數28 NSDictionary *dicc = [[NSDictionary alloc]initWithObjectsAndKeys:@"二道沙河",@"title", nil];29 // 5、擷取資源30 [manager GET:@"sdf" parameters:dicc success:^(AFHTTPRequestOperation *operation, id responseObject) {31 NSLog(@"%@",responseObject);//如果成功,則列印擷取的結果32 } failure:^(AFHTTPRequestOperation *operation, NSError *error) {33 NSLog(@"%@",error);//如果失敗,則列印錯誤的資訊34 }];35 36 }37 38 @end
IOS開發中使用AFNetworking請求網路資料