Swift:应用程序(_:打开URL:选项::)根本没有被调用

发布于 2025-02-12 16:07:20 字数 1704 浏览 0 评论 0原文

我正在尝试通过我构建的另一个应用程序打开一个应用程序中的辅助视图控制器(不是初始)。因此,我试图将不是最初的视图控制器的链接,但根本没有调用该功能。

我已经实现了openurlcontexts

import UIKit

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow?
    
    func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
        print(URLContexts)
    }

未被调用:

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

    print(url)
    let urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: true)
    let host = urlComponents?.host ?? ""
    print(host)

    if host == "accessPage" {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let vc = storyboard.instantiateViewController(withIdentifier: "jsonVC") as? JSONViewController
        window?.rootViewController = vc
    }
    return true
}

我添加了所有info.plist东西,并确保willfinishlaunchingwithoptions didfinishishlaunchingwithingwithoptions都返回true。

每当我从第一个应用程序打开第二个应用程序时,主视图控制器始终是要显示的应用程序,而不是我想要的一个应用程序,它是jsonviewController

这是第一个应用程序中的代码:

import UIKit

class ViewController: UIViewController {

    @IBAction func accessButtonPressed(_ sender: UIButton) {
        
        let application = UIApplication.shared
        
        let secondAppPath = "second://accessPage"
        guard let appURL = URL(string: secondAppPath) else { return }
        
        if application.canOpenURL(appURL) {
            application.open(appURL, options: [:], completionHandler: nil)
        }
    }
}

I'm trying to open a secondary view controller (not initial) in one app from a different app I built. So I'm trying to deep link to a view controller that's not the initial one, but the function is not being called at all.

I already implemented openURLContexts:

import UIKit

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow?
    
    func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
        print(URLContexts)
    }

Not being called:

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

    print(url)
    let urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: true)
    let host = urlComponents?.host ?? ""
    print(host)

    if host == "accessPage" {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let vc = storyboard.instantiateViewController(withIdentifier: "jsonVC") as? JSONViewController
        window?.rootViewController = vc
    }
    return true
}

I added all the info.plist stuff and made sure willFinishLaunchingWithOptions and didFinishLaunchingWithOptions both return true.

Whenever I open the second app from the first one the primary view controller is always the one being displayed, not the one I want which is the JSONViewController.

Here's the code from the first app:

import UIKit

class ViewController: UIViewController {

    @IBAction func accessButtonPressed(_ sender: UIButton) {
        
        let application = UIApplication.shared
        
        let secondAppPath = "second://accessPage"
        guard let appURL = URL(string: secondAppPath) else { return }
        
        if application.canOpenURL(appURL) {
            application.open(appURL, options: [:], completionHandler: nil)
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仄言 2025-02-19 16:07:20

评论SceneDelegate文件和删除applicationsCeneManifestinfo.plist.plist,重新启动Xcode中,它的工作正常。

Commented out the SceneDelegate file and deleted ApplicationSceneManifest in the info.plist, restarted Xcode and it worked just fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文