NSURLErrorDomain Code=-999 使用 AFNetwork 示例代码


NSURLErrorDomain Code=-999 using AFNetworking Sample Code

我正在使用AFNetworking的示例来使多部分表单上传正常工作。

我有一个 php 应用程序,它在 url 上有一个多部分表单,它正在运行:

digital@nefarious-3 [03:26:21] [~]
-> % curl --form "upload=@Snow.jpg" http://example.dev/api/v1/sendFile
["Successful"]

以上是我试图让AFNetworking请求使用的相同端点。仅当在 api 端成功处理文件时,才会生成消息。

下面是服务器上 ls -lash 的输出:

-rw-r--r-- 1 vagrant vagrant 290190 Jan 26 15:26 Snow.jpg

以下是AFNetworking页面的示例代码:

NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"http://example.dev/upload" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFileURL:[NSURL fileURLWithPath:@"file://path/to/image.jpg"] name:@"upload" fileName:@"filename.jpg" mimeType:@"image/jpeg" error:nil];
    } error:nil];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSProgress *progress = nil;
NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
    if (error) {
        NSLog(@"Error: %@", error);
    } else {
        NSLog(@"%@ %@", response, responseObject);
    }
}];
[uploadTask resume];

此错误如下:

2015-01-26 15:16:09.683 digitaltest[8507:241970] Error: Error Domain=NSURLErrorDomain Code=-999 "cancelled" UserInfo=0x7fb223551a50 {NSErrorFailingURLKey=http://example.dev/api/v1/sendFile, NSLocalizedDescription=cancelled, NSErrorFailingURLStringKey=http://example.dev/api/v1/sendFile}

我可以从 PHP 端的自定义日志记录中看到 iOS 应用程序永远不会访问服务器。该站点在我的本地计算机和iPad模拟器上都可见,没有重定向,并且不会通过SSL运行。

我已经在我现有的应用程序和一个带有简单 hello world 按钮的全新应用程序中尝试了示例代码,但没有任何运气。

我如何让它工作?

EDIT: As requested here is additional debug:
2015-01-26 20:38:58.211 digitaltest[9340:276782] fileDataURL: file:/Users/digital/Desktop/snow.jpg -- file:///
2015-01-26 20:38:58.223 digitaltest[9340:276782] error: Error Domain=NSCocoaErrorDomain Code=260 "The operation couldn’t be completed. (Cocoa error 260.)" UserInfo=0x7ff531757020 {NSFilePath=/file:/Users/digital/Desktop/snow.jpg, NSUnderlyingError=0x7ff53177bc90 "The operation couldn’t be completed. No such file or directory"}

这是证明文件存在的输出:

digital@nefarious-3 [08:39:59] [~]
-> % cd ~/Desktop
digital@nefarious-3 [08:40:05] [~/Desktop]
-> % LL
zsh: command not found: LL
digital@nefarious-3 [08:40:06] [~/Desktop]
-> % ll
total 1272
  0 drwxr-xr-x+  15 digital  staff   510B 26 Jan 20:35 .
  0 drwxr-xr-x+ 113 digital  staff   3.8K 26 Jan 20:40 ..
 24 -rw-r--r--@   1 digital  staff   8.0K 26 Jan 20:35 .DS_Store
  0 -rw-r--r--    1 digital  staff     0B 15 Jul  2013 .localized
  8 -rw-r--r--@   1 digital  staff   2.5K  5 Jan 15:29 Snow[0].jpg
  8 -rw-r--r--@   1 digital  staff   2.5K  5 Jan 15:29 Snow[1].jpg
  8 -rw-r--r--@   1 digital  staff   2.5K  5 Jan 15:29 Snow[2].jpg
  8 -rw-r--r--@   1 digital  staff   2.5K  5 Jan 15:29 Snow[3].jpg
  8 -rw-r--r--@   1 digital  staff   2.5K 26 Jan 20:35 snow.jpg

记下时间戳。

由于沙盒化,允许的路径是通过使用函数NSSearchPathForDirectoriesInDomains和域NSUserDomainMask来获得的。

有关可用应用程序目录的信息,请参阅关于 iOS 文件系统。

下面是为名为"fileName"的文件创建文档目录路径的示例:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths firstObject];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:fileName];

此外,还允许从应用程序包中读取文件。