Skip to content

Instantly share code, notes, and snippets.

@rborn
Forked from benbahrenburg/app.js
Created January 15, 2016 18:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rborn/ab8d7f90f2326cbccf38 to your computer and use it in GitHub Desktop.
Save rborn/ab8d7f90f2326cbccf38 to your computer and use it in GitHub Desktop.
Titanium Settings Dialog Examples
Ti.UI.setBackgroundColor('#000');
var alertSettings = require("settings-dialog");
var win = Ti.UI.createWindow({
title:'Example', backgroundColor:'#fff', layout:"vertical"
});
win.add(Ti.UI.createLabel({
text:"Example on now to prompt user to change settings",
left:15, right:15, height:45, top:50, color:"#000", font:{fontSize:18, fontWeight:"bold"}
}));
var networkButton = Ti.UI.createButton({
title:"Ask to check network settings",
left:15, right:15, height:45, top:50, color:"#000"
});
win.add(networkButton);
networkButton.addEventListener("click", function(){
alertSettings.prompt({
title:"Information",
message:"We can't find a network connection. Please check your settings.",
buttonNames:["Settings", "Continue"],
settingsType : alertSettings.SETTINGS_TYPE.NETWORK, //The type of prompt
settingsIndex : 0 //What button index should launch the settings
}, function(d){
console.log("prompt results = " + JSON.stringify(d));
});
});
var locationButton = Ti.UI.createButton({
title:"Ask to check Location settings",
left:15, right:15, height:45, top:50, color:"#000"
});
win.add(locationButton);
locationButton.addEventListener("click", function(){
alertSettings.prompt({
title:"Information",
message:"Please enable location services to use this feature.",
buttonNames:["Settings", "Continue"],
settingsType : alertSettings.SETTINGS_TYPE.LOCATION_SERVICES, //The type of prompt
settingsIndex : 0 //What button index should launch the settings
}, function(d){
console.log("prompt results = " + JSON.stringify(d));
});
});
win.open();
var _isAndroid = Ti.Platform.osname === 'android';
var DIALOG_TYPE = {
NETWORK : 0,
LOCATION_SERVICES :1
};
exports.SETTINGS_TYPE = DIALOG_TYPE;
var getActionByType = function(settingsType){
if(settingsType ===DIALOG_TYPE.NETWORK){
return "android.settings.WIRELESS_SETTINGS";
}else{
return "android.settings.LOCATION_SOURCE_SETTINGS";
}
};
exports.prompt = function(args, callback){
if(!args.hasOwnProperty("settingsType")){
throw "settingsType property is required";
}
if(!args.hasOwnProperty("settingsIndex")){
throw "settingsIndex property is required";
}
var ew = Ti.UI.createAlertDialog(args);
ew.addEventListener("click",function(e){
e.settingsSelected = (e.index == args.settingsIndex);
if(e.settingsSelected){
if(_isAndroid){
var actionType = getActionByType(args.settingsType);
var intent = Ti.Android.createIntent({action: args.actionType});
Ti.Android.currentActivity.startActivity(intent);
}else{
Ti.Platform.openURL(Ti.App.iOS.applicationOpenSettingsURL);
}
}
return callback(e);
});
ew.show();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment