Prevent universal links from opening in `WKWebView`/`UIWebView`
sourcecode for WebKit:
static const WKNavigationActionPolicy WK_API_AVAILABLE(macosx(10.11), ios(9.0)) _WKNavigationActionPolicyAllowWithoutTryingAppLink = (WKNavigationActionPolicy)(WKNavigationActionPolicyAllow + 2);
if you are using WKWebView
, just use WKNavigationActionPolicyAllow
+ 2 instead of WKNavigationActionPolicyAllow
Present iTunes link inside of a WebView (prevent redirect)?
A possible work-around is to request the desktop-mode of the website, which will display the intended content instead of redirecting you.
Using UIWebView:
UserDefaults.standard.register(defaults: ["UserAgent": "Custom-Agent"])
Make sure you register this custom agent before loading the URLRequest
. Note that this method would apply for all UIWebView
objects in your application. If you want just some specific views to load the desktop version, you'll need to use WKWebView
as follows, seeing as it allows you to use a custom agent per object.
Using WKWebView:
First, you must import WebKit
. Then, initialize it like this:
let url = URL(string: "https://itunes.apple.com/us/album/burn-that-broken-bed/id1120162623?i=1120163074&uo=4")!
let wkWebView = WKWebView(frame: self.view.frame, configuration: WKWebViewConfiguration())
wkWebView.uiDelegate = self // Optional line - must conform to WKUIDelegate
// the line below specifies the custom agent, which allows you to request the desktop version of the website
wkWebView.customUserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.109 Safari/537.36"
wkWebView.load(URLRequest(url: url))
self.view.addSubview(wkWebView)
Update: (Integrating WKWebView)
Unfortunately, you can't add a WKWebView in IB as of XCode 8, so you'll have to add it programmatically. The good news here is that you can use the frame
of the UIWebView you created in IB to slightly ease the programmatic instantiation of the WKWebView object
Check this out: (untested code)
// for ease of use
extension WKWebView {
func setDesktopMode(on: Bool) {
if on {
customUserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.109 Safari/537.36"
return
}
customUserAgent = nil
}
}
And in your custom cell file
class MyCustomCell: UICollectionViewCell {
var wkWebView: WKWebView! // add this line
@IBOutlet weak var webView: UIWebView! // the one you created in IB
}
Then in your UIViewController
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CellIdentifier", for: indexPath) as! MyCustomCell
let url = URL(string: "url here")!
cell.wkWebView = WKWebView(frame: cell.webView.frame, configuration: WKWebViewConfiguration()) // using the webView's frame that was created in IB
cell.wkWebView.uiDelegate = self // Optional line - must conform to WKUIDelegate
cell.wkWebView.setDesktopMode(on: true) // true = loads desktop mode (for your iTunes URLs)
cell.wkWebView.load(URLRequest(url: url))
return cell
}
Related Topics
Swift 3 - How to Make Timer Work in Background
Xcode 4.3.2, Issue with Running on Simulator
"Apprequests" Dialog Reports Success, Recipients Receive Nothing
Setting Up a Parent-Child Relationship in Core Data
Invalid Device State - Xcode/iOS Simulator Error
How to Make Offline Database for My App
Why Force Unwrapping Is Required in Case of Enum and Switch
Detect Touch on Child Node of Object in Spritekit
Storyboard Is Not Showing Custom Color Hex Code in Xcode 12.0.1
Uibutton Remove All Target-Actions
Open Links in Safari Instead of Uiwebview
Changing Uitableview's Section Header/Footer Title Without Reloading the Whole Table View
Round Top Corners of a Uiview and Add Border
Simple Low-Latency Audio Playback in iOS Swift
Calling Performseguewithidentifier Doesn't Call Shouldperformseguewithidentifier
iOS Facebook Sdk - Post Open Graph and Show on Timeline Without Clicking Activity Log