ios - 通过深层链接打开 View Controller 时导航栏消失

标签 ios swift xcode deep-linking

我使用了一个教程来实现应用程序委托(delegate)中的方法,以从 safari 的 HTML 按钮打开特定的 View Controller 。我的应用程序委托(delegate)如下所示:

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>

但是当我的应用程序主页打开时,导航栏消失了,用户被卡住并且无法访问菜单按钮或后退按钮(如果是内部 View Controller )。

深层链接前后主视图 Controller 的屏幕截图:

when navigation disappears

how it should be

最佳答案

请按如下方式更新您的代码..您应该将导航 Controller 作为窗口 Root View Controller ..

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

关于ios - 通过深层链接打开 View Controller 时导航栏消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50117172/

相关文章:

ios - 带有原型(prototype)单元的 tableView 不重新加载数据

ios - Swift 4 中 3 个线程的同步

swift - VC 推送时线程 1 出错?

ios - 更改导航栏标题字体 - swift

c++ - 无法创建addressbook.pb.h对象(从protoc构建)

cocoa - 如何将 View 连接到图形上下文?

c++ - Xcode 4.6/Clang 4.2 找不到 C++11 header

iphone - 从 AFImageRequestOperation 中的成功 block 返回图像

ios - 将 Realm 添加到 iOS 项目时出错

iphone - 如何在UILabel iPhone sdk中提供跑马灯效果?