Skip to content
Titanium SDK
Version13.4.0

You are reading 13.4.0. See this page in 13.4.1.

Titanium.Network

moduleAndroidiPhoneiPadmacOSsince 0.1

Extends Titanium.ModuleTitanium.Proxy

The top level network module.

The Network module is used to access networking related functionality.

For TCP sockets, see Titanium.Network.Socket.TCP.

The legacy Titanium.Network.TCPSocket object is still required by the BonjourBrowser and BonjourService objects.

For all other socket needs, use Titanium.Network.Socket.TCP.

App Transport Security

Starting with iOS 9, Apple introduced new security and compatibility guidelines for networking and connectivity, which include:

  • All connections must support TLS 1.2 or greater, and use forward secrecy ciphers supported by iOS
  • Certificates must use at least a SHA-256 fingerprint with either a 2048-bit or greater RSA key, or a 256-bit or greater ECC key
  • Application must be compatible with IPv6 DNS64/NAT64 networks as part of App Store submissions

Because of the new requirements, Titanium disables the App Transport Security (ATS) requirements, which only includes the TLS and certificate cipher requirements, to allow all connections by default.

If you wish to re-enable ATS, add the NSAppTransportSecurity dict to the ios plist section of the tiapp.xml file, then add the NSAllowArbitraryLoads key and set it to false, indicating that you want to manage your whitelisted URL's yourself.

<ti:app>
  <!-- ... -->
  <ios>
    <plist>
      <dict>
        <key>NSAppTransportSecurity</key>
        <dict>
          <key>NSAllowsArbitraryLoads</key>
          <false/>
        </dict>
      </dict>
    </plist>
  </ios>
  <!-- ... -->
</ti:app>

Listed below are other guidelines from Apple.

Avoid hard-coded IPv4 address literals

Try not to include hard-coded IPv4 address literals in your application, for example, 127.0.0.1.

Whitelist URLs

If ATS is enabled and the web service you are trying to contact does not support the new security guidelines, iOS will not allow you to connect to the service. An error will be returned in the network response stating the connection failed because it did not meet the ATS policy standards.

To allow the connection, you will need to add a NSAppTransportSecurity dict to the ios plist section of the tiapp.xml file. In the NSAppTransportSecurity dict, you add the NSExceptionDomains dict to whitelist specific domains.

For more information, see iOS Developer Library: App Transport Security.

Example:

<ti:app>
  <!-- ... -->
  <ios>
    <plist>
      <dict>
        <key>NSAppTransportSecurity</key>
        <dict>
          <!-- 1) Default: Disable ATS completely -->
          <key>NSAllowsArbitraryLoads</key>
          <true/>

          <-- 2) Alternative: Define whitelisted domains -->
          <key>NSExceptionDomains</key>
          <dict>
            <!-- Loosen security measure for www.foo.com domain -->
            <key>www.foo.com</key>
            <dict>
              <!-- Set TLS v1.0 as the minimum TLS version -->
              <key>NSExceptionMinimumTLSVersion</key>
              [string](api:Global.String)TLSv1.0</string>
              <!-- Can use other ciphers besides forward secrecy ciphers -->
              <key>NSExceptionRequiresForwardSecrecy</key>
              <false/>
              <!-- Allow HTTP connections -->
              <key>NSExceptionAllowsInsecureHTTPLoads</key>
              <true/>
            </dict>
          </dict>
        </dict>
      </dict>
    </plist>
  </ios>
  <!-- ... -->
</ti:app>

Test IPV6 DNS64/NAT64

If you submit your application to the App Store, you should setup an IPv6 DNS64/NAT64 network to test your application to verify it is compatible with IPv6.

If you are running macOS 10.11 or greater, you can use your computer to setup a local IPv6 Wi-Fi network, which requires an Internet connection not through Wi-Fi.

To create the network, from System Preferences, enable Internet sharing with NAT64 network enabled to share your Wi-Fi connection.

For detailed instructions, see Mac Developer Library: Test for IPv6 DNS64/NAT64 Compatibility.

Properties 24

apiName

Stringsince Android 3.2.0, iPhone 3.2.0, iPad 3.2.0, macOS 9.2.0read-only

Inherited from Titanium.Proxy

The name of the API that this proxy corresponds to.

bubbleParent

Booleansince Android 3.0.0, iPhone 3.0.0, iPad 3.0.0, macOS 9.2.0

Inherited from Titanium.Proxy

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

Default: true

NETWORK_LAN

Numberread-only

A networkType value indicating that the device is communicating over a local-area network.

This constant is also a possible value for the networkType property of the change event.

NETWORK_MOBILE

Numberread-only

A networkType value indicating that the device is communicating over a mobile network.

This constant is also a possible value for the networkType property of the change event.

NETWORK_NONE

Numberread-only

A networkType value indicating that no network is available.

This constant is also a possible value for the networkType property of the change event.

NETWORK_UNKNOWN

Numberread-only

A networkType value indicating that the current network type is unknown.

This constant is also a possible value for the networkType property of the change event.

NETWORK_WIFI

Numberread-only

A networkType value indicating that the device is communicating over a WiFi network.

This constant is also a possible value for the networkType property of the change event.

networkTypeName

Stringread-only

Network type as a String. Returns one of NONE, WIFI, LAN, MOBILE, or UNKNOWN.

NOTIFICATION_TYPE_NEWSSTAND

NumberiPhoneiPadmacOSread-only

Constant value for a Newsstand style push notification. Only available on iOS 5 and later

online

Booleanread-only

Boolean value indicating if the device is connected to the network.

The online property is true if the device has network access via WiFi, LAN, or mobile/cellular network.

Note that if the device is connected to a private network without Internet access, then this property will be true as well. This means you cannot assume the device has Internet access if it's connected to the network.

PROGRESS_UNKNOWN

Numbersince 3.5.0read-only

Constant value specifying that the progress of a download can not be calculated.

remoteDeviceUUID

StringiPhoneiPadmacOSread-only

Remote device UUID if the device is registered with the Apple Push Notification Service, or null if it is not registered.

remoteNotificationTypes

Array<Number>iPhoneiPadmacOSread-only

Array of push notification type constants enabled for the application.

TLS_VERSION_1_0

Numbersince Android 3.6.0, iPhone 1.8.0.1, iPad 1.8.0.1read-only

Constant value specifying TLS version 1.0 for SSL.

TLS_VERSION_1_1

Numbersince Android 3.6.0, iPhone 1.8.0.1, iPad 1.8.0.1read-only

Constant value specifying TLS version 1.1 for SSL.

TLS_VERSION_1_2

Numbersince Android 3.6.0, iPhone 1.8.0.1, iPad 1.8.0.1read-only

Constant value specifying TLS version 1.2 for SSL.

TLS_VERSION_1_3

Numbersince 8.3.0read-onlyAndroid 10.0+, iOS 12.2+

Constant value specifying TLS version 1.3 for SSL.

Methods 23

Inherited from Titanium.Proxy

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

Parameters
ParametersTypeDescription
nameString

Name of the event.

callbackCallback<Titanium.Event>

Callback function to invoke when the event is fired.

4 fields
bubbles Boolean

True if the event will try to bubble up if possible.

cancelBubble Boolean

Set to true to stop the event from bubbling.

source Object

Source object that fired the event.

type String

Name of the event fired.

addHTTPCookie

since Android 3.2.0, iPhone 3.3.0, iPad 3.3.0

Adds a cookie to the HTTP client cookie store.

On iOS, the cookie will override an existing cookie with the same name, domain and path, if any. The cookie's expiryDate property must be a date later than the creating date, otherwise the cookie will not persist in the cookie storage. On Android, any existing cookie with the same domain and name will be replaced with the new cookie. This is a bug in BasicCookieStore because based on RFC6265, an existing cookie can be evicted and replaced only when it has the same cookie-name, domain-value and path-value with the new cookie.

Parameters
ParametersTypeDescription
cookieTitanium.Network.Cookie

The cookie to add

addSystemCookie

Androidsince 3.2.0

Adds a cookie to the system cookie store.

On Android, Titanium.UI.WebView uses the system cookie store. Any existing cookie with the same domain, path and name will be replaced with the new cookie.

Parameters
ParametersTypeDescription
cookieTitanium.Network.Cookie

The cookie to add

Returns a decoded version of a URI encoded value.

Parameters
ParametersTypeDescription
valueString

URI encoded input value to be decoded.

Returns String

Returns a URI encoded version of the specified URI component.

On iOS, this also escapes the following characters, which are allowed in a URL but may not be allowed or desirable inside a path component:

!*'();:@+$,/?%#[]=&
Parameters
ParametersTypeDescription
valueString

Input value to be encoded.

Returns String

Inherited from Titanium.Proxy

Fires a synthesized event to any registered listeners.

Parameters
ParametersTypeDescription
nameString

Name of the event.

event?Dictionary

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

getHTTPCookies

since Android 3.2.0, iPhone 3.3.0, iPad 3.3.0

Gets all the cookies with the domain, path and name matched with the given values from the HTTP client cookie store.

The domain matching and path matching follow RFC6265. If name is null, return all the cookies with the domain and path matched.

Parameters
ParametersTypeDescription
domainString

The domain of the cookie to get. It is case-insensitive.

pathString

The path of the cookie to get. It is case-sensitive.

nameString

The name of the cookie to get. It is case-sensitive.

getHTTPCookiesForDomain

since Android 3.2.0, iPhone 3.3.0, iPad 3.3.0

Gets all the cookies with the domain matched with the given values from the HTTP client cookie store.

The domain matching follows RFC6265.

Parameters
ParametersTypeDescription
domainString

The domain of the cookie to get. It is case-insensitive.

getSystemCookies

Androidsince 3.2.0

Gets all the cookies with the domain, path and name matched with the given values from the system cookie store.

On Android, Titanium.UI.WebView uses the system cookie store. The domain matching and path matching follow RFC6265. If name is null, return all the cookies with the domain and path matched. The returned cookies only have their name and value properties returned.

Parameters
ParametersTypeDescription
domainString

The domain of the cookie to get. It is case-insensitive.

pathString

The path of the cookie to get. It is case-sensitive.

nameString

The name of the cookie to get. It is case-sensitive.

Registers for push notifications with the Apple Push Notification Service.

Call this method to allow remote push notifications. It should usually be called on application start.

iOS Notes: Before calling this method, the application needs to call the Titanium.App.iOS.registerUserNotificationSettings method and pass it the notification types to use. Setting the notification types with this method will have no effect and throws a warning.

Android Notes: Make sure to also add the <uses-permission android:name="android.permission.POST_NOTIFICATIONS"/> permission in the <manifest> tag of your tiapp.xml. Example:

const win = Ti.UI.createWindow();
win.addEventListener("open", function() {
  console.log("Is enabled", Ti.Network.remoteNotificationsEnabled);

  Ti.Network.registerForPushNotifications({
    success: (e) => {
      console.log("success", e);
    },
    error: (e) => {
       console.log("error", e);
    }
  });
});
win.open();

The permission request will only be shown twice. After that it won't show up again and the user has to go to the app permissions to enable it.

Parameters
ParametersTypeDescription
configPushNotificationConfig

Dictionary specifying push-notification related options.

4 fields
callback Callback<PushNotificationData>

Callback function invoked upon receiving a new push notification.

error Callback<PushNotificationErrorArg>

Callback function called when an error occurs during registration.

success Callback<PushNotificationSuccessArg>

Callback function called when the push registration is successfully completed.

types Array<Number>

Array of NOTIFICATION_TYPE constants that the application would like to receive.

removeAllHTTPCookies

since Android 3.2.0, iPhone 3.3.0, iPad 3.3.0

Removes all the cookies from the HTTP client cookie store.

Inherited from Titanium.Proxy

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

Parameters
ParametersTypeDescription
nameString

Name of the event.

callbackCallback<Titanium.Event>

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

4 fields
bubbles Boolean

True if the event will try to bubble up if possible.

cancelBubble Boolean

Set to true to stop the event from bubbling.

source Object

Source object that fired the event.

type String

Name of the event fired.

removeHTTPCookie

since Android 3.2.0, iPhone 3.3.0, iPad 3.3.0

Removes the cookie with the domain, path and name exactly the same as the given values from the HTTP client cookie store.

Parameters
ParametersTypeDescription
domainString

The domain of the cookie to remove. It is case-insensitive.

pathString

The path of the cookie to remove. It is case-sensitive.

nameString

The name of the cookie to remove. It is case-sensitive.

removeHTTPCookiesForDomain

since Android 3.2.0, iPhone 3.3.0, iPad 3.3.0

Removes the cookies with the domain matched with the given values from the HTTP client cookie store.

The domain matching follows RFC6265.

Parameters
ParametersTypeDescription
domainString

The domain of the cookie to remove. It is case-insensitive.

removeSystemCookie

Androidsince 3.2.0

Removes the cookie with the domain, path and name exactly the same as the given values from the system cookie store.

On Android, Titanium.UI.WebView uses the system cookie store.

Parameters
ParametersTypeDescription
domainString

The domain of the cookie to remove. It is case-insensitive.

pathString

The path of the cookie to remove. It is case-sensitive.

nameString

The name of the cookie to remove. It is case-sensitive.

Events 1

Fired when network connectivity changes.

Event properties
Event propertiesTypeDescription
networkTypeNameString

New network type as a string.

onlineBoolean

Boolean indicating if the device is connected to the network.

networkTypeNumber

New network type

reasonString

Human-readable text describing the reason for the change.