Last active
August 29, 2015 13:56
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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