功能
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模拟器上,它将起作用。如果我从计算机上的其他任何位置拖动文件,则无法使用。
功能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
}
}
}
嗯,我想这与模拟器有关。如果我将文件从我的Dropbox拖到iOS模拟器上,它将起作用。如果我将文件从计算机上的其他任何地方拖到模拟器上,它将无法正常工作。但据我所知,它仍然可以在真实设备上正常工作。