AFNetworking AFHTTPClient类 [英] AFNetworking AFHTTPClient Class

查看:202
本文介绍了AFNetworking AFHTTPClient类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对iOS编程还很陌生,尤其是在涉及网络服务时。我正在出于学术目的开发应用程序,并且需要与服务器进行通信(当前使用AFNetworking2和Restler / php),当涉及到GET方法时,一切正常。但是我什么也上传不了。



花了几个小时,在github支持网站上,stackoverflow,几乎所有的示例/问题都上传了图片(而且很多) ),请使用以下行:

  AFHTTPClient * client = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@ http:// server] ]; 

我的共享客户端确实有一个Client类,即AFHTTPSessionManager的子类。但是所有示例都将此AFHTTPClient与initWithURL以及其他我无法找到的其他类(例如AFJSONRequestOperation)一起使用。



大多数情况下,我说我应该创建一个单例的AFHTTPClient子类,但我在任何地方都找不到。有些链接甚至将我发送到官方的github存储库,但现在不再可用。
所以我的问题是,在哪里可以获取有关AFHTTPClient的更多信息,我应该使用它吗?有人可以向我指出如何创建一个或至少了解其功能的教程。



欢呼

解决方案

在AFNetworking 2.0中,AFHTTPClient已由AFHTTPRequestOperationManager / AFHTTPSessionManager取代。我建议您参考git中示例。 Git克隆并在XCode中打开。它应该可以帮助您。

如果要使用AFHTTPClient,即1.x代码。这是到分支的git链接。相应的pod规格为



pod'AFNetworking','〜> 1.3.3'



In 2.0 AFNetworking,您可以像这样创建一个单例客户端。



界面



  @interface AFAppDotNetAPIClient:AFHTTPSessionManager 

+(instancetype)sharedClient;

@end



实施



  #import AFAppDotNetAPIClient.h 

静态NSString * const AFAppDotNetAPIBaseURLString = @ https://alpha-api.app.net/ ;

@implementation AFAppDotNetAPIClient

+(instancetype)sharedClient {
静态AFAppDotNetAPIClient * _sharedClient = nil;
static dispatch_once_t OnceToken;
dispatch_once(& onceToken,^ {
_sharedClient = [[AFAppDotNetAPIClient alloc] initWithBaseURL:[NSURL URLWithString:AFAppDotNetAPIBaseURLString]]];
[_sharedClient setSecurityPolicy:[AFSecurityPolicy policyWithPinningMode]:AFSSL b $ b});

return _sharedClient;
}

@end


I’m fairly new to iOS programming, especially when it comes to webservices. I’m developing a App for academic purposes, and I need to communicate with my server, currently using AFNetworking2 and Restler/php, everything work when it comes to GET methods. But I can’t upload anything.

Been reading for hours, in github support site, stackoverflow, pretty much all examples/questions to upload images (and there are a LOT) use this line:

AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://server"]]; 

I do have a Client class, subclass of AFHTTPSessionManager, with my sharedClient. But all examples use this AFHTTPClient with initWithURL and other classes like AFJSONRequestOperation that I can’t no longer find.

Mostly it says I should create a singleton, subclass of AFHTTPClient, but I can´t find it anywhere. Some links even send me to official github repository but it’s not available anymore. So my question is, where can I get more info about AFHTTPClient, should I use it, can anyone point me a tutorial on how to create one or at least understand its functionality.

Cheers

解决方案

In AFNetworking 2.0 the AFHTTPClient has been replaced by AFHTTPRequestOperationManager / AFHTTPSessionManager. I would suggest you to refer to the example in git by them. Git clone and open in XCode. It should help you. That has the most updated example.

If you want to use AFHTTPClient i.e 1.x code. Here is the git link to the branch. The pod spec to that would be

pod 'AFNetworking', '~> 1.3.3'

In 2.0 AFNetworking, you can create a singleton client like this.

interface

@interface AFAppDotNetAPIClient : AFHTTPSessionManager

+ (instancetype)sharedClient;

@end

Implementation

#import "AFAppDotNetAPIClient.h"

static NSString * const AFAppDotNetAPIBaseURLString = @"https://alpha-api.app.net/";

@implementation AFAppDotNetAPIClient

+ (instancetype)sharedClient {
    static AFAppDotNetAPIClient *_sharedClient = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedClient = [[AFAppDotNetAPIClient alloc] initWithBaseURL:[NSURL URLWithString:AFAppDotNetAPIBaseURLString]];
        [_sharedClient setSecurityPolicy:[AFSecurityPolicy policyWithPinningMode:AFSSLPinningModePublicKey]];
    });

    return _sharedClient;
}

@end

这篇关于AFNetworking AFHTTPClient类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