Domain = NSURLErrorDomain代码= -1001“该操作无法完成。 (NSURLErrorDomain错误-1001。)" [英] Domain=NSURLErrorDomain Code=-1001 "The operation couldn’t be completed. (NSURLErrorDomain error -1001.)"

查看:2224
本文介绍了Domain = NSURLErrorDomain代码= -1001“该操作无法完成。 (NSURLErrorDomain错误-1001。)"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序编写为从cam上传图片,示例代码如下:

  

#定义WEBSERVICE_URL @ http://192.168.0.104/upload.php
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

[选择器dismissViewControllerAnimated:YES完成:^ {

UIImage * selectedImage = [info objectForKey:UIImagePickerControllerOriginalImage];

NSData * imageData = UIImagePNGRepresentation(selectedImage);

NSMutableURLRequest *请求= [[AFHTTPRequestSerializer序列化器] multipartFormRequestWithMethod:@ POST URLString:WEBSERVICE_URL参数:nil constructioningBodyWithBlock:^(id formData){
[formData appendPartWithFileData:imageData name:@ upfile fileName:@ test mimeType:@ image / png];
}错误:无];

AFURLSessionManager * manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];

NSProgress *进度=零;

NSURLSessionUploadTask * uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progresscompleteHandler:^(NSURLResponse * response,id responseObject,NSError * error){

[进度removeObserver:self forKeyPath :@ fractionCompleted];
NSLog(error.debugDescription);


if(error){
[self.view updateWithMessage:[NSString stringWithFormat:@ Error:%@!,error.debugDescription]];
} else {
[self.view updateWithMessage:@非常成功!];

}
}];

[进度addObserver:self forKeyPath:@ fractionCompleted选项:NSKeyValueObservingOptionNew context:NULL];

[uploadTask简历];

self.imageUploadProgress = [[TNSexyImageUploadProgress alloc] init];
self.imageUploadProgress.radius = 100;
self.imageUploadProgress.progressBorderThickness = -10;
self.imageUploadProgress.trackColor = [UIColor blackColor];
self.imageUploadProgress.progressColor = [UIColor whiteColor];
self.imageUploadProgress.imageToUpload = selectedImage;
[self.imageUploadProgress show];

[[NSNotificationCenter defaultCenter] addObserver:自我选择器:@选择器(imageUploadCompleted :)名称:IMAGE_UPLOAD_COMPLETED对象:self.imageUploadProgress];

}];
}

错误是:

 



TNSexyImageUploadProgressDemo [5275:113032]
错误域= NSURLErrorDomain代码= -1001
操作无法完成。
(NSURLErrorDomain错误-1001。)
UserInfo = 0x7f92c2d907d0 {NSErrorFailingURLStringKey = http://192.168.0.104/upload.php
NSUnderlyingError = 0x7f92c2dcd5c0
操作无法完成。(kCFErrorDomainCFNetwork错误-1001。),
NSErrorFailingURLKey = h ttp://192.168.0.104/ upload.php }



无论在模拟器还是iPhone上,同样的错误,我使用野生动物园访问url,一切正确,我使用wifi访问网络。
ping 192.168.0.104是可以的。



服务器程序是用php编写的,代码令人沮丧:

  

100000000){
$ result_json ['error'] ='超出文件大小';
}

$ finfo = new finfo(FILEINFO_MIME_TYPE);

if(false === $ ext = array_search(
$ finfo-> file($ _ FILES ['upfile'] ['tmp_name'])),
array(
'png'=>'image / png'
),
true
)){
$ result_json ['error'] ='无效的文件格式';
}

