Capture redirect url in wkwebview in ios
Use this WKNavigationDelegate
method
public func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Swift.Void) {
if(navigationAction.navigationType == .other) {
if let redirectedUrl = navigationAction.request.url {
//do what you need with url
//self.delegate?.openURL(url: redirectedUrl)
}
decisionHandler(.cancel)
return
}
decisionHandler(.allow)
}
Hope this helps
WKWebView - Add a parameter to the URL on redirect in Swift - iOS
WKNavigationDelegate, URLComponents
class ViewerViewController: UIViewController, WKNavigationDelegate //<--
// then whenever you call the WKWebView
//ex: URL = https://en.wikipedia.org/wiki/Main_Page
WKWebView.navigationDelegate = self
WKWebView.load(URLRequest)
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
print("URL:", navigationAction.request.url)
if let host = navigationAction.request.url?.path
{
if host.contains("Main_Page")
{
//You can modify this part to form a new url and pass it again.
var components = URLComponents()
components.scheme = navigationAction.request.url?.scheme
components.host = navigationAction.request.url?.host
components.path = navigationAction.request.url!.path
print("new URL:", components.url!)
let customRequest = URLRequest(url: components.url!)
//change the url and pass it again..
WKWebView.load(customRequest) //<--load the new url again..
decisionHandler(.cancel)
}
else
{
decisionHandler(.allow) //<-- this will open the page.
}
}
else
{
decisionHandler(.allow)
}
}
Problem loading a URL in WKWebView that works fine on SFSafariViewController
The difference is caused by the User-Agent header being different when loading via a WKWebView vs SFSafariViewController. The website you are loading must be using the User-Agent header to determine where to redirect.
By setting the customUserAgent
property on your WKWebView to the default iOS Safari User-Agent the same page will be directed to in your WKWebView as when using SFSafariViewController:
webView.customUserAgent = "Mozilla/5.0 (iPhone; CPU iPhone OS 15_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.5 Mobile/15E148 Safari/604.1"
Related Topics
How to Get an Iphone's Device Name
Mkmapview Mkpointannotation Tap Event
Cocoa-Touch: How to See If Two Nsdates Are in the Same Day
Uibutton That Resizes to Fit Its Titlelabel
In Call Status Bar (Unable to Satisfy Constraints)
Ib_Designable, Ibinspectable -- Interface Builder Does Not Update
Uiapplication.Registerforremotenotifications() Must Be Called from Main Thread Only
How to Add Strings on X Axis in iOS-Charts
How to Hide Status Bar in Uiimagepickercontroller
How to Convert an Int to Hex String in Swift
Minimum and Maximum Date in Uidatepicker
How to Get a Cgpoint from a Tapped Location
How to Use Addtarget Method in Swift 3
Making a Phone Call in an iOS Application