Skip to content

Instantly share code, notes, and snippets.

@FokkeZB
Last active August 23, 2017 16:26
Show Gist options
  • Save FokkeZB/5487984 to your computer and use it in GitHub Desktop.
Save FokkeZB/5487984 to your computer and use it in GitHub Desktop.
Update lib for Titanium
var update = require('update');
// Set texts
update.title = 'Yeah, new version: %version'; // %version wll be replaced by new version number
update.message = 'In %version you will have:\n\n%notes'; // %notes will be replaced by the release notes
update.yes = 'I want it';
update.later = 'Maybe later';
update.never = 'Do not ask me again (for this version)';
// Set triggers
update.daysBetween = 1; // Check every day (as long as the test-method gets called)
// Set iTunes Track ID manually (will use bundle ID via Ti.App.id otherwise)
update.iTunesTrackId = 123456;
// Reset triggers (WARNING: Will also reset the user's wish to never be asked - for this version - again!)
update.reset();
// Test for triggers and if so then check for new version
update.test();
// Check for new version (regardless of triggers)
update.check();
// Open app in App Store
update.open();
require('update').test(); // Put me somewhere where I get called regularly
function test() {
return _trigger();
}
function check() {
return _trigger(true);
}
function open() {
if (!exports.iTunesTrackId) {
_lookup(function (result) {
if (!result) {
Ti.API.debug('[UPDATE] Lookup failed.');
return;
}
exports.iTunesTrackId = result.trackId;
return open();
});
return;
}
Ti.Platform.openURL('http://itunes.apple.com/app/id' + exports.iTunesTrackId);
return;
}
function reset() {
Ti.API.debug('[UPDATE] Reset.');
Ti.App.Properties.removeProperty('update_checked');
Ti.App.Properties.removeProperty('update_never');
return;
}
function _trigger(forced) {
if (Ti.Platform.name !== 'iPhone OS') {
Ti.API.debug('[UPDATE] Platform is not iOS.');
return;
}
var now = (new Date() / 1000);
if (!forced) {
var checked = Ti.App.Properties.getInt('update_checked', 0);
if (now - checked < (exports.daysBetween * 86400)) {
Ti.API.debug('[UPDATE] Checked less than ' + exports.daysBetween + ' days ago.');
return;
}
}
_lookup(function (result) {
if (!result) {
Ti.API.debug('[UPDATE] Lookup failed.');
return;
}
Ti.App.Properties.setInt('update_checked', now);
if (_cmpVersion(result.version, Ti.App.version) <= 0) {
Ti.API.debug('[UPDATE] No new version.');
return;
}
if (!forced) {
var never = Ti.App.Properties.getString('update_never');
if (never === result.version) {
Ti.API.debug('[UPDATE] Never ask again for this version.');
return;
} else {
Ti.App.Properties.removeProperty('update_never');
}
}
var buttonNames, cancel;
if (forced) {
buttonNames = [exports.later, exports.yes];
cancel = 0;
} else {
buttonNames = [exports.yes, exports.later, exports.never];
cancel = 2;
}
var title, messsage;
if (exports.title) {
title = exports.title;
if (exports.title.indexOf('%') !== -1) {
title = title.replace('%version', result.version);
}
}
if (exports.message) {
message = exports.message;
if (exports.message.indexOf('%') !== -1) {
message = exports.message
.replace('%version', result.version)
.replace('%notes', result.releaseNotes);
}
}
var alertDialog = Titanium.UI.createAlertDialog({
title: title,
message: message,
buttonNames: buttonNames,
cancel: cancel
});
alertDialog.addEventListener('click', function(e) {
if (buttonNames[e.index] === exports.yes) {
open();
} else if (buttonNames[e.index] === exports.never) {
Ti.App.Properties.setString('update_never', result.version);
}
return;
});
alertDialog.show();
return;
});
return;
}
function _lookup(_callback) {
var xhr = Ti.Network.createHTTPClient({
onload: function (e) {
if (xhr.status === 200 && this.responseText) {
try {
var json = JSON.parse(this.responseText);
if (json.resultCount === 1) {
_callback(json.results[0]);
return;
} else {
Ti.API.error('[UPDATE] LOOKUP ERROR ' + this.responseText);
}
} catch (err) {
Ti.API.error('[UPDATE] LOOKUP ERROR ' + JSON.stringify(err));
}
}
_callback();
return;
},
onerror: function (e) {
Ti.API.error('[UPDATE] LOOKUP ERROR ' + JSON.stringify(e.error));
_callback();
return;
}
});
var url = 'http://itunes.apple.com/lookup?';
if (exports.iTunesTrackId) {
url = url + 'id=' + exports.iTunesTrackId;
} else {
url = url + 'bundleId=' + Ti.App.id;
}
xhr.open('GET', url);
xhr.send();
return;
}
function _cmpVersion(a, b) {
var i, cmp, len, re = /(\.0)+[^\.]*$/;
a = (a + '').replace(re, '').split('.');
b = (b + '').replace(re, '').split('.');
len = Math.min(a.length, b.length);
for( i = 0; i < len; i++ ) {
cmp = parseInt(a[i], 10) - parseInt(b[i], 10);
if( cmp !== 0 ) {
return cmp;
}
}
return a.length - b.length;
}
exports.title = 'New version available';
exports.message = 'Upgrade to :version for:\n\n%notes';
exports.yes = 'Yes';
exports.later = 'Not now';
exports.never = 'No, thank you';
exports.iTunesTrackId = null;
exports.daysBetween = 3;
exports.test = test;
exports.check = check;
exports.open = open;
exports.reset = reset;

I like easy drop-in CommonJS libraries that need minimal configuration to perform common tasks.

This CommonJS library checks with Apple every X days to see if there's a new version available. If so, it asks the user if he/she would like to update and then opens the app in the App Store.

At minimum, the library requires no configuration at all and just a single line of code.

Look at advanced.js for all available options and methods.

Also check out my rating library: https://gist.github.com/FokkeZB/5466012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment