CocoaPods安装失败

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

AFNNSURLConnection包装方法

GET

//1.创建AFN管理者//AFHTTPRequestOperationManager内部包装了NSURLConnectionAFHTTPRequestOperationManager *manager =[AFHTTPRequestOperationManager manager];//2.利用AFN管理者发送请求NSDictionary *params=@{@"username":@"name",@"pwd":@"passWord"};

[manager GET:@"http://12.2.226.186:32812/login"parameters:paramssuccess:^(AFHTTPRequestOperation *operation,idresponSEObject) {

NSLog(@"请求成功---%@", responseObject);

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

NSLog(@"请求失败---%@", error);

}];

POST//1.创建AFN管理者//AFHTTPRequestOperationManager内部包装了NSURLConnectionAFHTTPRequestOperationManager *manager =[AFHTTPRequestOperationManager manager];//2.利用AFN管理者发送请求NSDictionary *params=@{@"username":@"name",@"pwd":@"password"};

[manager POST:@"http://12.2.226.186:32812/login"parameters:paramssuccess:^(AFHTTPRequestOperation *operation,idresponseObject) {

NSLog(@"请求成功---%@", responseObject);

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

NSLog(@"请求失败---%@", error);

}];

NSURLsession包装方法

GET

//1.创建AFN管理者AFHTTPSessionManager *manager =[AFHTTPSessionManager manager];//2.利用AFN管理者发送请求NSDictionary *params=@{@"username":@"name",@"pwd":@"password"};

[manager GET:@"http://12.2.226.186:32812/login"parameters:paramssuccess:^(NSURLSessionDataTask *task,idresponseObject) {

NSLog(@"请求成功---%@", responseObject);

} failure:^(NSURLSessionDataTask *task, NSError *error) {

NSLog(@"请求失败---%@", error);

}];

POST//1.创建AFN管理者AFHTTPSessionManager *manager =[AFHTTPSessionManager manager];//2.利用AFN管理者发送请求NSDictionary *params=@{@"username":@"name",@"pwd":@"password"};

[manager POST:@"http://12.2.226.186:32812/login"parameters:paramssuccess:^(NSURLSessionDataTask *task,idresponseObject) {

NSLog(@"请求成功---%@", responseObject);

} failure:^(NSURLSessionDataTask *task, NSError *error) {

NSLog(@"请求失败---%@", error);

}];

文件下载

//1.创建AFN管理者AFHTTPSessionManager *manager =[AFHTTPSessionManager manager];//2.利用AFN管理者发送请求NSURLRequest *reuqest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://12.2.226.186:32812/resources/videos/minion_02.mp4"]];

[[manager downloadTaskWithRequest:reuqest progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {//targetPath: 已经下载好的文件路径NSLog(@"targetPath = %@", targetPath);

NSString*path =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];

NSURL*documentsDirectoryPath =[NSURL fileURLWithPath:[path stringByAppendingPathComponent:response.suggestedFilename]];//返回需要保存文件的目标路径returndocumentsDirectoryPath;

} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {

NSLog(@"filePath = %@", filePath);

}] resume];

监听进度

/*要跟踪进度,需要使用 NSProgress,是在 iOS 7.0 推出的,专门用来跟踪进度的类!

NSProgress只是一个对象!如何跟踪进度!-> KVO 对属性变化的监听!

@property int64_t totalUnitCount; 总单位数

@property int64_t completedUnitCount; 完成单位数*/NSProgress*progress =nil;//注册通知[progress addObserver:self forKeyPath:@"completedUnitCount"options:NSKeyValueObservingOptionNew context:nil];- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)objectchange:(NSDictionary *)change context:(void*)context {

NSLog(@"%@",object);/**

准确的获得进度

localizedDescription 10%

localizedAdditionalDescription completed 32,768 of 318,829

fractionCompleted 0.102776(completedUnitCount/totalUnitCount)*/if([objectisKindOfClass:[NSProgressclass]]) {

NSProgress*p = (NSProgress *)object;

NSLog(@"%@, %@, %f", p.localizedDescription, p.localizedAdditionalDescription, p.fractionCompleted);

文件上传

//1.创建AFN管理者AFHTTPSessionManager *manager =[AFHTTPSessionManager manager];//2.利用AFN管理者发送请求[manager POST:@"http://12.2.226.186:32812/upload"parameters:@{@"username":@"jx"} constructingBodyWithBlock:^(id<AFMultipartFormData>formData) {

NSData*data = [NSData dataWithContentsOfFile:@"/Users/Jx-Liu/Desktop/Snip20150811_1.png"];

[formData appendPartWithFileData:data name:@"file"fileName:@"jx.png"mimeType:@"image/png"];

} success:^(NSURLSessionDataTask *task,idresponseObject) {

NSLog(@"请求成功---%@", responseObject);

} failure:^(NSURLSessionDataTask *task, NSError *error) {

NSLog(@"请求失败---%@", error);

}];

[formData appendPartWithFileURL:[NSURL fileURLWithPath:@"/Users/Jx-Liu/Desktop/Snip20150811_1.png"] name:@"file"fileName:@"jx.png"mimeType:@"image/png"error:nil];

[formData appendPartWithFileURL:[NSURL fileURLWithPath:@"/Users/Jx-Liu/Desktop/Snip20150811_1.png"] name:@"file"error:nil];

AFN解耦

自定义单利类基础Manager优点: 替换框架只需要求改单利类即可序列化

AFN默认将服务器返回的数据当做JSON处理, 会自动解析manager.responseSerializer = [AFJSONRequestSerializer serializer];告诉AFN,以xml形式解析服务器返回的数据manager.responseSerializer = [AFXMLParserResponseSerializer serializer];告诉AFN, 不处理服务器返回的数据, 原样返回manager.responseSerializer = [AFHTTPResponseSerializer serializer];网络状态检测/1.创建网络监听对象

AFNetworkReachabilityManager*manager =[AFNetworkReachabilityManager sharedManager];//2.设置网络状态改变回调[manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {/*AFNetworkReachabilityStatusUnknown = -1, // 未知

AFNetworkReachabilityStatusNotReachable = 0, // 无连接

AFNetworkReachabilityStatusReachableViaWWAN = 1, // 3G 花钱

AFNetworkReachabilityStatusReachableViaWiFi = 2, // 局域网络,不花钱*/switch(status) {case0:

NSLog(@"无连接");

break;case1:

NSLog(@"3G 花钱");

break;case2:

NSLog(@"局域网络,不花钱");

break;default:

NSLog(@"未知");

break;

}

}];//3.开始监听[manager startMonitoring];

苹果自带- (void)viewDidLoad {

[super viewDidLoad];//注册通知[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getNetworkStatus) name:kReachabilityChangedNotificationobject:nil];//开始监听网络self.reachability =[Reachability reachabilityForInternetConnection];

[self.reachability startNotifier];

}- (void)dealloc

{

[[NSNotificationCenter defaultCenter] removeObserver:self];

[self.reachability stopNotifier];

}- (void)getNetworkStatus{if([Reachability reachabilityForLocalWiFi].currentReachabilityStatus !=NotReachable) {

NSLog(@"wifi");

}elseif([Reachability reachabilityForInternetConnection].currentReachabilityStatus !=NotReachable)

{

NSLog(@"手机自带网络");

}else{

NSLog(@"没有网络");

}

}

CocoaPods安装失败CocoaPods安装东西的时候它要找到Xcode的Developer文件夹, 如果找不到会报如下错误

解决方案

JX替换为你自己的用户名sudo xcode-select --switch/Users/JX/applications/Xcode.app/Contents/Developer

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航