通过深层链接打开视图控制器时导航栏消失

时间:2018-05-01 13:15:37

标签: ios swift xcode deep-linking

我已经使用教程在app delegate中实现一个方法,从safari的HTML按钮打开特定的视图控制器。我的app委托看起来像这样:

func application(_ app: UIApplication, open URL: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
    print("url \(url)")
    print("url host :\(url.host!)")
    print("url path :\(url.path)")


    let urlPath : String = (url.path as String?)!
    let urlHost : String = (url.host as String?)!
    let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

    if(urlHost != "mywebsite.ir")
    {
        print("Host is not correct")
        return false
    }

    if(urlPath == "/index"){


        let innerPage: ViewController = mainStoryboard.instantiateViewController(withIdentifier: "HomeViewController") as! ViewController
        self.window?.rootViewController = innerPage
    }
    self.window?.makeKeyAndVisible()
    return true
}

我的HTML按钮引用是:

<a href= "myapp://mywebsite.ir/index">open app</a>

但是当我的应用主页打开时,导航栏消失了,用户卡住了,无法访问菜单按钮或后退按钮(如果是内部视图控制器)。

深层链接之前和之后的主视图控制器的屏幕截图:

when navigation disappears

how it should be

2 个答案:

答案 0 :(得分:0)

请更新您的代码,如下所示..您应该将导航控制器作为窗口根视图控制器..

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
print("url \(url)")
print("url host :\(url.host!)")
print("url path :\(url.path)")


let urlPath : String = (url.path as String?)!
let urlHost : String = (url.host as String?)!
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

if(urlHost != "mywebsite.ir")
{
    print("Host is not correct")
    return false
}

if(urlPath == "/index"){


    let innerPage: ViewController = mainStoryboard.instantiateViewController(withIdentifier: "HomeViewController") as! ViewController
let nav = UINavigationController.init(rootViewController: innerPage)
    self.window?.rootViewController = nav
}
self.window?.makeKeyAndVisible()
return true

答案 1 :(得分:0)

HomeViewController是最初的ViewController吗?如果没有,您可以使用导航控制器调用应该是视图控制器的初始视图控制器。

尝试这样做: mainStoryboard.instantiateInitialViewController()

如果行为仍然存在,您可以在代码

上嵌入NavigationController
let viewController = mainStoryboard.instantiateViewController(withIdentifier: "HomeViewController") as! ViewController
let navigationController = UINavigationController(rootViewController: viewController)
self.window?.rootViewController = navigationController

你也不需要调用window.makeKeyAndVisible()