Theme
Theme

Reputation: 417

application openURL giving invalid urls

The function

func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {

is still called like normal, but when I do:

func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
    do {
        let contentsOfFile = try NSString(contentsOfFile: url.path, encoding: String.Encoding.utf8.rawValue)
        Swift.print("COF \(contentsOfFile)")
    } catch let error {
        Swift.print("error \(error)")
    }
    ...
}

I get the error "The file “____” couldn’t be opened because there is no such file."

This used to work in iOS 12. I'm not doing anything with SceneDelegate or anything, so I'm not sure why it's giving me invalid URLs now.

Update: if I drag a file from my Dropbox onto the iOS Simulator, it works. If I drag a file from anywhere else on my computer, it doesn't work.

Upvotes: 1

Views: 272

Answers (2)

Theme
Theme

Reputation: 417

Well, I guess it's just something to do with the simulator. If I drag a file from my Dropbox onto the iOS Simulator, it works. If I drag a file from anywhere else on my computer onto the simulator, it doesn't work. But it still works fine on a real device, as far as I can tell.

Upvotes: 0

The function open url can be used to read the files like below ,

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        let stringPath = Bundle.main.path(forResource: "bbc", ofType: "json") // File name 
        let fileUrl = URL(fileURLWithPath: stringPath!)
        let canOpenBool =  application(UIApplication.shared, open: fileUrl)
        print(canOpenBool)
    }

    func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
        do {
            let contentsOfFile = try NSString(contentsOfFile: url.path, encoding: String.Encoding.utf8.rawValue)
            Swift.print("COF \(contentsOfFile)")
             return true
        } catch let error {
            Swift.print("error \(error)")
             return false
        }
    }
}

Upvotes: 1

Related Questions