# Modules.URLSession.Session

The session object used to start new tasks.

Availability
6.1.0

# Overview

These APIs are supported on iOS 7 and later.

The NSURLSession class and related classes provide an API for downloading content. This API provides a rich set of delegate methods for supporting authentication and gives your app the ability to perform background downloads when your app is not running or, in iOS, while your app is suspended.

iOS Background Services guide (opens new window).

# Properties

# apiName READONLY

Availability
6.1.0
apiName :String

The name of the API that this proxy corresponds to.

The value of this property is the fully qualified name of the API. For example, Titanium.UI.Button returns Ti.UI.Button.


# bubbleParent

Availability
6.1.0
bubbleParent :Boolean

Indicates if the proxy will bubble an event to its parent.

Some proxies (most commonly views) have a relationship to other proxies, often established by the add() method. For example, for a button added to a window, a click event on the button would bubble up to the window. Other common parents are table sections to their rows, table views to their sections, and scrollable views to their views. Set this property to false to disable the bubbling to the proxy's parent.

Default: true


# configuration CREATION ONLY

Availability
6.1.0

The configuration used for this url session.

Configuration options for an Modules.URLSession.Session object.
When a session is created, a copy of the configuration object is made. you cannot modify the configuration of a session after it has been created.

The shared session uses the global singleton credential, cache and cookie storage objects.

An ephemeral session has no persistent disk storage for cookies, cache or credentials.

A background session can be used to perform networking operations on behalf of a suspended application, within certain constraints.

# Methods

# addEventListener

Availability
6.1.0
addEventListener(name, callback) void

Adds the specified callback as an event listener for the named event.

Parameters

Name Type Description
name String

Name of the event.

callback Callback<Titanium.Event>

Callback function to invoke when the event is fired.

Returns

Type
void

# applyProperties

Availability
6.1.0
applyProperties(props) void

Applies the properties to the proxy.

Properties are supplied as a dictionary. Each key-value pair in the object is applied to the proxy such that myproxy[key] = value.

Parameters

Name Type Description
props Dictionary

A dictionary of properties to apply.

Returns

Type
void

# dataTask

Availability
6.1.0
dataTask(args) String

Creates a data task for the specified URL, within the provided session object and local data.

An data task does not provide any additional functionality over a usual session task and its presence is merely to provide lexical differentiation from download and upload tasks.

Once this function is called, the task starts automatically. Once finished, the data task will call the sessioncompleted event containing infos about the response.

Parameters

Name Type Description
args UploadDataTaskType

An object representing the arguments to add a new task task.

Returns

Task's identifier number.

Type
String

# downloadTask

Availability
6.1.0
downloadTask(args) String

Creates a download task for the specified URL, within the provided session object and saves the results to a file.

Once this function is called, the download starts automatically. The progress of the download can be monitored by listening to the downloadprogress, downloadcompleted, sessioneventscompleted and sessioncompleted events.

Parameters

Name Type Description
args DownloadTaskType

An object representing the arguments to add a new background task.

Returns

Task's identifier number.

Type
String

# finishTasksAndInvalidate

Availability
6.1.0
finishTasksAndInvalidate() void

Invalidates the given session object, allowing any outstanding tasks to finish.

This method returns immediately without waiting for tasks to finish. Once a session is invalidated, new tasks cannot be created in the session, but existing tasks continue until completion. Once invalidated, references to the events and callback objects are broken. Session objects cannot be reused. To cancel all outstanding tasks, call invalidateAndCancel instead.

Returns

Type
void

# fireEvent

Availability
6.1.0
fireEvent(name[, event]) void

Fires a synthesized event to any registered listeners.

Parameters

Name Type Description
name String

Name of the event.

event Dictionary

A dictionary of keys and values to add to the Titanium.Event object sent to the listeners.

Returns

Type
void

# flush

Availability
6.1.0
flush(callback) void

Flushes storage to disk and clear transient network caches.

Parameters

Name Type Description
callback Callback<CompletedCallback>

Callback to be invoked when the reset completes.

Returns

Type
void

# invalidateAndCancel

Availability
6.1.0
invalidateAndCancel() void

Cancels all outstanding tasks and then invalidates the session object.

Once invalidated, references to the events and callback objects are broken. Session objects cannot be reused. To allow outstanding tasks to run until completion, call finishTasksAndInvalidate instead.

Returns

Type
void

# removeEventListener

Availability
6.1.0
removeEventListener(name, callback) void

Removes the specified callback as an event listener for the named event.

Multiple listeners can be registered for the same event, so the callback parameter is used to determine which listener to remove.

When adding a listener, you must save a reference to the callback function in order to remove the listener later:

var listener = function() { Ti.API.info("Event listener called."); }
window.addEventListener('click', listener);

To remove the listener, pass in a reference to the callback function:

window.removeEventListener('click', listener);

Parameters

Name Type Description
name String

Name of the event.

callback Callback<Titanium.Event>

Callback function to remove. Must be the same function passed to addEventListener.

Returns

Type
void

# reset

Availability
6.1.0
reset(callback) void

Empties all cookies, cache and credential stores, removes disk files, calls flush.

Parameters

Name Type Description
callback Callback<CompletedCallback>

Callback to be invoked when the reset completes.

Returns

Type
void

# uploadTask

Availability
6.1.0
uploadTask(args) String

Creates a upload task for the specified URL, within the provided session object and local data.

Once this function is called, the upload starts automatically. The progress of the upload can be monitored by listening to the uploadprogress and sessioncompleted events.

Parameters

Name Type Description
args UploadDataTaskType

An object representing the arguments to add a new upload task.

Returns

Task's identifier number.

Type
String