我是 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/