我正在尝试使用AFNetworking 2.0来执行我的网络请求,但我遇到了一些奇怪的行为.我按照文档中的建议进行了子类化AFHTTPSessionManager,并提供了一个类方法,该方法返回一个单例对象,该对象具有基本URL设置以及设置我的auth标头.
+ (id)sharedInstance {
static dispatch_once_t once;
static MyHTTPClient *sharedInstance;
dispatch_once(&once, ^{
sharedInstance = [[self alloc] initWithBaseURL: NSURLURLWithString:kPlatformAPIBaseURL]];
});
//Uncommenting this line makes the error go away
//sharedInstance.responseSerializer = [AFJSONResponseSerializer serializer];
//get latest session id everytime someone gets an instance of the client
sharedInstance.sessionId = [MySessionManager getSessionId];
return sharedInstance;
}
- (instancetype)initWithBaseURL:(NSURL *)url {
self = [super initWithBaseURL:url];
if(self) {
self.sessionId = [FSSessionManager getSessionId];
self.serializer = [AFHTTPRequestSerializer serializer];
[_serializer setValue:_sessionId forHTTPHeaderField:kAuthorizationHeader];
[_serializer setValue:@"application/json" forHTTPHeaderField:kAcceptHeader];
self.requestSerializer …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 NSURLSession 对后台上传的支持来上传文件。
NSURLSessionTask *task = [session uploadTaskWithRequest:request fromFile:fileUrl];
Run Code Online (Sandbox Code Playgroud)
在 iOS8 中一切正常,但当我在 iOS7 中运行相同的代码时,任务立即被取消,并显示错误消息:
"Error Domain=NSURLErrorDomain Code=-999 "cancelled" UserInfo=0x17df82a0 {NSErrorFailingURLKey=https://my.url.com/, NSErrorFailingURLStringKey=https://my.url.com/, NSLocalizedDescription=cancelled}"
Run Code Online (Sandbox Code Playgroud)
据我所知,iOS7 应该支持此功能,但我还没有找到任何内容来解释为什么会发生这种情况或如何修复它。