mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-16 20:11:24 +08:00
web client for iOS
This commit is contained in:
@ -1,5 +1,4 @@
|
|||||||
import image_module = require("Image/image");
|
import image_module = require("Image/image");
|
||||||
// TODO: Not implemented for iOS
|
|
||||||
|
|
||||||
export module tk {
|
export module tk {
|
||||||
export module web {
|
export module web {
|
||||||
@ -11,25 +10,42 @@ export module tk {
|
|||||||
* Downloads string from url.
|
* Downloads string from url.
|
||||||
*/
|
*/
|
||||||
public downloadString(url: string, successCallback: (result: string) => void, errorCallback?: (e: Error) => void) {
|
public downloadString(url: string, successCallback: (result: string) => void, errorCallback?: (e: Error) => void) {
|
||||||
try {
|
Client.getDataFromUrl(url, function (data) {
|
||||||
if (successCallback) {
|
if (successCallback) {
|
||||||
// successCallback(result);
|
successCallback(Foundation.NSString.initWithDataEncoding(data, 4));
|
||||||
}
|
}
|
||||||
} catch (ex) {
|
}, errorCallback);
|
||||||
|
|
||||||
if (errorCallback) {
|
|
||||||
errorCallback(ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public downloadImage(url: string, successCallback: (image: image_module.tk.ui.Image) => void, errorCallback?: (e: Error) => void) {
|
public downloadImage(url: string, successCallback: (image: image_module.tk.ui.Image) => void, errorCallback?: (e: Error) => void) {
|
||||||
try {
|
Client.getDataFromUrl(url, function (data) {
|
||||||
if (successCallback) {
|
if (successCallback) {
|
||||||
// successCallback(response);
|
var image = new image_module.tk.ui.Image();
|
||||||
|
image.loadFromData(data);
|
||||||
|
successCallback(image);
|
||||||
}
|
}
|
||||||
} catch (ex) {
|
}, errorCallback);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static getDataFromUrl(url: string, successCallback: (result: any) => void, errorCallback?: (e: Error) => void)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
var sessionConfig = Foundation.NSURLSessionConfiguration.defaultSessionConfiguration();
|
||||||
|
var queue = Foundation.NSOperationQueue.mainQueue();
|
||||||
|
var session = Foundation.NSURLSession.sessionWithConfigurationDelegateDelegateQueue(sessionConfig, null, queue);
|
||||||
|
var dataTask = session.dataTaskWithURLCompletionHandler(Foundation.NSURL.URLWithString(url), function (data, response, error) {
|
||||||
|
if (error) {
|
||||||
|
if (errorCallback) {
|
||||||
|
errorCallback(error.description);
|
||||||
|
}
|
||||||
|
} else if (successCallback) {
|
||||||
|
successCallback(data);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
dataTask.resume();
|
||||||
|
session.finishTasksAndInvalidate();
|
||||||
|
} catch (ex) {
|
||||||
if (errorCallback) {
|
if (errorCallback) {
|
||||||
errorCallback(ex);
|
errorCallback(ex);
|
||||||
}
|
}
|
||||||
|
20
declarations.ios.d.ts
vendored
20
declarations.ios.d.ts
vendored
@ -58,4 +58,24 @@ declare module Foundation {
|
|||||||
static defaultManager(): NSFileManager;
|
static defaultManager(): NSFileManager;
|
||||||
URLsForDirectoryInDomains(directory: number, mask: number): any;
|
URLsForDirectoryInDomains(directory: number, mask: number): any;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class NSString {
|
||||||
|
static initWithDataEncoding(data: any, encoding: any) : any;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class NSURLSessionConfiguration {
|
||||||
|
static defaultSessionConfiguration(): any;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class NSOperationQueue {
|
||||||
|
static mainQueue(): any;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class NSURLSession {
|
||||||
|
static sessionWithConfigurationDelegateDelegateQueue(config: any, param: any, queue : any): any;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class NSURL {
|
||||||
|
static URLWithString(url : string): any;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user