ios - 尝试重定向到 WKWebView 中的 url

标签 ios swift

我是 iOS 开发新手,当通过自定义 URL (foo://) 打开应用程序时,我尝试将应用程序重定向到特定网站。使用此 URL 将我带到 AppDelegate 中的此处。

func application(app: UIApplication, openURL url: NSURL, options:[String: AnyObject]) -> Bool {
    let viewController = ViewController();
    viewController.loadRequestExt()
    return true
}   

调用 ViewController 中的 loadRequestExt() 函数

func loadRequestExt() {
    super.viewDidLoad()
    self.loadView()
    let url = NSURL (string: "http://www.google.com");
    let requestObj = NSURLRequest(URL: url!);
    self.webView!.loadRequest(requestObj);
    self.loadView()
}

override func loadView() {
    print("loadView")
    super.loadView()

    let contentController = WKUserContentController();
    contentController.addScriptMessageHandler(
        self,
        name: "iOS"
    )

    let config = WKWebViewConfiguration()
    config.userContentController = contentController

    self.webView = WKWebView(
        frame: self.view.frame,
        configuration: config
    )
    self.view = self.webView
    self.webView?.navigationDelegate = self

    // configure the activity view
    activityView.center = self.view.center
    activityView.hidesWhenStopped = true
    activityView.startAnimating()

    self.view.addSubview(activityView)
}

override func viewDidLoad() {
    print("viewDidLoad")

    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(registerPushToken), name: "didRegisterForRemoteNotificationsWithDeviceToken", object: nil)

    let url = NSURL (string: "http://charlycares-frontend.herokuapp.com/#/app/login");

    let requestObj = NSURLRequest(URL: url!);
    self.webView!.loadRequest(requestObj);
}

它进入 loadView 并执行 loadRequest,但它仍保留在同一页面上。

最佳答案

您必须将您的viewcontroller设置为appdelegate中窗口属性的rootviewcontroller(我想您没有使用 Storyboard,在这种情况下,您必须将 viewcontroller 设置为初始 viewcontroller 并且不要向 appdelegate 添加代码):

func application(app: UIApplication, openURL url: NSURL, options:[String: AnyObject]) -> Bool {

    self.window = UIWindow(frame: UIScreen.main.bounds)
    self.window?.rootViewController = ViewController()
    self.window?.makeKeyAndVisible()    

    return true
}

在您的 ViewController viewDidLoad 中,调用您的 loadRequest 函数:

self.loadRequestExt()

关于ios - 尝试重定向到 WKWebView 中的 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41870996/

相关文章:

iphone - iOS 图片上传,ASIHttpRequest 以外的第三方 api

ios - 为客户上传或只是存档应用程序

objective-c - 不能给 View 添加阴影,CALayer

swift - swift 中的模块是什么意思?

swift - bannerViewDidLoadAd 未被调用

ios - 我的 `import` 中这个额外的 `NSManagedObjectSubClass` 关键字是什么?

ios - 正确调整 MapKit map 注释图钉的大小

ios - 未调用 iOS 回调的 FCM 推送

swift - 如果你不返回回调会发生什么

ios - 自定义 UISwitch 兼容所有版本的 iOS