Skip to content

Instantly share code, notes, and snippets.

@ericboehs
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericboehs/9301434 to your computer and use it in GitHub Desktop.
Save ericboehs/9301434 to your computer and use it in GitHub Desktop.
Titanium script to open a link in Chrome or Safari. Be sure to set `appUrlScheme` in config.json.
openURL = (url, protocol='http') ->
Ti.Platform.openURL "#{protocol}://#{url}"
openApp = (e) ->
if e?.index == 1
protocol = e.source.alternativeProtocol
# Add Google Chrome specific callback params
if e.source.alternativeProtocol == 'googlechrome-x-callback'
url = "x-callback-url/open/?"
url += "x-source=#{encodeURIComponent Ti.App.name}&"
url += "x-success=#{encodeURIComponent Alloy.CFG.appUrlScheme + ':'}&"
url += "url=#{encodeURIComponent 'http://' + e.source.url}&"
openURL (url ? e.source.url), (protocol ? e.source.protocol)
checkAndPromptForApp = (e) ->
dialog = $[e.source.promptDialog]
if e.source?.alternativeProtocol && Ti.Platform.canOpenURL "#{e.source.alternativeProtocol}:"
dialog.protocol = e.source.protocol
dialog.url = e.source.url
dialog.show()
else
openApp e
Alloy
Window
Button(
onClick='checkAndPromptForApp'
alternativeProtocol='googlechrome-x-callback'
url='example.com'
promptDialog='openInBrowserDialog') http://example.com
OptionDialog#openInBrowserDialog(onClick='openApp' cancel=2 alternativeProtocol='googlechrome-x-callback')
Options
Option Open in Safari
Option Open in Chrome
Option Cancel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment