使用AFN需要先导入库文件有:MobileCoreServices.framework、SystemConfiguration.framework、CoreGraphics.framework、CoreLocation.framework、Foundation.framework、UIKit.framework、Security.framework;其次需要引入AFN的第三方文件(AFNetWorking),然后在需要使用AFN的文件中引入头文件即可。以下为示例:
#import "ViewController.h"
#import "AFNetworking.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self afn];
}
-(void)afn
{
AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
NSLog(@"%d",netWorkManager.isReachable);
//url接口
NSString *url_string = [NSString stringWithFormat:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/activitylist.php"];
NSLog(@"%@",url_string);
AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
// @"text/javascript"
//AFHTTPRequestOperationManager的属性//文件类型
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
//AFHTTPRequestOperationManager GET异步,文件获取是否成功
[manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
[netWorkManager stopMonitoring];
NSLog(@"object === %@",responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"失败 %@",error);
}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end