我的应用程序中有一个WKWebview并加载本地.html文件。 我想使用深层链接重定向特定页面。 我不知道该怎么做。 你们有什么想法吗?请告诉我一个解决方案?
深层链接示例:
HELLOAPP://index.html#/article/list/123345
码:
@property (strong, nonatomic) IBOutlet WKWebView *webView;
- (void)viewDidLoad {
self.webView = [self.webView initWithFrame:[UIScreen mainScreen].bounds configuration:[self createWKWebApp]];
[self.webView setNavigationDelegate:self];
[self.webView setUIDelegate:self];
[self.webView.configuration.preferences setValue:@YES forKey:@"allowFileAccessFromFileURLs"];
self.webView.scrollView.bounces = NO;
[self.webView setContentMode:UIViewContentModeScaleAspectFit];
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html" inDirectory:@"www/phoneApp"]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:180];
[request setHTTPMethod:@"POST"];
[self.webView loadRequest:request];
}
你必须实现decidePolicyForNavigationAction
的方法WKNavigationDelegate
。
- (void) webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
if ([navigationAction.request.URL.scheme isEqualToString:@"helloAPP"]){
// Open app page based on the url
decisionHandler(WKNavigationActionPolicyCancel);
return;
}
decisionHandler(WKNavigationActionPolicyAllow);
}
你应该在你的班级实施WKNavigationDelegate
。
这是一些有用的代表。 (代表是用Swift编写的。目标C中有相同的代表。)
// MARK:- WebKit Navigation Delegate
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
print("didstart called")
//Here you can open url.
UIApplication.shared.open((webView.url)!, options: [:], completionHandler: nil)
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
print("didFinish navigation: \(navigation)")
}
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
decisionHandler(.allow)
}
func webView(_ webView: WKWebView, didReceiveServerRedirectForProvisionalNavigation navigation: WKNavigation!) {
print("didReceiveServerRedirectForProvisionalNavigation navigation: \(navigation)")
}
func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
print("didCommit: \(navigation)")
}
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
print("loading Error: \(error)")
}
您可以在这些函数中编写逻辑。
要了解Visit here。