2

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();

    }
    */
    ?>

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
chris wang
  • 351
  • 2
  • 4
  • 7
  • According to [this answer](http://stackoverflow.com/a/9415479/3702797) it is due to connection timeout, is your server accessible? Ps: you're working on local server so your phone may have problems accessing it – Kaiido Sep 07 '14 at 10:20
  • my server can be access from mobile safiri or pc safiri or ie,but the simulator or iphone both run time out. – chris wang Sep 07 '14 at 10:30
  • 2
    I am experiencing the same issue after upgrading to xcode6 gm. My server is accessible on the browser(s), but it times out in the simulator. – izk Sep 11 '14 at 14:43
  • @izk, did you found the reason of this issue? I have the same. NSURLSessionDownloadTask works on iOS7 simulator, but return -1001 code on any request under iOS8... – alexey.metelkin Sep 18 '14 at 10:40
  • ok, for me it was error related to NSURLSessionConfiguration. In iOS7 HTTPMaximumConnectionsPerHost = 0 means unlimited or default (don't remember). Now, on iOS8 it's really zero. So task cannot run with this. – alexey.metelkin Sep 18 '14 at 11:50
  • Hi, you have fix the error? I have been same, I use NSUrlSesstion with "dataTaskWithRequest". Then I reset my simulator and rebuild. The result is an error :rror Domain=NSURLErrorDomain Code=-1005 "The network connection was lost." . What was happened? – AmyNguyen Apr 27 '16 at 10:02

1 Answers1

2

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.

Trinca
  • 1,606
  • 1
  • 15
  • 17