URLSession in swift 4 for uploading jpeg file from iOS gallery generating Error
Asked Answered
I

0

8

I am trying to make uploading of my image from iPhone gallery to my web-server running on PHP. For uploading request I am using URLSession and for selecting images from iPhone Gallery UIImagePicker.

I am getting this error from my iPhone 7 and iPhone Simulator.

Error code from console is here

2018-06-19 12:06:56.076335+0200 google01[4054:1274269] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /private/var/containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles
2018-06-19 12:06:56.077701+0200 google01[4054:1274269] [MC] Reading from public effective user settings.
2018-06-19 12:06:56.077701+0200 google01[4054:1274269] [MC] Reading from public effective user settings.
2018-06-19 12:07:00.633120+0200 google01[4054:1274376] [BoringSSL] Function boringssl_session_errorlog: line 2878 [boringssl_session_write] SSL_ERROR_SYSCALL(5): operation failed externally to the library
2018-06-19 12:07:00.644399+0200 google01[4054:1274552] [discovery] errors encountered while discovering extensions: Error Domain=PlugInKit Code=13 "query cancelled" UserInfo={NSLocalizedDescription=query cancelled}
2018-06-19 12:07:00.668971+0200 google01[4054:1274376] [BoringSSL] Function boringssl_session_errorlog: line 2878 [boringssl_session_write] SSL_ERROR_SYSCALL(5): operation failed externally to the library
2018-06-19 12:07:00.715021+0200 google01[4054:1274376] [BoringSSL] Function boringssl_session_errorlog: line 2878 [boringssl_session_write] SSL_ERROR_SYSCALL(5): operation failed externally to the library
2018-06-19 12:07:00.727139+0200 google01[4054:1274376] [BoringSSL] Function boringssl_session_errorlog: line 2878 [boringssl_session_write] SSL_ERROR_SYSCALL(5): operation failed externally to the library
2018-06-19 12:07:00.802869+0200 google01[4054:1274376] [BoringSSL] Function boringssl_session_write: line 3420 length is zero
2018-06-19 12:07:00.803070+0200 google01[4054:1274376] [BoringSSL] Function boringssl_session_errorlog: line 2878 [boringssl_session_write] SSL_ERROR_SYSCALL(5): operation failed externally to the library
2018-06-19 12:07:00.814943+0200 google01[4054:1274376] [BoringSSL] Function boringssl_session_write: line 3420 length is zero
2018-06-19 12:07:00.815057+0200 google01[4054:1274376] [BoringSSL] Function boringssl_session_errorlog: line 2878 [boringssl_session_write] SSL_ERROR_SYSCALL(5): operation failed externally to the library
2018-06-19 12:07:00.851161+0200 google01[4054:1274376] [BoringSSL] Function boringssl_session_write: line 3420 length is zero
2018-06-19 12:07:00.851395+0200 google01[4054:1274376] [BoringSSL] Function boringssl_session_errorlog: line 2878 [boringssl_session_write] SSL_ERROR_SYSCALL(5): operation failed externally to the library
2018-06-19 12:07:00.855101+0200 google01[4054:1274376] [BoringSSL] Function boringssl_session_write: line 3420 length is zero
2018-06-19 12:07:00.855244+0200 google01[4054:1274376] [BoringSSL] Function boringssl_session_errorlog: line 2878 [boringssl_session_write] SSL_ERROR_SYSCALL(5): operation failed externally to the library
2018-06-19 12:07:00.867205+0200 google01[4054:1274376] [BoringSSL] Function boringssl_session_errorlog: line 2878 [boringssl_session_write] SSL_ERROR_SYSCALL(5): operation failed externally to the library
2018-06-19 12:07:00.883086+0200 google01[4054:1274376] [BoringSSL] Function boringssl_session_write: line 3420 length is zero
2018-06-19 12:07:00.883189+0200 google01[4054:1274376] [BoringSSL] Function boringssl_session_errorlog: line 2878 [boringssl_session_write] SSL_ERROR_SYSCALL(5): operation failed externally to the library
2018-06-19 12:07:00.891112+0200 google01[4054:1274376] [BoringSSL] Function boringssl_session_write: line 3420 length is zero
2018-06-19 12:07:00.891201+0200 google01[4054:1274376] [BoringSSL] Function boringssl_session_errorlog: line 2878 [boringssl_session_write] SSL_ERROR_SYSCALL(5): operation failed externally to the library
2018-06-19 12:07:00.907172+0200 google01[4054:1274376] [BoringSSL] Function boringssl_session_write: line 3420 length is zero

... (the same repeating lines of code but 100x times) and

