Skip to content

Instantly share code, notes, and snippets.

@sloanlance
Last active June 25, 2021 14:47
Show Gist options
  • Save sloanlance/06fb1f7659a526d688664fcb8714a73e to your computer and use it in GitHub Desktop.
Save sloanlance/06fb1f7659a526d688664fcb8714a73e to your computer and use it in GitHub Desktop.
Pythonista: Opening other apps from the application extension (iOS sharing mechanism)

In older versions of iOS, a Pythonista program run from the application extension (iOS sharing mechanism) could open other applications using code like:

import webbrowser
webbrowser.open('googlechrome://example.com/')

Where googlechrome (and googlechromes for secure connections) is a special URL scheme registered by the Google Chrome application. Some other applications have their own schemes.

However, that doesn't work in more recent versions of iOS (at least version 9.3.5 and newer; older versions may be affected, too). That code will open Google Chrome if the program is run from the Pythonista UI, but not if it's run from a sharing request.

Fortunately, later versions of Pythonista includes the objc_util module to facilitate working with iOS' internal Objective-C API. Using that, the iOS functions can be called to open the URL in Google Chrome with this code instead:

from objc_util import *
app = UIApplication.sharedApplication()
app.openURL_(nsurl('googlechrome://example.com/'))

Which is not much longer than the webbrowser.open() method. This example was given to me by @omz, the author of Pythonista, in the app's user community forum:

https://forum.omz-software.com/topic/3546/webbrowser-get-and-register/6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment