Last active
December 27, 2015 04:49
-
-
Save srahim/7269313 to your computer and use it in GitHub Desktop.
error when trying to use the completion handlers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/************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