Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Titanium and URL Schemes

Titanium and URL Schemes

Talk from TiConf US where I talk about implementing URL Schemes in your apps to allow two-way, inter-app communication and transferring of data and binary files.

Jason Kneen

June 29, 2013
Tweet

More Decks by Jason Kneen

Other Decks in Programming

Transcript

  1. What is a URL Scheme? "Top level of the uniform

    resource locator naming structure" or "URI Schemes" if you like Saturday, 29 June 13
  2. What is a URL Scheme? "Top level of the uniform

    resource locator naming structure" or "URI Schemes" if you like Saturday, 29 June 13
  3. • https:// What is a URL Scheme? "Top level of

    the uniform resource locator naming structure" or "URI Schemes" if you like Saturday, 29 June 13
  4. • https:// • ftp:// What is a URL Scheme? "Top

    level of the uniform resource locator naming structure" or "URI Schemes" if you like Saturday, 29 June 13
  5. • https:// • ftp:// • mailto: What is a URL

    Scheme? "Top level of the uniform resource locator naming structure" or "URI Schemes" if you like Saturday, 29 June 13
  6. • https:// • ftp:// • mailto: • tel: What is

    a URL Scheme? "Top level of the uniform resource locator naming structure" or "URI Schemes" if you like Saturday, 29 June 13
  7. • https:// • ftp:// • mailto: • tel: • outlook://

    What is a URL Scheme? "Top level of the uniform resource locator naming structure" or "URI Schemes" if you like Saturday, 29 June 13
  8. What can we do with them? The same thing we

    do every night, Pinky - try to take over the URL! Saturday, 29 June 13
  9. What can we do with them? • Detect Apps The

    same thing we do every night, Pinky - try to take over the URL! Saturday, 29 June 13
  10. What can we do with them? • Detect Apps •

    Launch / Resume Apps The same thing we do every night, Pinky - try to take over the URL! Saturday, 29 June 13
  11. What can we do with them? • Detect Apps •

    Launch / Resume Apps • Send commands/data to apps The same thing we do every night, Pinky - try to take over the URL! Saturday, 29 June 13
  12. What can we do with them? • Detect Apps •

    Launch / Resume Apps • Send commands/data to apps • Receive data/callbacks from apps The same thing we do every night, Pinky - try to take over the URL! Saturday, 29 June 13
  13. Launch Center apps How it works • App detection via

    URL Schemes • Ability to add “Actions” Saturday, 29 June 13
  14. Launch Center apps How it works • App detection via

    URL Schemes • Ability to add “Actions” • Dependent on URL Scheme list Saturday, 29 June 13
  15. Google Maps Launching the native app for map links Web

    App Native App Saturday, 29 June 13
  16. Examples iOS Gmail app, detecting other Google app installs Before

    Google Maps installation Saturday, 29 June 13
  17. Examples iOS Gmail app, detecting other Google app installs Before

    Google Maps installation After Google Maps installation Saturday, 29 June 13
  18. Apps using URL Schemes • youtube:// • tweetbot:// • googlemaps://

    • twitter:// • bufferapp:// • linkedin:// • pinterest:// • googlegmail:// Enter into the browser to test • http://wiki.akosma.com/IPhone_URL_Schemes Useful links • http://canhandleUrl.com Saturday, 29 June 13
  19. Launching apps with actions Try it out without code tweetbot://something/post?text=Hello!

    http://tinyurl.com/ticonfurl http://handleopenurl.com/scheme/tweetbot Saturday, 29 June 13
  20. Launching apps with actions Try it out without code tweetbot://something/post?text=Hello!

    http://tinyurl.com/ticonfurl http://handleopenurl.com/scheme/tweetbot Saturday, 29 June 13
  21. URL Scheme usages • App launching (Chrome, Maps, YouTube) •

    Cross-promote apps Saturday, 29 June 13
  22. URL Scheme usages • App launching (Chrome, Maps, YouTube) •

    Cross-promote apps • Single Sign On Saturday, 29 June 13
  23. URL Scheme usages • App launching (Chrome, Maps, YouTube) •

    Cross-promote apps • Single Sign On • SMS based Push Notifications Saturday, 29 June 13
  24. URL Scheme usages • App launching (Chrome, Maps, YouTube) •

    Cross-promote apps • Single Sign On • SMS based Push Notifications • Use specific features of other apps Saturday, 29 June 13
  25. Sharing data between apps • Encode the file into base64

    Transferring binary files Saturday, 29 June 13
  26. Sharing data between apps • Encode the file into base64

    • Transfer it via URL Transferring binary files Saturday, 29 June 13
  27. Sharing data between apps • Encode the file into base64

    • Transfer it via URL • URL Decode on the other side Transferring binary files Saturday, 29 June 13
  28. Sharing data between apps • Encode the file into base64

    • Transfer it via URL • URL Decode on the other side • Decode from base64 Transferring binary files Saturday, 29 June 13
  29. Sharing data between apps • Encode the file into base64

    • Transfer it via URL • URL Decode on the other side • Decode from base64 • Do Stuff Transferring binary files Saturday, 29 June 13
  30. Implementing URL Schemes Adding to Titanium Apps • Register a

    URL Scheme for an App Saturday, 29 June 13
  31. Implementing URL Schemes Adding to Titanium Apps • Register a

    URL Scheme for an App • Add events and handling code to read url Saturday, 29 June 13
  32. Implementing URL Schemes Adding to Titanium Apps • Register a

    URL Scheme for an App • Add events and handling code to read url • Add code to parse URLs Saturday, 29 June 13
  33. Implementing URL Schemes Adding to Titanium Apps • Register a

    URL Scheme for an App • Add events and handling code to read url • Add code to parse URLs • Add actions, responses, callbacks Saturday, 29 June 13
  34. Implementing URL Schemes Adding to Titanium Apps • Register a

    URL Scheme for an App • Add events and handling code to read url • Add code to parse URLs • Add actions, responses, callbacks • Let people know about it (if public) Saturday, 29 June 13
  35. Detecting and launching apps on iOS        

                     if  (Ti.Platform.canOpenUrl(“tweetbot://”);{           //  do  stuff Ti.Platform.openUrl(“tweetbot://default/post? text=Hello!”);                } Saturday, 29 June 13
  36. Detecting and launching apps on Android var  myIntent  =  Ti.Android.createIntent({

                               action:  Ti.Android.ACTION_MAIN,                            packageName:  "com.mybundle.appname",                            className:  "com.mybundle.appname.appname",                            flags:  Ti.Android.FLAG_ACTIVITY_NEW_TASK, }); myIntent.addCategory(Ti.Android.CATEGORY_LAUNCHER); Ti.Android.currentActivity.startActivity(  myIntent  ); Saturday, 29 June 13
  37. Register a URL Scheme for an App In iOS using

    info.plist Saturday, 29 June 13
  38. Register a URL Scheme for an App In Android <android

    xmlns:android="http://schemas.android.com/apk/res/android"> <manifest android:versionCode="1" android:versionName="1.00"/> <activities> <activity url="app.js"> <intent-filter> <action android:name="android.intent.action.VIEW"/> <category android:name="android.intent.category.DEFAULT"/> <category android:name="android.intent.category.BROWSABLE"/> <data android:host="" android:scheme="myscheme"/> </intent-filter> </activity> </activities> </android> TiApp.xml :- Saturday, 29 June 13
  39. Register a URL Scheme for an App In Android <android

    xmlns:android="http://schemas.android.com/apk/res/android"> <manifest android:versionCode="1" android:versionName="1.00"/> <activities> <activity url="app.js"> <intent-filter> <action android:name="android.intent.action.VIEW"/> <category android:name="android.intent.category.DEFAULT"/> <category android:name="android.intent.category.BROWSABLE"/> <data android:host="" android:scheme="myscheme"/> </intent-filter> </activity> </activities> </android> TiApp.xml :- Saturday, 29 June 13
  40. Register a URL Scheme for an App In both iOS

    and Android within TiApp.xml Saturday, 29 June 13
  41. Add handling code to parse URLs In iOS, trap app

    start and resume events                          if  (Ti.App.getArguments().url)  {                                  urlToObject(Ti.App.getArguments().url);                } http://tinyurl.com/urlschemecode Saturday, 29 June 13
  42. Add handling code to parse URLs In Android var  activity

     =  Ti.Android.currentActivity; var  data  =  activity.getIntent().getData();                                if  (data)  {                    urlToObject(data);                } http://tinyurl.com/urlschemecode Saturday, 29 June 13
  43. Parsing the URLs In Android and iOS function  urlToObject(url)  {

    var  returnObj  =  {}; url  =  url.replace('URLSCHEME://?',  ''); var  params  =  url.split('&'); params.forEach(function(param)  {                                 var  keyAndValue  =  param.split('='); returnObj[keyAndValue[0]]  =  decodeURI(keyAndValue[1]); }); return  obj; } http://tinyurl.com/urlschemecode Saturday, 29 June 13
  44. URL Scheme compatibility issues • URL Schemes on non-mobile devices

    • Not recognized by some apps / services Saturday, 29 June 13
  45. URL Scheme compatibility issues • URL Schemes on non-mobile devices

    • Not recognized by some apps / services • Use a redirect script Saturday, 29 June 13
  46. Using a redirect script • Allows you to detect apps,

    act accordingly Saturday, 29 June 13
  47. Using a redirect script • Allows you to detect apps,

    act accordingly • You can use services that don’t accept URL schemes Saturday, 29 June 13
  48. Using a redirect script • Allows you to detect apps,

    act accordingly • You can use services that don’t accept URL schemes • Allows checking, fallback URLs Saturday, 29 June 13
  49. Summary • Direct cross-platform, inter-app communication • Growing support from

    developers • Resources available to find out Schemes / Actions Saturday, 29 June 13
  50. Summary • Direct cross-platform, inter-app communication • Growing support from

    developers • Resources available to find out Schemes / Actions • Shorten, handle failures, desktop access Saturday, 29 June 13
  51. Summary • Direct cross-platform, inter-app communication • Growing support from

    developers • Resources available to find out Schemes / Actions • Shorten, handle failures, desktop access • You can send more than text! Saturday, 29 June 13