Swift How to make links open in Webview? - ios

I can't find solution for this. I hope You'll help me.
I want buttons with hyperlinks to open websites in webview on second screen. Hyperlink opens in safari and I want it to open on second screen in webview.
Thank you

You can use SFSafariViewController to open link in your application, here is the code for that
First you need to import SafariServices
import SafariServices
then on the button action, you can open SFSafariViewController
For swift 3.0
let svc = SFSafariViewController(url: URL(string:"http://stackoverflow.com")!)
self.present(svc, animated: true, completion: nil)

Swift 4.1 and Swift 5. If there are some links inside the app then it will open in safari, So to opens the links within same webview implement following navigationDelegate fo WKWebView.
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: #escaping (WKNavigationActionPolicy) -> Void) {
if navigationAction.navigationType == WKNavigationType.linkActivated {
webView.load(navigationAction.request)
decisionHandler(.cancel)
return
}
decisionHandler(.allow)
}

Related

Prevent WKWebView From Being Able To Navigate To Specific Pages On A Website (Swift 5)

I am trying to prevent users from being able to navigate to different pages on a website by using WKNavigationDelegate and implementing the function below:
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: #escaping (WKNavigationActionPolicy) -> Void) {
if let urlStr = navigationAction.request.url?.absoluteString {
if urlStr == "https://random.ca/#/profile" {
decisionHandler(.cancel)
return
}
}
decisionHandler(.allow)
}
This function seems to only be called when I first load the home page of the website and when I navigate to other pages inside of this site it is not triggered and so I cannot prevent the user from navigating to those pages.
Any advice would be greatly appreciated.
I found something
Just before calling webView.load()
add this
webView.isUserInteractionEnabled = false
check this Disable links in WKWebView? for more details

Disable Youtube app redirect from WKWebview in swift

I developed simple web browser with WKWebView in Swift.
When I click Youtube link, Youtube app is auto launched.
I hope to play Youtube video inside my web browser.
I don't need to launch Youtube app.
Please help me.
Here are my sample code.
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let webViewConfiguration = WKWebViewConfiguration()
webViewConfiguration.allowsInlineMediaPlayback = true
wkWebView.configuration.allowsInlineMediaPlayback = true
wkWebView.navigationDelegate = self
let myURL = URL(string: "https://www.google.com")
let youtubeRequest = URLRequest(url: myURL!)
wkWebView.load(youtubeRequest)
}
Yes, you can do this.
First you need to set up a WKNavigationDelegate in your webview, then in webview:decidePolicyFor method cancel any link activated youtube requests, and force it to load within your webview.
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: #escaping (WKNavigationActionPolicy) -> Void) {
if navigationAction.navigationType == .linkActivated && navigationAction.request.url?.host?.hasSuffix("youtube.com") {
decisionHandler(.cancel)
DispatchQueue.main.async {
webView.load(navigationAction.request)
}
} else {
decisionHandler(.allow)
}
}
Unfortunately you can't do that programatically. Following are the steps to achieve something like you want manually.
Open youtube app from Home screen
Tap on your profile picture on the upper right corner.
Go to Settings and tap on Google app Settings
Check the Safari Option.

How to open social link by clicking on button inside WKWebView

There is a social link button inside WKWebView. When I click on the button nothing happens.
Could you please tell me how to open the link by clicking on the button?
Thanks in advance.
what link u want to open change "facebook","twitter" and you can add more links also
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: #escaping (WKNavigationActionPolicy) -> Void) {
if navigationAction.navigationType == .linkActivated {
if let url = navigationAction.request.url ,UIApplication.shared.canOpenURL(url) {
// let urlString = try! String(contentsOf: url)
if (url.absoluteString.range(of: "facebook.com") != nil || url.absoluteString.range(of: "twitter.com") != nil){
//UIApplication.shared.open(url)
print("Redirected to browser. No need to open it locally")
decisionHandler(.cancel)
}else{
print("Open it locally")
decisionHandler(.allow)
}
}
You need a on click listener.
You can use WKWebView to display interactive web content in your app. Ideal for displaying HTML markup, styled text content, or complete web pages. After that, you need to handle the Social button you're calling and the data submitted if any...
Responding To WKWebView Events With WKNavigationDelegate
A WKWebView web view has two main delegate protocols and properties:
navigationDelegate of type WKNavigationDelegate, which responds to
navigation events
uiDelegate of type WKUIDelegate, which responds to user
interaction events
Of these two delegate protocols, the WKNavigationDelegate is probably used most frequently. So, let’s hook into some page navigation events! We’ll do so with these delegate functions:
webView(_:didStartProvisionalNavigation:)
webView(_:didCommit:)
webView(_:didFinish:)
webView(_:didFail:withError:)
webView(_:didFailProvisionalNavigation:withError:)
First, let’s adopt the protocol and set the webView delegate. Here’s how:
Add the WKNavigationDelegate protocol to your view controller’s class declaration, like: class WebViewController: UIViewController, WKNavigationDelegate
Set the navigationDelegate property of webView to self, before loading the web request, like: webView?.navigationDelegate = self
Next, implement the five delegate functions described earlier. Set their function bodies to print(#function), so we can see the order in which the functions are called.
Like this:
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!)
{
print(#function)
}

WKWebView not loading content from iframe on a webpage

I have a WKWebView where a user goes through several pages to reach a page which contains a video from lesson.ly.
The problem is that the video will not show up at all within the webview, there is just a blank gap in the page where the video would be. However, if I open chrome on iOS, it loads just fine.
I've pinpointed the issue by debugging the HTML code. Here is a screen shot of the HTML on the webview:
And Here's a picture of the HTML on Chrome for Mac:
If you noticed, there is nothing within the iframe for the webview. I have no idea why it's not loading the data. I have enabled 'Allows Arbitrary Loads' for App Transport Security to no avail.
Any help appreciated, thanks.
Alternatively, instead of allowing specific URLs, you can check if request in question is targeting main frame or not:
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: #escaping (WKNavigationActionPolicy) -> Swift.Void) {
// Allow navigation for requests loading external web content resources.
guard navigationAction.targetFrame?.isMainFrame != false else {
decisionHandler(.allow)
return
}
...
}
Figured out the problem. It was all in the delegate method webView:decidePolicyForNavigationAction:decisionHandler: and because the Lessonly videos come from a different source (fast.wistia.net) I had to add that url expliclity.
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: #escaping (WKNavigationActionPolicy) -> Void) {
guard let requestURL = navigationAction.request.url?.absoluteString else { return }
if requestURL.tc_contains("mydomain.lesson.ly") || requestURL.tc_contains("fast.wistia.net"){
decisionHandler(.allow)
}
else {
decisionHandler(.cancel)
}
}

Restricting the domain/url of a WKWebView

I'm trying to include an online form in my application using a UIWebView, and I noticed that once the user finishes the form, s/he can navigate to different addresses. Is there any way I can restrict the domain/url access of a webkit?
(This used to be possible using UIWebView example: https://stackoverflow.com/questions/7673116/restrict-uiwebview-to-certain-pages‌ but it's now deprecated)
WKWebView can utilize the WKNavigationDelegate to restrict navigation.
func webView(webView: WKWebView, decidePolicyForNavigationAction navigationAction: WKNavigationAction, decisionHandler: (WKNavigationActionPolicy) -> Void) {
if let url = navigationAction.request.URL {
if url == permittedUrl {
decisionHandler(.allow)
} else {
decisionHandler(.cancel)
}
}
}

Resources