0

I am displaying a WebApp through WKWebView in Swift 3 (Xcode 8.3.3)

The WebApp is functioning as expected, except when I try to post an image to the server through the WebView.

The console from Xcode simply prints "Creating an image format with an unknown type is an error" without any further info to help me troubleshoot.

As this thread indicates, there might be something with alerts that is causing this issue. That the WebView is not set up correctly to display alerts and that this might cause an error.

However I have not been able to figure that one out by looking at the suggested solutions there.

In my viewWillAppear I call the function initWebView() which is set up like this:

func initWebView() {
   let source: NSString = "var meta = document.createElement('meta');" +
        "meta.name = 'viewport';" +
        "meta.content = 'width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no';" +
        "var head = document.getElementsByTagName('head')[0];" +
        "head.appendChild(meta);" as NSString
    let sourceScript: WKUserScript = WKUserScript(source: source as String, injectionTime: .atDocumentStart, forMainFrameOnly: true)

    let addCookieScript="localStorage.setItem('device', '\(self.tokenFirebase)');"
    let script: WKUserScript = WKUserScript(source: addCookieScript as String, injectionTime: .atDocumentStart, forMainFrameOnly: false)

    // Create the user content controller and add the script to it
    let userContentController = WKUserContentController()
    userContentController.addUserScript(script)
    userContentController.addUserScript(sourceScript)

    // Create the configuration with the user content controller
    let config = WKWebViewConfiguration()
    config.userContentController = userContentController

    let wkWebView = WKWebView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height), configuration: config)
    wkWebView.translatesAutoresizingMaskIntoConstraints = true
    self.wkWebView = wkWebView
    wkWebView.restorationIdentifier = "wkWebView"
    view.addSubview(wkWebView)
    wkWebView.navigationDelegate = self
    wkWebView.uiDelegate = self
    let request : NSMutableURLRequest = NSMutableURLRequest(url: self.webUrl as URL)
    wkWebView.isHidden = false
    wkWebView.load(request as URLRequest)

}

I have been testing the app with the same results across iPhone SE (iOS 10) iPhone 7 (iOS 10) and iPad (iOS 9.3.3).

EDIT: The possible duplicate entry does not appear valid to me as it refers to implementing UIImagePickerController by code, but in my scenario it is the image picker that is created by the WKWebView which seems to cause this issue. However if I'm mistaken please guide me in the right direction :)

4
  • Possible duplicate of Creating an image format with an unknown type is an error Objective-C Xcode 8 Commented Sep 14, 2017 at 15:39
  • As you see in that thread the problem pertains to code implementation of UIImagePicker in swift, in my example this code is not even called as it (should be?) handled by the WKWEbView itself. Commented Sep 14, 2017 at 15:43
  • Since the warning appears while showing the system image picker, I do think that this is a duplicate. The solution for them are, at least, the same: ignoring this warning. Commented Sep 14, 2017 at 15:45
  • Well, I did not find one accepted solution, and none of them in Swift. Perhaps you could direct me in the right way, I seem to be a bit lost with regards to this issue, which is why I created this thread in the first place. I have also taken the time to search both Stack Overflow and Google thoroughly. How would I go about ignoring said warning? Commented Sep 14, 2017 at 16:15

1 Answer 1

1

Creating an image format with an unknown type is an error this is a generic warning you can safely ignore the warning. The problem should be something else.

import UIKit
import WebKit
class ViewController: UIViewController {
    var webView:WKWebView!
    override func viewDidLoad() {
        super.viewDidLoad()
       loadWebView()
    }

    func loadWebView(){
        webView = WKWebView(frame: self.view.frame)
        self.view.addSubview(webView)
        let urlRequest = URLRequest(url: URL(string: "https://imgbb.com/")!)
        webView.load(urlRequest)
    }
}

I created a webview and tried to upload an image from photo library. it does give me that warning but it successfully uploaded the pic from photo library. You can try to above code and see if it works for you. btw don't forget to add permission explanation to access photolibrary in info.plist

result

Sign up to request clarification or add additional context in comments.

4 Comments

Strangely, no, this code does not work as intended either, not in simulator or any of the physical devices. I have bot the "Privacy - Camera Usage Description" and the "Privacy - Photo Library Usage Description" set in Info.plist.
Correction, when I initialized the webview in viewDidLoad and not viewWillAppear it performs as expected. However in my app (with your code used exactly, but with URL replaced) I get the Generic error message again and it seems as though it goes through the viewDidLoad steps once more....
@lundzern viewDidLoad method will be called first than viewwillappear. webview is working fine. however if possible email me the project at [email protected]. so that i can see in which problem you are running into
I got it working in my own app as well now after removing this code snippet from viewDidLoad; let tap: UIGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(ViewController.dismissKeyboard)) view.addGestureRecognizer(tap) this was calling the follwing function: func dismissKeyboard() { view.endEditing(true) }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.