mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-16 11:42:04 +08:00
web client for iOS
This commit is contained in:
@ -1,5 +1,4 @@
|
||||
import image_module = require("Image/image");
|
||||
// TODO: Not implemented for iOS
|
||||
|
||||
export module tk {
|
||||
export module web {
|
||||
@ -11,25 +10,42 @@ export module tk {
|
||||
* Downloads string from url.
|
||||
*/
|
||||
public downloadString(url: string, successCallback: (result: string) => void, errorCallback?: (e: Error) => void) {
|
||||
try {
|
||||
Client.getDataFromUrl(url, function (data) {
|
||||
if (successCallback) {
|
||||
// successCallback(result);
|
||||
successCallback(Foundation.NSString.initWithDataEncoding(data, 4));
|
||||
}
|
||||
} catch (ex) {
|
||||
|
||||
if (errorCallback) {
|
||||
errorCallback(ex);
|
||||
}
|
||||
}
|
||||
}, errorCallback);
|
||||
}
|
||||
|
||||
public downloadImage(url: string, successCallback: (image: image_module.tk.ui.Image) => void, errorCallback?: (e: Error) => void) {
|
||||
try {
|
||||
Client.getDataFromUrl(url, function (data) {
|
||||
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) {
|
||||
errorCallback(ex);
|
||||
}
|
||||
|
20
declarations.ios.d.ts
vendored
20
declarations.ios.d.ts
vendored
@ -58,4 +58,24 @@ declare module Foundation {
|
||||
static defaultManager(): NSFileManager;
|
||||
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