OLD | NEW |
(Empty) | |
| 1 <h1 class="page_title">Connect Apps with Web Intents</h1> |
| 2 <p> |
| 3 <a href="http://webintents.org/">Web Intents</a> |
| 4 allow your application to quickly communicate |
| 5 with other applications on the user's system and inside their browser. |
| 6 Your application can register to handle specific user actions |
| 7 such as editing images via the <code>manifest.json</code>; |
| 8 your application can also invoke actions to be handled by other applications. |
| 9 </p> |
| 10 <p>Pacakged apps use Web Intents as their primary mechanism for inter-app |
| 11 communication.</p> |
| 12 <p class="note"> |
| 13 <b>API Samples: </b> |
| 14 Want to play with the code? |
| 15 Check out the |
| 16 <a href="https://github.com/GoogleChrome/chrome-app-samples/tree/master/webinten
ts">webintents</a> sample. |
| 17 </p> |
| 18 <h2 id="register">Register your app to handle an action</h2> |
| 19 <p> |
| 20 You must supply the intent in the manifest: |
| 21 </p> |
| 22 <pre> |
| 23 "intents":{ |
| 24 "http://webintents.org/edit" : [{ |
| 25 "title" : "Best Image editing app", |
| 26 "type" : ["image/*"] |
| 27 }] |
| 28 } |
| 29 </pre> |
| 30 <p> |
| 31 Unlike extensions and hosted apps, packaged applications do not |
| 32 need a "href" attribute in the manifest declaration, this is |
| 33 because packaged apps have a single entry point for |
| 34 launch - the <code>onLaunched</code> event. |
| 35 </p> |
| 36 <h2 id="content">Handling content types</h2> |
| 37 <p> |
| 38 Your application can be the user's preferred choice for handling a file type. |
| 39 For example, your application could handle viewing images or viewing pdfs. |
| 40 You must supply the intent in the manifest |
| 41 and use the "http://webintents.org/view" action: |
| 42 </p> |
| 43 <p>To be able declare your application's ability to view RSS and ATOM |
| 44 feeds, you would add the following to your manifest. |
| 45 </p> |
| 46 <pre> |
| 47 "intents": { |
| 48 "http://webintents.org/view" : [{ |
| 49 "title" : "RSS Feed Reader", |
| 50 "type" : ["application/atom+xml", "application/rss+xml"] |
| 51 }] |
| 52 } |
| 53 </pre> |
| 54 <p> |
| 55 Your application will receive intent payload through the <code>onLaunched</code>
event. |
| 56 </p> |
| 57 <pre> |
| 58 chrome.experimental.app.onLaunched(function(intent) { |
| 59 // App Launched |
| 60 if(intent.action == "http://webinents.org/view" && |
| 61 intent.type == "application/atom+xml") { |
| 62 // obtain the ATOM feed data. |
| 63 var data = intent.data; |
| 64 } |
| 65 }); |
| 66 </pre> |
| 67 <h2 id="launching">Launching an app with a file</h2> |
| 68 <p> |
| 69 If your app handles the <code>view</code> intent, |
| 70 it is possible to launch it from the command line with a file as a parameter. |
| 71 </p> |
| 72 <pre> |
| 73 chrome.exe --app-id [app_id] [path_to_file] |
| 74 </pre> |
| 75 <p> |
| 76 This will implicity launch your application with an intent payload populated |
| 77 with the action set to "http://webintents.org/view", the type set to the |
| 78 mime-type of the file and the data as a <code>FileEntry</code> object. |
| 79 </p> |
| 80 <pre> |
| 81 chrome.experimental.app.onLaunched(function(intent) { |
| 82 // App Launched |
| 83 var data = intent.data; |
| 84 }); |
| 85 </pre> |
| 86 <h2 id="launching">Manipulating the file</h2> |
| 87 <p> |
| 88 When your application is launched with a file as the parameter |
| 89 on the command-line, |
| 90 the <code>intent.data</code> property is a <code>FileEntry</code>. |
| 91 This is really cool because now you have a direct reference back to the physic
al |
| 92 file on the disk, |
| 93 and you can write data back to it. |
| 94 </p> |
| 95 <pre> |
| 96 chrome.experimental.app.onLaunched(function(intent) { |
| 97 // App Launched |
| 98 var data = intent.data; |
| 99 if(data instanceof FileEntry) { |
| 100 data.createWriter(function(writer) { |
| 101 writer.onwriteend = function(e) { |
| 102 console.log('Write completed.'); |
| 103 }; |
| 104 writer.onerror = function(e) { |
| 105 console.log('Write failed: ' + e.toString()); |
| 106 }; |
| 107 // Create a new Blob and write it to log.txt. |
| 108 var bb = new BlobBuilder(); // Note: window.WebKitBlobBuilder in Chrome 12
. |
| 109 bb.append('Lorem Ipsum'); |
| 110 writer.write(bb.getBlob('text/plain')); |
| 111 }); |
| 112 } |
| 113 }); |
| 114 </pre> |
| 115 <h2 id="return">Returning data to calling application</h2> |
| 116 <p> |
| 117 Lots of applications want to cooperate |
| 118 with the app that invoked them. |
| 119 It's easy to send data back to the calling client |
| 120 using <code>intent.postResult</code>: |
| 121 </p> |
| 122 <pre> |
| 123 chrome.experimental.app.onLaunched(function(intent) { |
| 124 // App Launched |
| 125 console.log(intent.action); |
| 126 console.log(intent.type); |
| 127 var data = intent.data; |
| 128 // Do something with the data; |
| 129 intent.postResult(newData); |
| 130 }); |
| 131 </pre> |
| 132 <h2 id="localize">Localizing your app title</h2> |
| 133 <p> |
| 134 If your application or extension is localized |
| 135 as per the guidelines in |
| 136 <a href="i18n.html">Internationalization (i18n)</a>, |
| 137 you can localize the title of your intent in the picker |
| 138 using the exact same infrastructure: |
| 139 </p> |
| 140 <pre> |
| 141 "intents": { |
| 142 "http://webintents.org/edit" : [{ |
| 143 "title" : "__MSG_intent_title__", |
| 144 "type" : ["image/*"], |
| 145 "disposition" : "inline" |
| 146 }] |
| 147 } |
| 148 </pre> |
| 149 <h2 id="invoke">Invoking an action</h2> |
| 150 <p> |
| 151 If your application needs to be able |
| 152 to use the functionality of another application, |
| 153 it can simply ask the browser for it. |
| 154 To ask for an application that supports image editing, |
| 155 it's as simple as: |
| 156 </p> |
| 157 <pre> |
| 158 var intent = new WebKitIntent("http://webintents.org/edit", "image/png", "dataUr
i://"); |
| 159 window.navigator.webkitStartActivity(intent, function(data) { |
| 160 // The data from the remote application is returned here. |
| 161 }); |
| 162 </pre> |
| 163 <h2 id="errors">Handling Errors and Exceptions</h2> |
| 164 <p> |
| 165 If your service application needs to signal to the client application |
| 166 that an unrecoverable error has occurred, |
| 167 then your application will need |
| 168 to call <code>postError</code> on the intent object. |
| 169 This will signal to the client’s onError callback |
| 170 that something has gone wrong. |
| 171 </p> |
| 172 <h3>Client</h3> |
| 173 <pre> |
| 174 var intent = new WebKitIntent("http://webintents.org/edit", "image/png", "dataUr
i://"); |
| 175 var onSuccess = function(data) {}; |
| 176 var onError = function() {}; |
| 177 window.navigator.webkitStartActivity(intent, onSuccess, onError); |
| 178 </pre> |
| 179 <h3>Service</h3> |
| 180 <pre> |
| 181 chrome.experimental.app.onLaunched(function(intent) { |
| 182 // App Launched |
| 183 console.log(intent.action); |
| 184 console.log(intent.type); |
| 185 var data = intent.data; |
| 186 // Do something with the data; |
| 187 intent.postResult(newData); |
| 188 }); |
| 189 </pre> |
| 190 <p class="backtotop"><a href="#top">Back to top</a></p> |
OLD | NEW |