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