Ios 正在尝试重定向到WKWebView中的url
我是iOS开发新手,当应用程序通过自定义url(foo://)打开时,我尝试将我的应用程序重定向到特定网站。使用此URL可将我带到AppDelegate中的此处Ios 正在尝试重定向到WKWebView中的url,ios,swift,Ios,Swift,我是iOS开发新手,当应用程序通过自定义url(foo://)打开时,我尝试将我的应用程序重定向到特定网站。使用此URL可将我带到AppDelegate中的此处 func application(app: UIApplication, openURL url: NSURL, options:[String: AnyObject]) -> Bool { let viewController = ViewController(); viewController.loadRequ
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
,但是它保持在同一页面上 您必须将appdelegate
中窗口属性的viewcontroller
设置为rootviewcontroller
(我假设您没有使用情节提要,在这种情况下,您必须将viewcontroller
设置为初始viewcontroller
,并且不要将代码添加到appdelegate
):
在ViewController
viewDidLoad
中,调用loadRequest
函数:
self.loadRequestExt()
self.loadRequestExt()