2018-06-19 12:09:02.226689+0200 google01[4054:1275279] [BoringSSL] Function boringssl_session_errorlog: line 2881 [boringssl_session_read] SSL_ERROR_ZERO_RETURN(6): operation failed because the connection was cleanly shut down with a close_notify alert
2018-06-19 12:09:02.227160+0200 google01[4054:1275279] [BoringSSL] Function boringssl_session_errorlog: line 2881 [boringssl_session_read] SSL_ERROR_ZERO_RETURN(6): operation failed because the connection was cleanly shut down with a close_notify alert
2018-06-19 12:09:02.227357+0200 google01[4054:1275279] [BoringSSL] Function boringssl_session_errorlog: line 2881 [boringssl_session_read] SSL_ERROR_ZERO_RETURN(6): operation failed because the connection was cleanly shut down with a close_notify alert
2018-06-19 12:09:02.227508+0200 google01[4054:1275279] TIC Read Status [1:0x1c0166d80]: 1:57
2018-06-19 12:09:02.227649+0200 google01[4054:1275279] TIC Read Status [1:0x1c0166d80]: 1:57
2018-06-19 12:09:02.227739+0200 google01[4054:1275279] TIC Read Status [1:0x1c0166d80]: 1:57

My code for swift4 is this:

import UIKit

class uploadCustom: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, URLSessionDelegate, URLSessionTaskDelegate, URLSessionDataDelegate {

@IBOutlet weak var myImageView: UIImageView!
@IBOutlet weak var uploadButton: UIButton!
@IBOutlet weak var imageUploadProgressView: UIProgressView!
@IBOutlet weak var progressLabel: UILabel!

override func viewDidLoad() {

    super.viewDidLoad()

}

@IBAction func uploadButtonTapped(_ sender: Any) {
    var myPickerController = UIImagePickerController()
    myPickerController.delegate = self
    myPickerController.sourceType = UIImagePickerControllerSourceType.photoLibrary
    self.present(myPickerController, animated: true, completion: nil)    
}


public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    myImageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage
    myImageView.backgroundColor = UIColor.clear
    self.dismiss(animated: true, completion: nil)
    uploadImage()
}


func uploadImage() {
    let imageData = UIImageJPEGRepresentation(myImageView.image!, 1)

    if(imageData == nil) {
        return
    }

    self.uploadButton.isEnabled = false
    let uploadScriptUrl = URL(string: "https://mywebserver/upload5/")

    var request = URLRequest(url: uploadScriptUrl!)
    request.httpMethod = "POST"
    request.setValue("Keep-Alive", forHTTPHeaderField: "Connection")
    var configuration = URLSessionConfiguration.default
    var session = URLSession(configuration: configuration, delegate: self as! URLSessionDelegate, delegateQueue: OperationQueue.main)
    //var task = session.uploadTaskWithRequest(request as URLRequest, fromData: imageData!)
    //var task = session.uploadTask(with: request as URLRequest, fromFile: imageData)
    var task = session.uploadTask(with: request, from: imageData!)
    task.resume()

}

func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {

    let myAlert = UIAlertController(title: "Alert", message: error?.localizedDescription, preferredStyle: .alert)
    myAlert.addAction(UIAlertAction(title: "Yes", style: .default, handler: nil))
    self.present(myAlert, animated: true, completion: nil)
    self.uploadButton.isEnabled = true
}

func urlSession(_ session: URLSession, task: URLSessionTask, didSendBodyData bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64) {

    var uploadProgress:Float = Float(totalBytesSent) / Float(totalBytesExpectedToSend)
    imageUploadProgressView.progress = uploadProgress
    let progressPercent = Int(uploadProgress*100)
    progressLabel.text = "\(progressPercent)%"

}


func urlSessionDidFinishEvents(forBackgroundURLSession session: URLSession) {

    self.uploadButton.isEnabled = true
    print("Done")

}

}

The server-side PHP code is this:

<?
$target_dir = "uploadDestination/";
if(!file_exists($target_dir))
{
    mkdir($target_dir, 0777, true);
}

$target_dir = $target_dir . "/" . basename($_FILES["file"]["name"]);

if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_dir)) {
   echo json_encode([
      "Message" => "The file ". basename( $_FILES["file"]["name"]). " has been uploaded.",
       "Status" => "OK"
   ]);
 } else {
      echo json_encode([
          "Message" => "Sorry, there was an error uploading your file.",
          "Status" => "Error"
      ]);
 }

 ?>

The parent folder of my upload script as well as folder for uploading have set permission on 777

I Xcode Info.plist file I have added privacy statements:

Privacy - Photo Library Additions Usage Description
Privacy - Camera Usage Description
Inpour answered 19/6, 2018 at 12:13 Comment(4)
does your server support https?Magallanes
Hi Michael, yes it support it. It has valid SSL certificate created for my domain name from Let's Encrypt.Inpour
I had the same issue and for some reason it was do to the compressionQuality, change that from 1 to 0.9 for example and you should no longer get the errorMartyry
@Inpour could you find a solution for this?Cenis

© 2022 - 2024 — McMap. All rights reserved.