if(!move_uploaded_file(
$ _FILES ['upfile'] ['tmp_name'],
sprintf('./ uploads /%s。% s',
sha1_file($ _ FILES ['upfile'] ['tmp_name']),
$ ext

)){
$ result_json ['error '] ='无法移动上传的文件';
}

//立即发送结果
echo json_encode($ result_json);

/ *
try {

if(!move_uploaded_file(
$ _FILES ['upfile'] ['tmp_name'],
sprintf('./ uploads /%s。%s',
sha1_file($ _ FILES ['upfile'] ['tmp_name']),
$ ext

) ){
//抛出新的RuntimeException('无法移动上传的文件。');
}

//回显json_encode(array(’succes’=> true));
} catch(RuntimeException $ e){

// echo $ e-> getMessage();

}
* /
?>




https://github.com。 com / AFNetworking / AFNetworking



如果您使用的是Xcode 6,请尝试一下。


my program is write for upload the picture from the cam,sample code below:



    #define WEBSERVICE_URL @"http://192.168.0.104/upload.php"
    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

        [picker dismissViewControllerAnimated:YES completion:^{

            UIImage *selectedImage = [info objectForKey:UIImagePickerControllerOriginalImage];

            NSData *imageData = UIImagePNGRepresentation(selectedImage);

            NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:WEBSERVICE_URL parameters:nil constructingBodyWithBlock:^(id formData) {
                [formData appendPartWithFileData:imageData name:@"upfile" fileName:@"test" mimeType:@"image/png"];
            } 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) {

                [progress removeObserver:self forKeyPath:@"fractionCompleted"];
                NSLog(error.debugDescription);


                if (error) {
                    [self.view updateWithMessage:[NSString stringWithFormat:@"Error : %@!", error.debugDescription]];
                } else {
                    [self.view updateWithMessage:@"Great success!"];

                }
            }];

            [progress addObserver:self forKeyPath:@"fractionCompleted" options:NSKeyValueObservingOptionNew context:NULL];

            [uploadTask resume];

            self.imageUploadProgress = [[TNSexyImageUploadProgress alloc] init];
            self.imageUploadProgress.radius = 100;
            self.imageUploadProgress.progressBorderThickness = -10;
            self.imageUploadProgress.trackColor = [UIColor blackColor];
            self.imageUploadProgress.progressColor = [UIColor whiteColor];
            self.imageUploadProgress.imageToUpload = selectedImage;
            [self.imageUploadProgress show];

            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(imageUploadCompleted:) name:IMAGE_UPLOAD_COMPLETED object:self.imageUploadProgress];

        }];
    }

the error is :

TNSexyImageUploadProgressDemo[5275:113032] Error Domain=NSURLErrorDomain Code=-1001 "The operation couldn’t be completed. (NSURLErrorDomain error -1001.)" UserInfo=0x7f92c2d907d0 {NSErrorFailingURLStringKey="http://192.168.0.104/upload.php", NSUnderlyingError=0x7f92c2dcd5c0 "The operation couldn’t be completed. (kCFErrorDomainCFNetwork error -1001.)", NSErrorFailingURLKey="http://192.168.0.104/upload.php"}

whatever on simulator or iPhone,the same error,i use safari to access url,anything is correct,i use wifi to access network. ping 192.168.0.104 is ok.

server's program is write in php,code blelow:



     100000000) {
        $result_json['error'] = 'Exceeded filesize';
    }

    $finfo = new finfo(FILEINFO_MIME_TYPE);

    if (false === $ext = array_search(
        $finfo->file($_FILES['upfile']['tmp_name']),
            array(          
                'png' => 'image/png'           
            ),
            true
        )) {
            $result_json['error'] = 'Invalid file format';
    }

    if (!move_uploaded_file(
        $_FILES['upfile']['tmp_name'],
        sprintf('./uploads/%s.%s',
            sha1_file($_FILES['upfile']['tmp_name']),
            $ext
        )
        )) {
            $result_json['error'] = 'Failed to move uploaded file';
    }

    // send the result now
    echo json_encode($result_json);

    /*
    try {

        if (!move_uploaded_file(
            $_FILES['upfile']['tmp_name'],
            sprintf('./uploads/%s.%s',
                sha1_file($_FILES['upfile']['tmp_name']),
                $ext
            )
            )) {
                //throw new RuntimeException('Failed to move uploaded file.');
        }

        //echo json_encode(array('succes'=>true));
    } catch (RuntimeException $e) {

        //echo $e->getMessage();

    }
    */
    ?>

解决方案

I was having the same problem since I started using Xcode 6. Updating the AFNetworking library fixed the problem. Everything is working fine again.

https://github.com/AFNetworking/AFNetworking

If you're using Xcode 6, give it a try.

这篇关于Domain = NSURLErrorDomain代码= -1001“该操作无法完成。 (NSURLErrorDomain错误-1001。)"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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