|
1 | | -urlSession [](https://travis-ci.org/appcelerator-modules/ti.urlsession) |
2 | | -========== |
| 1 | +# Ti.URLSession Module |
3 | 2 |
|
4 | | -**This module is available only for iOS 7 and above.** |
| 3 | +## Description |
5 | 4 |
|
6 | | -## COMPILING AND PACKAGING THE MODULE |
| 5 | +Creates a lightweight wrapper around `NSURLSession` for working with the Backgrounding Feature in iOS 7 and later. |
7 | 6 |
|
8 | | -Open `ios/ti.urlsession/titanium.xcconfig` and edit the Ti SDK path. |
| 7 | +## Accessing the urlSession Module |
9 | 8 |
|
10 | | -```bash |
11 | | -cd ios/ti.urlsession |
12 | | -./build.sh |
| 9 | +To access this module from JavaScript, you would do the following: |
| 10 | + |
| 11 | + var URLSession = require('com.appcelerator.urlSession'); |
| 12 | + |
| 13 | +The `URLSession` variable is a reference to the Module object. |
| 14 | + |
| 15 | +## Module |
| 16 | + |
| 17 | +### Methods |
| 18 | + |
| 19 | +#### createSessionConfiguration(arguments) |
| 20 | + |
| 21 | +Creates a preconfigured session configuration object that can be used to create a URLSession for |
| 22 | +performing a background the download task. |
| 23 | + |
| 24 | +* Takes one argument, a string: An object with the `identifier` of the new session configuration |
| 25 | +that is unique for your app. Your app can retrieve the download or the upload response later by creating a |
| 26 | +new background session with the same identifier. Also, you can set the `HTTPHeaderFields` property to |
| 27 | +specify additional HTTP header-fields. |
| 28 | + |
| 29 | +Returns a preconfigured session configuration object. This variable needs to be passed into the `createURLSession()` |
| 30 | +function to create a session for background download. |
| 31 | + |
| 32 | +#### createURLSession(sessionConfig) |
| 33 | + |
| 34 | +Creates a session with the specified session configuration. If the session configuration was created |
| 35 | +with the identifier of a existing session, then this function would return the pre-existing session. |
| 36 | + |
| 37 | +* Takes one argument, a session configuration object: Created using `createSessionConfiguration()`. |
| 38 | + |
| 39 | +Returns a session object. |
| 40 | + |
| 41 | +<strong>Important</strong>The session object keeps a strong reference until your app explicitly |
| 42 | +invalidates the session. If you do not invalidate the session by calling the invalidateAndCancel() |
| 43 | +or finishTasksAndInvalidate() method, your app leaks memory. |
| 44 | + |
| 45 | +## SessionConfiguration |
| 46 | + |
| 47 | +### Properties |
| 48 | + |
| 49 | +- `identifier` (String, creation-only) |
| 50 | +- `HTTPHeaderFields` (Array<String: String>) |
| 51 | + |
| 52 | +## Session |
| 53 | + |
| 54 | +### Methods |
| 55 | + |
| 56 | +#### backgroundTask(arguments) |
| 57 | + |
| 58 | +Creates a download task for the specified URL, within the provided session object and saves the results to a file. |
| 59 | +Once this function is called, the download starts automatically. The progress of the download can be monitored by listening |
| 60 | +to `downloadprogress` , `downloadcompleted`, `sessioneventscompleted` and `sessioncompleted` events explained below. |
| 61 | + |
| 62 | +* Takes an object of one argument: |
| 63 | + * url (String): The remote url used for this data task. |
| 64 | + |
| 65 | +#### uploadTask(arguments) |
| 66 | + |
| 67 | +Creates an upload task for the specified URL, within the provided session object and the file-blob to upload. |
| 68 | +Once this function is called, the upload starts automatically. The progress of the upload can be monitored by listening |
| 69 | +to `uploadprogress` and `sessioncompleted` events explained below. |
| 70 | + |
| 71 | +* Takes an object of arguments: |
| 72 | + * url (String): The remote url used for this data task. |
| 73 | + * data (TiBlob): The data blob used for this data task. |
| 74 | + * method (String): The request method (e.g. POST or PUT). Default: POST. |
| 75 | + * requestHeaders (Array<String: String>): Additional request headers to pass to the request. |
| 76 | + |
| 77 | +Returns the new created task's identifier number. |
| 78 | + |
| 79 | +#### finishTasksAndInvalidate() |
| 80 | + |
| 81 | +Invalidates the given session object, allowing any outstanding tasks to finish. |
| 82 | + |
| 83 | +This method returns immediately without waiting for tasks to finish. Once a session is |
| 84 | +invalidated, new tasks cannot be created in the session, but existing tasks continue until completion. |
| 85 | +Once invalidated, references to the events and callback objects are broken. Session objects cannot be reused. |
| 86 | +To cancel all outstanding tasks, call invalidateAndCancel() instead. |
| 87 | + |
| 88 | +* Takes one argument, a session object : the session which the user wants to invalidate. |
| 89 | + |
| 90 | +### invalidateAndCancel() |
| 91 | + |
| 92 | +Cancels all outstanding tasks and then invalidates the session object. |
| 93 | + |
| 94 | +Once invalidated, references to the events and callback objects are broken. Session objects cannot be reused. |
| 95 | +To allow outstanding tasks to run until completion, call finishTasksAndInvalidate() instead. |
| 96 | + |
| 97 | +### reset(callback) |
| 98 | + |
| 99 | +Empties all cookies, cache and credential stores, removes disk files and calls flush() internally. |
| 100 | +Calls the `callback` when the reset is finished. |
| 101 | + |
| 102 | +### flush(callback) |
| 103 | + |
| 104 | +Flushes storage to disk and clear transient network caches. |
| 105 | +Calls the `callback` when the flush is finished. |
| 106 | + |
| 107 | +## Events |
| 108 | + |
| 109 | +### downloadprogress |
| 110 | + |
| 111 | +Periodically informs the user about the download's progress. |
| 112 | + |
| 113 | +<strong>Important</strong>This event is exposed inside Ti.App.iOS Proxy. |
| 114 | +usage : |
| 115 | +```js |
| 116 | +Ti.App.iOS.addEventListener('downloadprogress', function(e) { |
| 117 | + // Handle the download progress |
| 118 | +}); |
13 | 119 | ``` |
| 120 | + |
| 121 | +The following event information will be provided: |
| 122 | + |
| 123 | +* taskIdentifier (Number) : The task identifier number for the download task that finished. |
| 124 | +* bytesWritten (Number) : The number of bytes transferred since the last time this delegate method was called. |
| 125 | +* totalBytesWritten (Number) : The total number of bytes transferred so far. |
| 126 | +* totalBytesExpectedToWrite (Number) : The expected length of the file, as provided by the Content-Length header. If this header was not provided, the value is zero. |
| 127 | + |
| 128 | +### downloadcompleted |
| 129 | + |
| 130 | +Informs the app that a download task has finished downloading. |
| 131 | + |
| 132 | +<strong>Important</strong>This event is exposed inside Ti.App.iOS Proxy. |
| 133 | +usage : |
| 134 | +```js |
| 135 | +Ti.App.iOS.addEventListener('downloadcompleted', function(e) { |
| 136 | + // Handle the completed download |
| 137 | +}); |
| 138 | +``` |
| 139 | + |
| 140 | +The following event information will be provided: |
| 141 | + |
| 142 | +taskIdentifier[int]: The task identifier number for the download task that finished. |
| 143 | +data[TiBlob](http://docs.appcelerator.com/titanium/latest/#!/api/Titanium.Blob) : The downloaded content as [blob](http://docs.appcelerator.com/titanium/latest/#!/api/Titanium.Blob) object |
| 144 | + |
| 145 | +### sessioneventscompleted |
| 146 | + |
| 147 | +If an application has received an `backgroundtransfer` event, this session event will be fired to indicate |
| 148 | +that all messages previously enqueued for this session have been delivered. At this time it is safe to |
| 149 | +invoke the previously stored completion handler |
| 150 | + |
| 151 | +<strong>Important</strong>This event is exposed inside Ti.App.iOS Proxy. |
| 152 | +usage : |
| 153 | +```js |
| 154 | +Ti.App.iOS.addEventListener('sessioneventscompleted', function(e) { |
| 155 | + // Handle completed session events |
| 156 | +}); |
| 157 | +``` |
| 158 | + |
| 159 | +### sessioncompleted |
| 160 | + |
| 161 | +Informs the app that the task finished transfering data. |
| 162 | + |
| 163 | +<strong>Important</strong>This event is exposed inside Ti.App.iOS Proxy. |
| 164 | +usage : |
| 165 | +```js |
| 166 | +Ti.App.iOS.addEventListener('sessioncompleted', function(e) { |
| 167 | + // Handle a finished transfer |
| 168 | +}); |
| 169 | +``` |
| 170 | + |
| 171 | +The following event information will be provided: |
| 172 | + |
| 173 | +* taskIdentifier (Number) : The task identifier number for the download or upload task that finished. |
| 174 | +* success (Boolean) : Indicates if the operation succeeded. Returns true if download or upload succeeded, false otherwise. |
| 175 | +* errorCode (Number) : The error code of the error, if any (potentially system-dependent). |
| 176 | +* message (String) : A string containing the localized description of the error. |
| 177 | + |
| 178 | +## Usage |
| 179 | + |
| 180 | +See Sample |
| 181 | + |
| 182 | +## Author |
| 183 | + |
| 184 | +Hans Knoechel / Sabil Rahim |
| 185 | + |
| 186 | +## Module History |
| 187 | + |
| 188 | +View the [change log](changelog.html) for this module. |
| 189 | + |
| 190 | +## Feedback and Support |
| 191 | + |
| 192 | +Please direct all questions, feedback, and concerns to [info@appcelerator.com](mailto:info@appcelerator.com?subject=iOS%20urlSesson%20Module). |
| 193 | + |
| 194 | +## License |
| 195 | +Copyright (c) 2010-Present by Axway Appcelerator. All Rights Reserved. Please see the LICENSE |
| 196 | +file included in the distribution for further details. |
0 commit comments