0
votes

I am using amazon affiliate image links in my website that is then converted to an App using the WKWebView in Swift 4.

The Amazon affiliate link works perfectly fine if I access the webpage directly from Safari or any other browser. But the image is not showing in the app that is using WKWebView: a small question mark icon is displayed instead, and when I click the small icon it opens the right amazon link.

Here is my ViewController.swift:

import UIKit
import WebKit

class ViewController: UIViewController, WKNavigationDelegate {
    var webView: WKWebView!


    override func loadView() {
        webView = WKWebView()
        webView.navigationDelegate = self
        view = webView
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // 1
        let url = URL(string: "http://myurl/")!
        webView.load(URLRequest(url: url))

        // 2
        let refresh = UIBarButtonItem(barButtonSystemItem: .refresh, target: webView, action: #selector(webView.reload))
        toolbarItems = [refresh]
        navigationController?.isToolbarHidden = false
    }

    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        title = webView.title
    }
}

Here is the Amazon affiliate link that I have in my HTML:

<a target="_blank"  href="https://rads.stackoverflow.com/amzn/click/com/B00KEBYK88" rel="nofollow noreferrer"><img border="0" src="https://ws-na.amazon-adsystem.com/widgets/q?_encoding=UTF8&MarketPlace=US&ASIN=B00KEBYK88&ServiceVersion=20070822&ID=AsinImage&WS=1&Format=_SL160_&tag=questionrs-20" ></a><img src="https://ir-na.amazon-adsystem.com/e/ir?t=questionrs-20&l=am2&o=1&a=B00KEBYK88" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" />

Note that it has an img tag:

<img border="0" src="https://ws-na.amazon-adsystem.com/widgets/q?_encoding=UTF8&MarketPlace=US&ASIN=B00KEBYK88&ServiceVersion=20070822&ID=AsinImage&WS=1&Format=_SL160_&tag=questionrs-20" >

If I put this img tag directly in my HTML the image is still not shown via the WKWebView in the app (but shows fine if I open the website in any browser directly)

If I open the image src in the browser it is converted to:

https://images-na.ssl-images-amazon.com/images/I/41ZIpVTW8-L._SL160_.jpg

And, if I put this new URL in the src, WKWebView can display it.

Is there any possibility to have WKWebView display the original image URL?

1
You are loading WKWebView with a internal phone url or a external website? Also see if this helps stackoverflow.com/questions/32456848/…Tarun Lalwani
The link you provided helped; I added Allow Arbitrary Loads in Web Content = YES and Allow Arbitrary Loads = YES in App Transport Security Settings dictionary in info.plist and all worked. Please include this as an answer so I can give the 200 bounty after 24 hours restriction is passed.Nylon Smile

1 Answers

3
votes

You need to add Allow Arbitrary Loads in Web Content = YES and Allow Arbitrary Loads = YES in App Transport Security Settings dictionary in info.plist

iOS9 does not load insecure resources from a secure page (SSL/HTTPS)