Skip to content

Instantly share code, notes, and snippets.

@srahim
Last active December 27, 2015 04:49
Show Gist options
  • Save srahim/7269313 to your computer and use it in GitHub Desktop.
Save srahim/7269313 to your computer and use it in GitHub Desktop.
error when trying to use the completion handlers
/************app.hjs******/
@import("Foundation");
@import("UIKit");
exports.backgroundDownloadTaskWithURL = function(identifier, url, callback) {
if (identifier != null) {
var sessionConfig = NSURLSessionConfiguration.backgroundSessionConfiguration(identifier);
var session = NSURLSession.sessionWithConfiguration(sessionConfig);
var taskurl = NSURL.URLWithString(url);
function completionHandler(data, response, error) {
if (!error) {
console.log("[DEBUG] Completed with data task with response :", response);
var value = NSString.alloc().initWithData(data,NSUTF8StringEncoding);
callback(value);
} else {
console.log("[WARN] Session completed with error :", error);
}
}
var task = session.downloadTaskWithURL(taskurl, completionHandler);
task.resume();
} else {
console.log("[WARN] Need to specify a proper identifier to create a URLSession.");
}
}
/***********app.js***************/
var keyWindow = Ti.UI.createWindow({backgroundColor:"white"});
keyWindow.open();
var module = require("com.appcelerator.urlSession");
function callback(data) {
Ti.API.info("Response Recieved");
alert(data);
}
module.backgroundDownloadTaskWithURL("uniquevalue","http://api.openweathermap.org/data/2.5/weather?lat=37.389587&lon=-122.05037",callback);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment