应用程序openURL提供无效的URL

问题描述 投票:0回答:2

功能

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

仍然被称为正常,但是当我这样做时:

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)")
    }
    ...
}

我收到错误消息“由于没有这样的文件而无法打开文件“ ____”。”

过去曾在iOS 12中运行。我没有使用SceneDelegate做任何事情,所以我不确定为什么现在给我无效的URL。

更新:如果我将文件从我的Dropbox拖到iOS模拟器上,它将起作用。如果我从计算机上的其他任何位置拖动文件,则无法使用。

ios swift
2个回答
1
投票

功能open url可用于读取如下文件,

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
        }
    }
}

0
投票

嗯,我想这与模拟器有关。如果我将文件从我的Dropbox拖到iOS模拟器上,它将起作用。如果我将文件从计算机上的其他任何地方拖到模拟器上,它将无法正常工作。但据我所知,它仍然可以在真实设备上正常工作。

© www.soinside.com 2019 - 2024. All rights reserved.