ios - 应用程序 openURL 提供无效的 url
问题描述
功能
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
}
}
}
推荐阅读
- mysql - 如何连接三个不同的表,其中一列是每个组件的总和?
- binary - 为不同的十进制值形成一个 16 位有符号二进制补码
- r - 如何按字母文件名导入数据文件,最好使用 tidyverse
- json - 如何使用 Angular 模型解析具有通用名称的 JSON 对象?
- jupyter-notebook - 我不知道是什么问题
- visual-studio-code - 如何在 Visual Studio Code 的突出显示匹配标记扩展中更改匹配标记的下划线?
- python - 用于捕获两个子字符串之间的字符串的正则表达式
- javascript - Vimeo 自动播放和透明度
- mongodb - 何时将集合分解为更小的集合
- django - ModuleNotFoundError:没有名为“rest_framework”的模块。我该如何解决这个错误?