appcelerator/titanium_mobile — pull request #6527 by cheekiatng (2014-12-26 09:16:11+00:00)
Title: [TIMOB-17502] iOS: URLSession DownloadCompleted event now returns file url instead of TiBlob

JIRA: https://jira.appcelerator.org/browse/TIMOB-17502

*To test:*
```
// Require in the urlSession module
var urlSession = require("com.appcelerator.urlSession");
var session;

// App UI
var win = Ti.UI.createWindow({backgroundColor:"white"});
var progress = Ti.UI.createProgressBar({
width: 200,
height: 50,
min: 0,
max: 1,
value: 0,
style: Ti.UI.iPhone.ProgressBarStyle.PLAIN,
top: 10,
message: 'Downloading image URL',
font: { fontSize: 12, fontWeight: 'bold'},
color: '#888'
});
win.add(progress);
var imageView = Ti.UI.createImageView({
top:150,
height:300,
width:200
});
win.add(imageView);

var button = Ti.UI.createButton({
title:'Download Image (url)',
height:40,
width:200,
top:70
});

button.addEventListener('click', function()
{
// Check if the device is running iOS 8 or later, before registering for local notifications
if (Ti.Platform.name == "iPhone OS" && parseInt(Ti.Platform.version.split(".")[0]) >= 8) {
Ti.App.iOS.registerUserNotificationSettings({
types: [
Ti.App.iOS.USER_NOTIFICATION_TYPE_ALERT,
Ti.App.iOS.USER_NOTIFICATION_TYPE_SOUND,
Ti.App.iOS.USER_NOTIFICATION_TYPE_BADGE
]
});
}
// Create a session configuration
// The string parameter is an arbitrary string used to identify the session in events
var sessionConfig = urlSession.createURLSessionBackgroundConfiguration("com.appcelerator.test");
// Create a session
session = urlSession.createURLSession(sessionConfig);
// Create a background download task to get the asset with the URL
//urlSession.backgroundDownloadTaskWithURL(session,"https://raw.github.com/appcelerator-developer-relations/KitchenSink/master/Resources/images/dog@2x~iphone.jpg"); //small file
//urlSession.backgroundDownloadTaskWithURL(session,"http://imgsrc.hubblesite.org/hu/db/images/hs-2004-07-a-full_jpg.jpg"); //medium sized file
urlSession.backgroundDownloadTaskWithURL(session,"http://download.thinkbroadband.com/1GB.zip"); //huge file
progress.show();
});

win.add(button);
win.open();

// Monitor this event to receive updates on the progress of the download
Ti.App.iOS.addEventListener("downloadprogress", function(e) {
// Update the progress indicator
progress.value = (e.totalBytesWritten/e.totalBytesExpectedToWrite);
});

// Monitor this event to know when the download completes
Ti.App.iOS.addEventListener("downloadcompleted", function(e) {
Ti.API.info("download completed " + JSON.stringify(e));

// Copy file from cache
var cacheFile = Ti.Filesystem.getFile(e.url);
var downloadedFile = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, cacheFile.getName());
downloadedFile.write(cacheFile.read()); // copy and save from cache
Ti.API.info("downloadedFile: " + downloadedFile.getNativePath());
// set new Image
// imageView.setImage(downloadedFile.read());
// Invalidate the session and cancel current session tasks
urlSession.invalidateAndCancel(session);

// Notify the user the download is complete if the application is in the background
var notification = Ti.App.iOS.scheduleLocalNotification({
alertBody: "Download completed!",
badge: 1,
sound: "/alert.wav",
date: new Date(new Date().getTime())
});
progress.hide();
});

// Monitor this event to know when all session tasks have completed
Ti.App.iOS.addEventListener('sessioncompleted', function(e) {
Ti.API.info("sessioncompleted " + JSON.stringify(e));
if (e.success) {
alert("Downloads completed successfully.");
}
});
```
- Create a new Project
- Double-click the Project's "tiapp.xml" file to open it in the Overview tab.
- In the Modules section, click the Add button (green plus sign) to open the Mobile Modules dialog.
- Locate and select "com.appcelerator.urlSession". This module is part of the Titanium SDK since Release 3.2.0
- Click OK.
- Copy the code segment above to the project "app.js" file
- Run the Project