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