Is there a way to allow an app to load only certain URLs? I was thinking there was something like Exception Domains in App Transport Security settings, except this is not about allowing arbitrary loads from certain URL, but rather to allow only certain URLs and block all others, whether or not HTTPS/HTTP. Thanks
Asked
Active
Viewed 1,567 times
1
-
How are these URLs being loaded? – Phillip Mills Jul 15 '16 at 19:55
-
I have a webview inside the app leading to www.mysite.com and I never want the user to browse away from it by following links. It is a security risk. – KML Jul 15 '16 at 19:56
-
1If it's a `UIWebView`, I believe your delegate gets called at the start of a page load (`webView:shouldStartLoadWithRequest:navigationType:`). You can return whether you want the request to execute. – Phillip Mills Jul 15 '16 at 20:01
-
For WKWebViews, there's a similar WKNavigationDelegate that includes `webView(_:decidePolicyForNavigationAction:decisionHandler:)`. – Rob Napier Jul 15 '16 at 20:04
-
1http://stackoverflow.com/a/36231713/2303865 – Leo Dabus Jul 15 '16 at 22:43
1 Answers
3
You can use something like this in your 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.
}
In the comments you mention that you are using this all in a single UIWebView. In that case you can do the following:
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)
}
}
That bit of code handles both UIWebView and WKWebView.

Putz1103
- 6,211
- 1
- 18
- 25
-
I did not specify that I was looking for a swift solution and when I converted your answer it would not compile: This is what I got but it does not make any sense – KML Jul 15 '16 at 20:03
-
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String, annotation: AnyObject) -> Bool { var params: String = url.absoluteString().substringFromIndex(url.absoluteString().startIndex.advancedBy(url.absoluteString().rangeOfString("://").location + 3)) //Return true or false from this depending on if you want to load the page or not. return true } – KML Jul 15 '16 at 20:03
-
So there is no app wide wide implementation for Swift ? I really dont want the app to have access to any other sites. – KML Jul 15 '16 at 20:07
-
ok, and if I want to permit 2 or 3 URLs, is that possible ? Sorry, I am changing my question.. – KML Jul 15 '16 at 20:17
-
1Those WebView delegates let you do anything you want. Search the URL it provides to you and decide whether you want to allow going to that or not. Problem solved. – Putz1103 Jul 15 '16 at 20:18