我正在尝试实现深层链接来导航到一个应用程序上的帖子,这是一个旧的项目,所以我不得不添加SceneDelegate
类。深层链接的实现只有在应用程序处于活动状态或后台时才能工作。如果应用程序尚未加载,深层链接将无法工作。我看过很多关于这方面的帖子和教程,但还没有找到原因,有人遇到过类似的问题吗?
在AppDelegate
类中,我添加了实现来处理以下函数的链接:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {}
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {}
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {}
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {}
在SceneDelegate
中,我在以下函数中实现了对链接的处理:
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {}
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {}
func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {}
这些函数的实现如下所示:
let navigator = Navigator()
navigator.getDesination(for: url)
func getDesination(for url: URL){
let destination = Destination(for: url)
let ivc = InstantiateViewController()
switch destination {
case .post(let postID):
ivc.openPostVC(id: postID, showComment: true, commentID: nil)
case .user(let userID):
ivc.openProfileVC(userID: userID)
default:
break
}
}
enum Destination {
case post(Int)
case user(Int)
case feed(String)
case store
case safari
init(for url: URL){
if(url.pathComponents[1] == "p"){
self = .post(Int(url.pathComponents[2])!)
} else if(url.pathComponents[1] == "user") {
self = .user(Int(url.pathComponents[2])!)
} else if(url.pathComponents[1] == "store") {
self = .store
} else if(url.pathComponents[1] == "s") {
self = .feed(url.pathComponents[2])
} else {
self = .safari
}
}
}
func openProfileVC(userID: Int){
let service = UserPool.shared.request(for: userID)
let storyboard = UIStoryboard(name: "Profile", bundle: nil)
let profileVC = storyboard.instantiateViewController(withIdentifier: "ProfileView") as! ProfileViewController
profileVC.userService = service
profileVC.shouldNavigateToHome = true
profileVC.shouldNavigateToHomeAction = {
self.loadMainStoryboard()
}
let navigationVC = UINavigationController(rootViewController: profileVC)
navigationVC.view.backgroundColor = .white
if #available(iOS 13.0, *) {
guard let sceneDelegate = UIApplication.shared.connectedScenes.first?.delegate as? SceneDelegate else {return}
sceneDelegate.window?.rootViewController = navigationVC
sceneDelegate.window?.makeKeyAndVisible()
} else {
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
appDelegate.window?.rootViewController = navigationVC
appDelegate.window?.makeKeyAndVisible()
}
}
网站的app-site-assocation文件如下所示,并已在Xcode中添加了关联的域:
{"applinks":{"apps":[],"details":[{"appID":"{my ID}","paths":["*"]}]},"webcredentials":{"apps":["{my ID}"]}}
4条答案
按热度按时间9q78igpj1#
在iOS 13及更高版本中,通过场景代理,您的应用可以在启动时观察传入的通用链接事件,如下所示:
如果应用程序已在运行,请使用以下命令:
(我有一个非常简单的downloadable demo app,它证明了它确实有效。我不理解它不起作用的说法;也许问题在于不了解如何测试。)
o8x7eapl2#
我没有找到答案,所以我决定解决这个问题。我只恢复到
AppDelegate
,在这种情况下,只有当应用程序处于活动状态或后台时,深层链接才能工作。为了解决这个问题,我决定将URL存储在UserDefaults
中。因此,我在didFinishLaunchingWithOptions
函数中添加了以下内容:下面是我创建的
UserDefaults
扩展:最后,在初始视图控制器的
viewDidLoad
函数中,我处理链接:其中
Navigator
类决定将哪个视图控制器压入导航堆栈在这之后一切都很完美
tp5buhyn3#
epfja78i4#
来自苹果文档:
如果您的应用已选择进入Scenes,并且您的应用未运行,则系统会在启动后将URL传递给scene(:willConnectTo:options:)委托方法,并在应用运行或挂起在内存中时打开URL时将URL传递给scene(:openURLContexts:)。
完整示例:
应用程序终止时在场景代理中:
以及当应用程序是后台或前台时: