Skip to content

Commit

Permalink
feat(ios): support new APIs timeoutForResource and waitsForConnectivi…
Browse files Browse the repository at this point in the history
…ty (#11897)

Fixes TIMOB-28078
  • Loading branch information
vijaysingh-axway committed Aug 28, 2020
1 parent f8de8c0 commit 09f20d2
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 4 deletions.
56 changes: 56 additions & 0 deletions apidoc/Titanium/Network/HTTPClient.yml
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,34 @@ description: |
<Titanium.Network.getHTTPCookies>, <Titanium.Network.getHTTPCookiesForDomain>, <Titanium.Network.getSystemCookies>,
<Titanium.Network.removeHTTPCookie>, <Titanium.Network.removeHTTPCookiesForDomain>, <Titanium.Network.removeAllHTTPCookies>,
<Titanium.Network.removeSystemCookie>, <Titanium.Network.removeAllSystemCookies>.
#### Connect to local network in iOS
On iOS 14 and later, while connecting to local network a prompt will be shown to request user's permission.
Add key `NSLocalNetworkUsageDescription` to the `ios plist` section of the tiapp.xml file to show the message on prompt.
If this key is not added default message will be shown in prompt.
Example:
``` xml
<ti:app>
<!-- ... -->
<ios>
<plist>
<dict>
<!-- Reason to access local network-->
<key>NSLocalNetworkUsageDescription</key>
<string>
Specify the reason for accessing the local network.
This appears in the alert dialog when asking the user
for permission to access local network.
</string>
</dict>
</plist>
</ios>
<!-- ... -->
</ti:app>
```
extends: Titanium.Proxy
since: "0.1"

Expand Down Expand Up @@ -536,6 +564,34 @@ properties:
On iOS, the default is 15000 (15 seconds).
type: Number

- name: timeoutForResource
summary: |
The maximum amount of time (in milliseconds) that a resource request should be allowed to take.
description: |
The resource timeout controls how long (in milliseconds) to wait for an entire resource to transfer before giving up.
The resource timer starts when the request is initiated and counts until either the request completes or this timeout is reached, whichever comes first.
For more detail see [timeoutintervalforresource](https://developer.apple.com/documentation/foundation/nsurlsessionconfiguration/1408153-timeoutintervalforresource?language=objc)
from Apple documentation.
This property also applies if <Ti.Network.HttpClient.waitsForConnectivity> is set to `true`.
default: |
The default is `7*24*60*60*1000` milliseconds (7 days).
type: Number
platforms: [iphone, ipad]
since: '9.2.0'

- name: waitsForConnectivity
summary: |
A Boolean value that indicates whether the session should wait for connectivity to become available, or fail immediately.
description: |
Causes tasks to wait for network connectivity to become available, rather than immediately failing
with an error when it is not. When waiting for connectivity, the <Ti.Network.HttpClient.timeout>
property does not apply, but the <Ti.Network.HttpClient.timeoutForResource> property does.
default: false
type: Boolean
platforms: [iphone, ipad]
osver: {ios: {min: "11.0"}}
since: '9.2.0'

- name: username
summary: Sets the username parameter for authentication credentials.
description: Must be set before calling [open](Titanium.Network.HTTPClient.open).
Expand Down
10 changes: 10 additions & 0 deletions iphone/Classes/TiNetworkHTTPClientProxy.m
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,16 @@ - (void)send:(id)args
if ([self valueForUndefinedKey:@"timeout"]) {
[httpRequest setTimeout:[TiUtils doubleValue:[self valueForUndefinedKey:@"timeout"] def:15000] / 1000];
}

if ([self valueForUndefinedKey:@"timeoutForResource"]) {
httpRequest.timeoutForResource = [TiUtils doubleValue:[self valueForUndefinedKey:@"timeoutForResource"] def:7 * 24 * 60 * 60 * 1000] / 1000;
}

if ([self valueForUndefinedKey:@"waitsForConnectivity"]) {
httpRequest.waitsForConnectivity = [TiUtils boolValue:[self valueForUndefinedKey:@"waitsForConnectivity"]
def:NO];
}

if ([self valueForUndefinedKey:@"autoRedirect"]) {
[httpRequest setRedirects:
[TiUtils boolValue:[self valueForUndefinedKey:@"autoRedirect"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ typedef NS_ENUM(NSInteger, APSRequestError) {
@property (nonatomic, assign, readwrite) BOOL cancelled;
@property (nonatomic, assign, readwrite) NSURLRequestCachePolicy cachePolicy;
@property (nonatomic, assign, readonly, getter=isReady) BOOL ready;

@property (nonatomic, assign, readwrite) BOOL waitsForConnectivity;
@property (nonatomic, assign, readwrite) NSTimeInterval timeoutForResource;
/*!
@discussion Set to YES to block the caller's thread for the duration
of the network call. In this case the queue property is ignored. The
Expand All @@ -61,14 +62,14 @@ typedef NS_ENUM(NSInteger, APSRequestError) {
/*!
@discussion An optional NSOperationQueue for delegate callbacks.
The default value is nil, which means delegate callbakcs occur on
the caller's thread if the synchronous property is NO. If the
the caller's thread if the synchronous property is NO. If the
synchronous property is YES then this property is ignored.
*/
@property (nonatomic, strong, readwrite) NSOperationQueue *theQueue;

/*!
@discussion An optioanl array of run loop modes for delegate calllbacks
on the run loop of the caller's thread. The default is one element
@discussion An optional array of run loop modes for delegate callbacks
on the run loop of the caller's thread. The default is one element
array containing NSDefaultRunLoopMode. This is an advanced property,
and is ignored if synchronous is YES or theQueue is not nil. It is
the caller's responsibility to keep the thread and the run loop alive.
Expand Down
Binary file not shown.
22 changes: 22 additions & 0 deletions tests/Resources/ti.network.httpclient.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -735,4 +735,26 @@ describe('Titanium.Network.HTTPClient', function () {
xhr.open('GET', 'https://www.google .com/'); // URL with space
xhr.send();
});

it.ios('#timeoutForResource', function (finish) {
const xhr = Ti.Network.createHTTPClient({
timeout: 6e4,
timeoutForResource: 50
});

xhr.onload = _e => finish(new Error('onload shouldn\'t fire. Resource request timeout should reach before transferring entire resource.'));

xhr.onerror = e => {
try {
// Resource timeout is very less (50 milliseconds) so it should timeout before whole resource transferred
should(e.code).eql(Ti.UI.URL_ERROR_TIMEOUT);
} catch (err) {
return finish(err);
}
finish();
};

xhr.open('GET', 'https://www.google.com/');
xhr.send();
});
});

0 comments on commit 09f20d2

Please sign in to comment.