0

I would like my application to download a file with custom extension from the Internet and save it. I have written the following code:

import UIKit
class ViewController: UIViewController {

let url_to_my_file: URL = (URL(string: "https://my-url.com/file1.tcr"))!

override func viewDidLoad() {
    super.viewDidLoad()

    Downloader.load(url: url_to_my_file) {
        print("done")
    }
  }
}

====

import Foundation

class Downloader {

static let instanse = Downloader()

class func load(url: URL, completion: @escaping () -> ()) {

    let documentsURL = NSURL(fileURLWithPath: NSSearchPathForDirectoriesInDomains( .documentDirectory, .userDomainMask, true).first!, isDirectory: true)
    let localUrl = documentsURL.appendingPathComponent("file.tcr")!

    let sessionConfig = URLSessionConfiguration.default
    let session = URLSession(configuration: sessionConfig)
    var request = try! URLRequest(url: url)
    request.httpMethod = "GET"

    let task = session.downloadTask(with: request) { (tempLocalUrl, response, error) in
        if let tempLocalUrl = tempLocalUrl, error == nil {
            // Success
            if let statusCode = (response as? HTTPURLResponse)?.statusCode {
                print("Success: \(statusCode)")
            }

            do {
                try FileManager.default.copyItem(at: tempLocalUrl, to: localUrl)
                completion()
            } catch (let writeError) {
                print("error writing file \(localUrl) : \(writeError)")
            }

        } else {
            print("Failure: %@", error?.localizedDescription);
        }
    }
    task.resume()
  }
}

However, when i run it, i get messages in console that my code does not conform to ATS policy.

2018-07-12 21:44:31.965978+0300 dl_test[5294:4397157] App Transport Security
has blocked a cleartext HTTP (http://) resource load since it is insecure.
Temporary exceptions can be configured via your app's Info.plist file.
2018-07-12 21:44:31.966028+0300 dl_test[5294:4397157] Cannot start load of Task
<52F90327-B8A3-436D-B68C-716EBACFF0BF>.<1> since it does not conform to ATS
policy
2018-07-12 21:44:31.966121+0300 dl_test[5294:4397159] Task <52F90327-B8A3-436D-
B68C-716EBACFF0BF>.<1> finished with error - code: -1022
Failure: %@ Optional("The resource could not be loaded because the App
Transport Security policy requires the use of a secure connection.")

Weird thing, if i put let url_to_my_file: URL = (URL(string: "https://my-url.com/file2.txt"))! instead of let url_to_my_file: URL = (URL(string: "https://my-url.com/file1.tcr"))!, everything works without mistakes, file is saved and dowloaded. I tried swapping extension of the file1 into .txt, but it didn't change anything.

I believed that all i needed to do to conform to that ATS policy is make sure that I use HTTPS, and not HTTP links. Are there any other specific requirements for certain data types, or am i making some other mistake in my code?

I would like to conform to the policy, without using workarounds like editing info.plist. Is there a way to do it?

p.s. usage of Alamofire or any other pods for my project is not allowed, unfortunately

p.p.s i am aware about this topic Transport security has blocked a cleartext HTTP, however the responders offer workaround via editing inof.plist and that's not the solution that i'm looking for

Alice A.
  • 85
  • 1
  • 7
  • i am aware about this topic https://stackoverflow.com/questions/31254725/transport-security-has-blocked-a-cleartext-http, however the responders offer workaround via editing inof.plist and that's not the solution that i'm looking for – Alice A. Jul 12 '18 at 19:19
  • 1
    Editing `info.plist` is not a workaround, it's the proper (recommended) solution. – vadian Jul 12 '18 at 19:30
  • as i understood from comments in that topic, it's workaround. (or i'm getting totally confused now) will such application be accepted to Appstore later? and is there no another way at all to download a file without editing info.plist then? – Alice A. Jul 12 '18 at 19:33

0 Answers0