Search code examples
swiftnsurldata-uriopenurldata-uri-scheme

Swift shardApplication().openURL() with data URI


So I have a String with the template of

let dataURI = 'data:MIMEType;base64,DATA'

where the data is filled with a bunch of base64 representing the file I want to open. This url will load in a WKWebView or UIWebView; however, when I try to take the same URL and open it via

let uri = NSURL(string: dataURI)
UIApplication.sharedApplication().openURL(uri)

I get the error

LaunchServices: ERROR: There is no registered handler for URL scheme data

I also attempted a string with the format of

let uristring = 'data://' + dataURI 

which gets the same error.

Any ideas whats going on here?

Thanks!


Solution

  • iOS only contains handlers for a few URL schemes (notably http, mailto, tel, and sms). Unfortunately it doesn't come with built-in support for the data scheme. Because of this, the OS doesn't know that it can pass your data to Safari to be displayed.

    You can find more information on Inter-App Communication here and all of the schemes supported natively by iOS here.