有没有办法让应用只加载某些网址?我认为在App Transport Security设置中有类似Exception Domains的内容,除了这不是允许来自某个URL的任意加载,而是仅允许某些URL并阻止所有其他URL,无论是否为HTTPS / HTTP。感谢
答案 0 :(得分:3)
您可以在AppDelegate中使用类似的内容:
optional func application(_ app: UIApplication,
openURL url: NSURL,
options options: [String : AnyObject]) -> Bool
{
//Return true or false depending on if you want
//this application to open the URL.
}
在评论中,您提到您在单个UIWebView中使用此全部内容。在这种情况下,您可以执行以下操作:
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
let URL = request.URL
return handleRequest(URL)
}
@available(iOS 8.0, *)
func webView(webView: WKWebView, decidePolicyForNavigationAction navigationAction: WKNavigationAction, decisionHandler: (WKNavigationActionPolicy) -> Void) {
let request = navigationAction.request;
let url = request.URL
if(handleRequest(url))
{
decisionHandler(WKNavigationActionPolicy.Allow)
}
else
{
decisionHandler(WKNavigationActionPolicy.Cancel)
}
}
这段代码处理UIWebView和WKWebView。