如何解决Swift Scene Delegate在首次启动时不会运行代码
使用UTI导入文件打开应用程序时,我有一个在Scene Delegate中运行的代码。代码可以正常运行,但是必须运行该应用程序并加载视图控制器才能执行该代码,我该怎么办,当通过单击文件启动该应用程序时也可以运行该代码?
SceneDelegate.swift
class SceneDelegate: UIResponder,UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene,willConnectTo session: UISceneSession,options connectionOptions: UIScene.ConnectionOptions) {
guard let _ = (scene as? UIWindowScene) else { return }
}
func scene(_ scene: UIScene,openURLContexts URLContexts: Set<UIOpenURLContext>) {
if let url = URLContexts.first?.url {
// Handle URL
let needTo = url.startAccessingSecurityScopedResource()
do {
let data = try Data(contentsOf: url)
let contents = String(
data: data,encoding: String.Encoding.utf8
)
if let x = contents
{
MatchDecoder.decodeText(content: x)
}
} catch(let error) { print(error) }
if needTo {
url.stopAccessingSecurityScopedResource()
}
}
}
//....
解决方法
设法修复它,对于任何有相同问题的人,添加它,现在URL也将在首次启动时运行!
func scene(_ scene: UIScene,willConnectTo session: UISceneSession,options connectionOptions: UIScene.ConnectionOptions) {
guard let _ = (scene as? UIWindowScene) else { return }
self.scene(scene,openURLContexts: connectionOptions.urlContexts)
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。