Prevent user from leaving website in Swift - ios

I want to create a WebView in an iPad-App, that displays a website. I want to prevent the user to click on links that link to other websites (e.g. Facebook, Twitter, ...) but he should be allowed to move around on the current website freely.
How can I achieve this?
I tried with:
func webView(WebViewNews: UIWebView!, shouldStartLoadWithRequest request: NSURLRequest!, navigationType: UIWebViewNavigationType) -> Bool {
let newWebsite:String = (request.URL?.path)!
print(newWebsite)
if newWebsite.rangeOfString("float-schwabing") != nil{
return true;
} else {
return false;
}
}
It didn't work though. I can still access other websites.
It would be awesome, if somebody could help me with this.

As you have done you should be able to navigate within your website, you might have some internal links. Try the below code instead of how you have done it today:
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
if String(request.URL!).rangeOfString("YOUR WEBSITE") != nil{
return true
}
else{
return false
}
}
You basically check if the request url contains your websites name, if it does you might want to open that link otherwise you donĀ“t.

The problem should be in your if statement, did you try to return false in all case yet? just to see if it worked or not?
func webView(WebViewNews: UIWebView!, shouldStartLoadWithRequest request: NSURLRequest!, navigationType: UIWebViewNavigationType) -> Bool {
return false
}
And... it's rare but why not, did you add delegate to your webView?

Related

Disable UIWebView for opening links to redirect to apps installed on my iPhone

When I browse some link in my app(in UIWebView), it opens the that link's app installed in my device. How can I restrict it to open external app and load the same URL in my UIWebView.
Maybe someone will find it useful:
func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
if navigationType == .linkClicked, let req = request.urlRequest {
webView.loadRequest(req)
return false
}
return true
}
Thus, I block the opening of the link in the side application, such as YouTube app, but open it in the UIWebView.
You can use func webView(UIWebView, shouldStartLoadWith: URLRequest, navigationType: UIWebViewNavigationType) in UIWebViewDelegate to do that. For example:
func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
let urlString = request.url?.absoluteString ?? ""
if urlString == <your app link on webview> {
return false
}
return true
}
You now just replace <your app link on webview> with your actual link that you don't want web view to navigate to

disable a specific link on ios uiwebview

How can I disable a specific link on iOS uiwebview ?
The page loaded in the webview can have thousand urls but i want to disable one url.
I am sensing i have to use this method for the work. But can't figure it out.
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
}
I'm working with objective-c. Sorry if my swift code is wrong. I hope you understand with the logic.
Try it.
func webView(_ webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
let surl = request.url?.absoluteString
if surl == "http://disableurl.com" {
return false;
}
return true;
}

Call only first request UIWebView

I have one url request let say
let url = URL(string: "https://www.google.com/share?referCode=RSJDofpeW")
and we want to open this url in WebView and this link is also associated with the universal links. So opening this url will open the installed application but i want to load the page in UIWebView. So i checked the delegates and found that at first time it calls it is the above url then next time it will add scheme and appstore redirection.
func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
log.verbose(webView)
log.verbose(request)
log.error(navigationType.rawValue)
if request.url?.scheme == "itms-appss" || request.url?.scheme == "googleApp"{
return false
}
return true
}
So to overcome the issue of not loading the page in Web View i did the code like above so when scheme is itms-appss or googleApp it will not load the request but for the first time when it was correct url it should load that page but that is not opened.
//Check
var isUrlLoaded : Bool = false;
//delegate functions
func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
//check is the url is loaded for first time or not
if isUrlLoaded != true{
if request.url?.scheme == "itms-appss" || request.url?.scheme == "googleApp"{
return true
}
}
return false
}
func webViewDidFinishLoad(_ webView: UIWebView) {
if webView.request?.url?.scheme == "itms-appss" || webView.request?.url?.scheme == "googleApp"{
// is url loaded
isUrlLoaded = true;
}
}

How to open all links, besides some, in Safari with UIWebView?

I obtained the following code from here to open all other links that do not match my domain in Safari:
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
if navigationType == UIWebViewNavigationType.LinkClicked {
UIApplication.sharedApplication().openURL(request.URL!)
return false
}
return true
}
Although how can I allow another specified domain to be opened within my UIWebView instead of Safari, such as paypal.com?
You can store a list of allowed URLs and filter on the host name of the request URL. If the host matches one of the allowed URLs then return true to allow the URL to load in the web view. Otherwise use UIApplication.openURL() to open the URL in Safari.
For example:
let safeList = [ "paypal.com", "google.com" ]
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
if navigationType == UIWebViewNavigationType.LinkClicked {
if let host = request.URL?.host where safeList.contains(host) {
return true // Open in web view
}
UIApplication.sharedApplication().openURL(request.URL!)
return false
}
return true
}

Scale web page on link clicked

So I've created an app that would open an HTML page with some text and links on it. But if I click on a link I the page that would open after will not scale (obviously).
I know that I can scale the WebView in my first ViewController but in that case it will be hard to read my initial HTML page.
I've tried sevral methods:
scale my webView on link clicked:
if navigationType == UIWebViewNavigationType.LinkClicked {
UIApplication.sharedApplication().openURL(request.URL!)
myWebView.frame = UIScreen.mainScreen().bounds
myWebView.center = self.view.center
myWebView.scalesPageToFit = true
}
return true
Or like that:
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
switch navigationType {
case .LinkClicked:
// Open links in Safari
UIApplication.sharedApplication().openURL(request.URL!)
myWebView.scalesPageToFit = true
return false
default:
// Handle other navigation types...
return true
}
}
But to no succsess.
After that I've tried to set up a segue to my second ViewController in case link is clicked but the result was still the same.
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
if navigationType == UIWebViewNavigationType.LinkClicked {
let about = self.storyboard?.instantiateViewControllerWithIdentifier("openlink") as! dossierLink
self.navigationController?.pushViewController(about, animated: true)
}
return true
}
Can someone help me out on that one? Thank you!
My recommendtion would be to only load the content of next html page or url
webView.loadDataWithBaseURL(null,urlcontent, "UTF-8", null)
There are many ways to convert url to string content like Android Read contents of a URL (content missing after in result)

Resources