Changes to calling URL Schemes via system.openURL under iOS9

As part of Apple’s security changes in iOS9, use of custom URL Schemes to open other apps  (via system.openURL(“myurlscheme://”) may no longer work. You may see the following error in the console log …

-canOpenURL: failed for URL: “myurlscheme://” - error: "This app is not allowed to query for scheme myurlscheme”

This is prevent one app arbitrarily calling another app and Apple encourage developers to use their new Universal Links or app extensions instead. This is obviously going to break a lot of existing apps!

I am currently developing a series of children’s book apps that can each be called from a single library app via custom URL Schemes, which all worked fine under iOS8. When I upgraded one of my devices to iOS9 the apps (in beta on TestFlight and complied fro iOS8) still linked to each other but now popped up a dialog box asking the user to confirm the call to the other app was OK. This only happened the first time the call was made and thereafter transferred directly which is a reasonable compromise to the user experience. However, once the apps were recompiled for iOS9 the URL Scheme call failed with the error message above. What my code does if the openURL call returns false (i.e. the app is not already installed on the device) is link to the app store instead. Problem now is the even when the app is installed it links to the app store so the mechanism is broken!

The reason why this still worked with the iOS8 versions is that Apple is allowing up to 50 URL Schemes to be linked so that existing apps are not initially broken. So what’s the solution for new apps? Well the answer lies in a new Plist entry in build.settings.

                       LSApplicationQueriesSchemes =

                        {

                            “myurlscheme1”,

                            “myurlscheme2”,

                            “myurlscheme3”,

                        },

This works for my custom URL Schemes and presumably would also work if you specified another app that is not one of your own.

Happy linking!

Stefan

Thanks for sharing this.  What an awesome tip and a great write-up!

Methinks this should make in in the weekly top 3 forum posts.

Hi, I just wanted to clarify - How do you go about opening another app with, say, a specific document.

E.g. App 1 has Document A

App 1 calls to App 2 to open Document A <---- What does this call look like?

App 2 opens document A

When you you open an app via a URL Scheme - e.g. system.openURL(“myapp://”) -  you can also include parameters e.g.  system.openURL(“myapp://myapp://doca”).

This can be decoded in the app startup function …

local function onSystemEvent( event )

  if event.type == “applicationOpen” and event.url then

    – opened via url scheme

    local launchURL = event.url

    – parse launchURL here to detect parameters

  elseif event.type == “applicationStart” then

  elseif event.type == “applicationExit” then

  elseif event.type == “applicationSuspend” then

  end

end

Runtime:addEventListener( “system”, onSystemEvent )

Do you know what any of the calls are/where i can find out what they are? Like Safari or Pages?

This site has a good catalog of deeplinks for apps. I can’t vouch for their validity, but the listing appears to be relatively comprehensive. 

http://handleopenurl.com/scheme

If you want to use third-party app then Alex has pointed you in the right direction. You refer specifically to Safari and Pages - web pages get opened by system.openURL (and can be local or remote - use http:// prefix). As far as I know you cannot directly call the Pages app by a URL Scheme but mobile Safari will open Pages (and Word or PDF) documents so you can use the same call.

Otherwise read this guide for using your own URL Schemes …

https://coronalabs.com/blog/2011/12/22/using-app-url-schemes-in-ios/

It’s a bit old but still relevant for iOS.

Android Intents now also work. See here …

https://forums.coronalabs.com/topic/37105-handling-url-scheme-on-android/

I’ve been trying to open PDF and Word documents with it (Using system.pathForFile) and it never loads. Any ideas why/Is there anything I’m doing wrong?

system.openURL(system.pathForFile(“MyDocument.pdf”, system.DocumentsDirectory))

OK my mistake - try loading the document into a web view instead (which has the advantage of being resizable to doesn’t have to be full screen but, note, as it is a native object it displays on top of everything else) …

local webView = native.newWebView( display.contentCenterX, display.contentCenterY, 320, 480 )

webView:request( “MyDocument.pdf”, system.DocumentsDirectory )

No, that just presents a large blank white box

I tried a webpopup too and that does the same thing…

Can you post an example of your code?

I created a new project through Corona and just copied and pasted what you’d written into main.lua (I changed the document to one i actually had)

----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- -- Your code here local webView = native.newWebView( display.contentCenterX, display.contentCenterY, 320, 480 ) webView:request( "websitedesignbrief.docx", system.DocumentsDirectory )

OK I just tried it and it does only seems to work with a PDF file. I have a vague memory of using this method a few years ago but maybe it doesn’t work for local files, only ones on a web server, or something has changed? According to Apple’s documentation the web view should support other document types. 

The alternative is to use the Quick Look plug-in …

https://coronalabs.com/blog/2013/12/24/tutorial-introducing-the-quicklook-plugin-ios/

Note this is iOS only, although I think on Android the OS will try to open a web view document using an appropriate file viewer.

Ok! I’ll give that a go when I get back to work tomorrow! Thanks so much for your help so far :slight_smile:

Regarding .doc/.docx files specifically, the below thread might also be useful:

https://forums.coronalabs.com/topic/59324-read-docx-files/

So the plugin works perfectly! Thank you so much for your help, this has been a major problem for me! :slight_smile: Also, I’m interested in the XML stuff so I’ll take a look into that too. Thank you!

Glad to help!  :slight_smile:

Really helpful write up. I have some doubht about usage of iOS9, this write up help me to clear that.

Thankyou :slight_smile: :slight_smile: :slight_smile:

Thanks for sharing this.  What an awesome tip and a great write-up!

Methinks this should make in in the weekly top 3 forum posts.

Hi, I just wanted to clarify - How do you go about opening another app with, say, a specific document.

E.g. App 1 has Document A

App 1 calls to App 2 to open Document A <---- What does this call look like?

App 2 opens document A