I have an iOS hybrid application written on IBM MFP 7.1 with angular. Currently I'm trying to use push notifications but the code never enters in onReadyToSubscribe method.
I get all the code from the documentation about the push notifications and still I have the problem.
My application-descriptor.xml is
<application xmlns="http://www.worklight.com/application-descriptor" id="B" platformVersion="7.1.0.00.20151227-1725">
<displayName>A</displayName>
<description>A</description>
<author>
<name>application's author</name>
<email>application author's e-mail</email>
<homepage>http://mycompany.com</homepage>
<copyright>Copyright My Company</copyright>
</author>
<mainFile>index.html</mainFile>
<features/>
<thumbnailImage>common/images/thumbnail.png</thumbnailImage>
<ipad bundleId="xxx.xxx.xxx" version="1.0" securityTest="PushSecurityTest" >
<worklightSettings include="false"/>
<pushSender password="123456"/>
<security>
<encryptWebResources enabled="false"/>
<testWebResourcesChecksum enabled="false" ignoreFileExtensions="png, jpg, jpeg, gif, mp4, mp3"/>
</security>
</ipad>
main.js file the one where we should have the magic
function wlCommonInit() {
PushAppRealmChallengeHandler.init();
WL.Client.connect({
onSuccess: connectSuccess,
onFailure: connectFailure
});
//---------------------------- Set up push notifications -------------------------------
if (WL.Client.Push) {
WL.Client.Push.onReadyToSubscribe = function() {
WL.SimpleDialog.show("Push Notifications", "onReadyToSubscribe", [ {
text : 'Close',
handler : function() {}
}
]);
$('#SubscribeButton').removeAttr('disabled');
$('#UnsubscribeButton').removeAttr('disabled');
WL.Client.Push.registerEventSourceCallback(
"myPush",
"PushAdapter",
"PushEventSource",
pushNotificationReceived);
};
}
}
function connectSuccess() {
WL.Logger.debug ("Successfully connected to MobileFirst Server.");
}
function connectFailure() {
WL.Logger.debug ("Failed connecting to MobileFirst Server.");
WL.SimpleDialog.show("Push Notifications", "Failed connecting to MobileFirst Server. Try again later.",
[{
text : 'Reload',
handler : WL.Client.reloadapp
},
{
text: 'Close',
handler : function() {}
}]
);
}
function loginButtonClicked() {
var reqURL = '/j_security_check';
var options = {
parameters : {
j_username : $('#usernameInputField').val(),
j_password : $('#passwordInputField').val()
},
headers: {}
};
PushAppRealmChallengeHandler.submitLoginForm(reqURL, options, PushAppRealmChallengeHandler.submitLoginFormCallback);
}
function isPushSupported() {
var isSupported = false;
if (WL.Client.Push){
isSupported = WL.Client.Push.isPushSupported();
}
alert(isSupported);
WL.SimpleDialog.show("Push Notifications", JSON.stringify(isSupported), [ {
text : 'Close',
handler : function() {}}
]);
}
function isPushSubscribed() {
var isSubscribed = false;
if (WL.Client.Push){
isSubscribed = WL.Client.Push.isSubscribed('myPush');
}
WL.SimpleDialog.show("Push Notifications", JSON.stringify(isSubscribed), [ {
text : 'Close',
handler : function() {}}
]);
}
// --------------------------------- Subscribe ------------------------------------
function doSubscribe() {
WL.Client.Push.subscribe("myPush", {
onSuccess: doSubscribeSuccess,
onFailure: doSubscribeFailure
});
}
function doSubscribeSuccess() {
WL.SimpleDialog.show("Push Notifications", "doSubscribeSuccess", [ {
text : 'Close',
handler : function() {}}
]);
}
function doSubscribeFailure() {
WL.SimpleDialog.show("Push Notifications", "doSubscribeFailure", [ {
text : 'Close',
handler : function() {}}
]);
}
//------------------------------- Unsubscribe ---------------------------------------
function doUnsubscribe() {
WL.Client.Push.unsubscribe("myPush", {
onSuccess: doUnsubscribeSuccess,
onFailure: doUnsubscribeFailure
});
}
function doUnsubscribeSuccess() {
WL.SimpleDialog.show("Push Notifications", "doUnsubscribeSuccess", [ {
text : 'Close',
handler : function() {}}
]);
}
function doUnsubscribeFailure() {
WL.SimpleDialog.show("Push Notifications", "doUnsubscribeFailure", [ {
text : 'Close',
handler : function() {}}
]);
}
//------------------------------- Handle received notification ---------------------------------------
function pushNotificationReceived(props, payload) {
WL.SimpleDialog.show("Push Notifications", "Provider notification data: " + JSON.stringify(props), [ {
text : 'Close',
handler : function() {
WL.SimpleDialog.show("Push Notifications", "Application notification data: " + JSON.stringify(payload), [ {
text : 'Close',
handler : function() {}}
]);
}}
]);
}
And the last magic js file handles the authentication on the MFP server
var PushAppRealmChallengeHandler = (function(){
var challengeHandler;
function init() {
challengeHandler = WL.Client.createChallengeHandler("PushAppRealm");
challengeHandler.isCustomResponse = isCustomResponse;
challengeHandler.handleChallenge = handleChallenge;
challengeHandler.submitLoginFormCallback = submitLoginFormCallback;
}
function isCustomResponse(response) {
if (!response || response.responseText === null) {
return false;
}
var indicatorIdx = response.responseText.search('j_security_check');
if (indicatorIdx >= 0){
return true;
}
return false;
}
function handleChallenge(response) {
$('#AppBody').hide();
$('#AuthBody').show();
$('#passwordInputField').val('');
}
function submitLoginFormCallback(response) {
var isLoginFormResponse = challengeHandler.isCustomResponse(response);
if (isLoginFormResponse){
challengeHandler.handleChallenge(response);
} else {
$('#AppBody').show();
$('#AuthBody').hide();
challengeHandler.submitSuccess();
}
}
function submitLoginForm(url, options, callback) {
challengeHandler.submitLoginForm(url, options, callback)
}
return {
init: init,
submitLoginForm: submitLoginForm,
submitLoginFormCallback: submitLoginFormCallback
}
})();
I already checked the certificate and it is okay, also I redeploy everything when I add the certificate.
Do you have some ideas where I can have a problem?
When onReadyToSubscribe should be called?
Is it related with the authentication of the application?
Thanks in advance
This was an issue with Apple Sandbox APNs not providing token as reported in the following links:
https://forums.developer.apple.com/message/155239#155239
https://forums.developer.apple.com/thread/52224
Related
When using the geolcoation api in a react-native-webview, I am asked twice whether the app is allowed to use my current location. How is it possible to forward the already (not) granted permission to the webview?
I am currently using react-native 0.68 and react-native-webview 11.22.
First prompt:
Second prompt:
I should only be asked once for permission to use my current geolocation.
In case somebody faces the same problem, I solved this issue with the following workaround. I injected a custome javacscript into the webview to override the used geolocation api in the webview. My custome script does all the communication with the app. The app returns the geolocation and so the webview doesn't need to aks for permission.
Custome Script
export const getGeoLocationJS = () => {
const getCurrentPosition = `
navigator.geolocation.getCurrentPosition = (success, error, options) => {
window.ReactNativeWebView.postMessage(JSON.stringify({ event: 'getCurrentPosition', options: options }));
window.addEventListener('message', (e) => {
let eventData = {}
try {
eventData = JSON.parse(e.data);
} catch (e) {}
if (eventData.event === 'currentPosition') {
success(eventData.data);
} else if (eventData.event === 'currentPositionError') {
error(eventData.data);
}
});
};
true;
`;
const watchPosition = `
navigator.geolocation.watchPosition = (success, error, options) => {
window.ReactNativeWebView.postMessage(JSON.stringify({ event: 'watchPosition', options: options }));
window.addEventListener('message', (e) => {
let eventData = {}
try {
eventData = JSON.parse(e.data);
} catch (e) {}
if (eventData.event === 'watchPosition') {
success(eventData.data);
} else if (eventData.event === 'watchPositionError') {
error(eventData.data);
}
});
};
true;
`;
const clearWatch = `
navigator.geolocation.clearWatch = (watchID) => {
window.ReactNativeWebView.postMessage(JSON.stringify({ event: 'clearWatch', watchID: watchID }));
};
true;
`;
return `
(function() {
${getCurrentPosition}
${watchPosition}
${clearWatch}
})();
`;
};
Webview
import Geolocation from '#react-native-community/geolocation';
let webview = null;
<WebView
geolocationEnabled={ true }
injectedJavaScript={ getGeoLocationJS() }
javaScriptEnabled={ true }
onMessage={ event => {
let data = {}
try {
data = JSON.parse(event.nativeEvent.data);
} catch (e) {
console.log(e);
}
if (data?.event && data.event === 'getCurrentPosition') {
Geolocation.getCurrentPosition((position) => {
webview.postMessage(JSON.stringify({ event: 'currentPosition', data: position }));
}, (error) => {
webview.postMessage(JSON.stringify({ event: 'currentPositionError', data: error }));
}, data.options);
} else if (data?.event && data.event === 'watchPosition') {
Geolocation.watchPosition((position) => {
webview.postMessage(JSON.stringify({ event: 'watchPosition', data: position }));
}, (error) => {
webview.postMessage(JSON.stringify({ event: 'watchPositionError', data: error }));
}, data.options);
} else if (data?.event && data.event === 'clearWatch') {
Geolocation.clearWatch(data.watchID);
}
}}
ref={ ref => {
webview = ref;
if (onRef) {
onRef(webview)
}
}}
source={ url }
startInLoadingState={ true }
/>
I am trying to send VoIP push notifications directly to an iOS device from an App server using the NodeJS package called sns-mobile and Amazon SNS API.
However, When I try to send VoIP pushes using the below code, here is the error message that I get. Could someone please suggest me where I am going wrong, I have been spending nearly half a day to resolve this.
Invalid parameter: JSON must contain an entry for 'default' or
'APNS_VOIP
var iOSApp = new SNS({
platform: SNS.SUPPORTED_PLATFORMS.IOS,
region: 'us-west-2',
apiVersion: '2010-03-31',
accessKeyId: 'XXXXXXXXXXXXX',
secretAccessKey: 'XXXXXXXXXXXXX',
platformApplicationArn: 'arn:aws:sns:us-west-2:3303035XXXXX:app/APNS_VOIP/VoIPPushesApp'
});
iOSApp.addUser('deviceID',
JSON.stringify({
"APNS_VOIP": JSON.stringify({aps:{alert:"Hello and have a good day."}})
})
, function(err, endpointArn) {
if(err) {
console.log("The Error is :****: "+JSON.stringify(err, null, 4));
throw err;
}
// Send a simple String or data to the client
iOSApp.sendMessage(endpointArn, 'Hi There!', function(err, messageId) {
//iOSApp.sendMessage(endpointArn, messageTest, function(err, messageId) {
if(err) {
console.log("The Error in end message is :****: "+JSON.stringify(err, null, 4));
throw err;
}
console.log('Message sent, ID was: ' + messageId);
});
});
Here is the code that send a VoIP notifications to the receiver device using its device VoIP token. When a VoIP notification is received, the device calls a function called, didReceiveIncomingPushWithPayload
var AWS = require('aws-sdk');
// Amazon SNS module
AWS.config.update({
accessKeyId : 'XXXXXXXXXXXXXXXX',
secretAccessKey : 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
region : 'us-west-2'
});
var amazonSNS = new AWS.SNS();
// 1. Create Platform Endpoint
var createPlatformEndpoint = function(req, res, next){
var deviceVoipToken = req.deviceVoipToken; // Obtaining the device VoIP token from the request object
amazonSNS.createPlatformEndpoint({
// App in Sandboxmode (ie running on device directly from Xcode)
PlatformApplicationArn: "arn:aws:sns:us-west-2:xxxxxxxxxxxx:app/APNS_VOIP_SANDBOX/CurieVoip",
// App in Production mode (ie running on device after archiving and installed on device with a provisioning profile)
//PlatformApplicationArn: "arn:aws:sns:us-west-2:xxxxxxxxxxxx:app/APNS_VOIP/CurieVoip",
Token: deviceVoipToken
}, function(err, data) {
if (err) {
console.log(err.stack);
response.json({"status": "failure", "statusCode" : 402})
return;
}
var endpointArn = data.EndpointArn;
req.endpointArn = data.EndpointArn; // Passing the EndpointArn to the next function
next()
})
}
// 2. Publish notification
var publishVoipNotification = function(req, res, next){
var endpointArn = req.endpointArn;
var payload = {
default : 'Hello World, default payload',
APNS_VOIP : {
aps: {
alert: 'Hi there',
sound: 'default',
badge: 1
}
}
};
// first have to stringify the inner APNS object...
payload.APNS = JSON.stringify(payload.APNS);
// then have to stringify the entire message payload
payload = JSON.stringify(payload);
console.log('sending push');
amazonSNS.publish({
MessageStructure : 'json',
Message : payload,
TargetArn : endpointArn
}, function(err, data) {
if (err) {
console.log("Error stack: "+err.stack);
var message = "There has been an error in Publishing message via AmazonSNS with error: "+err.stack;
res.json({"status": "failure", "statusCode" : 402, "message" : message})
return;
}
next();
});
}
// 3. Deleting platform endpoint after sending a voipNotification; Ref: http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/SNS.html#deleteEndpoint-property
var deleteEndpointArn = function(req, res){
var endpointArn = req.endpointArn;
var roomName = req.roomName;
var params = {
EndpointArn: endpointArn /* required */
};
amazonSNS.deleteEndpoint(params, function(err, data) {
if (err){
var message = "Unable to deleteEndpointArn, with error: "+err.stack;
res.json({"status": "failure", "statusCode" : 400, "message":message})
}
else{
var message = "Deleted endpointArn successfully";
res.json({"status": "success", "statusCode" : 200, "message":message})
}
});
}
router.post('/sendVoipNotificationToReceiver', [createPlatformEndpoint, publishVoipNotification, deleteEndpointArn]);
Go to Amazon SNS
Click on Mobile: Push notifications
Click on Create platform Application,
User Apple Credentials "Used for development in sandbox" check this checkbox if you want to test voip notification on sandbox.
Push certificate type -- Voip Push certificate
Upload Certificate .P12 files.
Save Form and copy ARN
Create Lambda function. Code example below
// Load the AWS SDK for Node.js
var AWS = require('aws-sdk');
// Set region
AWS.config.update({region: 'us-east-1'});
var amazonSNS = new AWS.SNS();
const isset = (s) => {
return typeof s !== typeof undefined ? true : false;
};
exports.handler = (event, context, callback) => {
let data = event['body-json'];
if (isset(data)) {
let getAction = data['action'];
let userToken = data['token'];
let webPayload = data['payload'];
if (isset(getAction) && getAction == 'APNS_VOIP') {
amazonSNS.createPlatformEndpoint({
PlatformApplicationArn: 'YOUR APPLICATION ARN',
Token: userToken
}, function (err, data1) {
if (err) {
//handle error
console.log(err)
} else {
//Publish notification
var endpointArn = data1.EndpointArn;
console.log("endpointArn",endpointArn);
var payload = {
default: 'Silent voip push notification',
APNS_VOIP: {
//aps:webPayload
"aps" : {
"alert" : {
"title" : "Call Request"
},
"data":webPayload,
"content-available":1,
"category":"GENERAL"
}
}
};
payload.APNS_VOIP = JSON.stringify(payload.APNS_VOIP);
payload = JSON.stringify(payload);
amazonSNS.publish({
MessageStructure: 'json',
Message: payload,
MessageAttributes:{
"AWS.SNS.MOBILE.APNS.PRIORITY":{"DataType":"String","StringValue":"10"},
"AWS.SNS.MOBILE.APNS.PUSH_TYPE":{"DataType":"String","StringValue":"voip"}
},
TargetArn: endpointArn
}, function (err, data2) {
if (err) {
//handle error
console.log(err)
} else {
var params = {
EndpointArn: endpointArn /* required */
};
//Deleting platform endpoint after sending a voipNotification;
amazonSNS.deleteEndpoint(params, function (err, data3) {
if (err) {
//handle error
//console.log(err);
callback(null, {
"status": "error",
"data": [],
"message": []
});
} else {
//code success
console.log("delete",data3);
callback(null, {
"status": "Success",
"data": data3,
"message":"notification sent successfully"
});
}
});
}
});
}
});
}
else if (isset(getAction) && getAction == 'APNS_VOIP_SANDBOX') {
amazonSNS.createPlatformEndpoint({
PlatformApplicationArn: 'YOUR APPLICATION ARN',
Token: userToken
}, function (err, data1) {
if (err) {
//handle error
console.log(err)
} else {
//Publish notification
var endpointArn = data1.EndpointArn;
console.log("endpointArn",endpointArn);
var payload = {
default: 'Silent voip push notification',
APNS_VOIP_SANDBOX: {
"aps" : {
"alert" : {
"title" : "Call Request"
},
"data":webPayload,
"content-available":1,
"category":"GENERAL"
}
}
};
payload.APNS_VOIP_SANDBOX = JSON.stringify(payload.APNS_VOIP_SANDBOX);
payload = JSON.stringify(payload);
amazonSNS.publish({
MessageStructure: 'json',
Message: payload,
MessageAttributes:{
"AWS.SNS.MOBILE.APNS.PRIORITY":{"DataType":"String","StringValue":"10"},
"AWS.SNS.MOBILE.APNS.PUSH_TYPE":{"DataType":"String","StringValue":"voip"}
},
TargetArn: endpointArn
}, function (err, data2) {
if (err) {
//handle error
console.log(err)
} else {
var params = {
EndpointArn: endpointArn /* required */
};
//Deleting platform endpoint after sending a voipNotification; Ref: http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/SNS.html#deleteEndpoint-property
amazonSNS.deleteEndpoint(params, function (err, data3) {
if (err) {
//handle error
//console.log(err);
callback(null, {
"status": "error",
"data": [],
"message": []
});
} else {
//code success
console.log("delete",data3);
callback(null, {
"status": "Success",
"data": data3,
"message":"notification sent successfully"
});
}
});
}
});
}
});
}
else {
callback(null, {
"status": "error",
"data": [],
"message": "You have not posted valid data."
});
}
}
};
https://docs.aws.amazon.com/sns/latest/dg/sns-send-custom-platform-specific-payloads-mobile-devices.html
How Do I send VoIP push notifications to an iOS device using Amazon SNS in Node JS
UPDATE: I was getting this error because of a bug that Parse-Server 2.2.17 has. I fixed it by going back to 2.2.16.
Does anyone know why I am getting this error? Here is my cloud code:
`Parse.Cloud.define("Messages", function(request, response) {
var pushQuery = new Parse.Query(Parse.Installation);
Parse.Push.send({
where: pushQuery,
data: {
alert: "New Event Added",
sound: "default"
}
},{
success: function(){
console.log("Push Sent!")
},
error: function (error) {
console.log(error)
},
useMasterKey: true
});
});`
Here is the error I am getting:
And then this is how I am calling the code: `PFCloud.callFunctionInBackground("Messages", withParameters: nil) { (object, error) in
if error == nil {
print("Success!")
} else {
print(error)
}
}
index.js:
`
Can you please try the following code:
var query = new Parse.Query(Parse.Installation);
// query condition (where equal to .. etc.)
var payload = {
alert: "New Event Added",
sound: "default"
};
Parse.Push.send({
where: query, // Set our Installation query
data: payload
}, {
success: function () {
},
error: function (error) {
// Handle error
}
});
Please notice that i remove the useMasterKey if you want to add useMasterKey you need to insert it inside closures but for me it's works without the useMasterKey
the useMasterKeyVersion should look like the following:
Parse.Push.send({
where: query, // Set our Installation query
data: payload
},
{
useMasterKey: true
},
{
success: function () {
},
error: function (error) {
// Handle error
}
});
The promises version (according to best practices):
Parse.Push.send({where: query,data: payload})
.then(function(){
// success
},function(error){
// error ..
});
Update
by looking at your index.js file it looks like you didn't add the facebook oauth to your 3-party authentication logins.
so you will need to add the following:
oauth: {
facebook: {
appIds: "FACEBOOK APP ID"
}
}
below your emailAdapter config and inside "FACEBOOK APP ID" put the app ID that you created in facebook developers
in main.js put this code
// SEND PUSH NOTIFICATION
Parse.Cloud.define("push", function(request, response) {
var user = request.user;
var params = request.params;
var someKey = params.someKey
var data = params.data
var recipientUser = new Parse.User();
recipientUser.id = someKey;
var pushQuery = new Parse.Query(Parse.Installation);
pushQuery.equalTo("userID", someKey);
Parse.Push.send({
where: pushQuery, // Set our Installation query
data: data
}, { success: function() {
console.log("#### PUSH OK");
}, error: function(error) {
console.log("#### PUSH ERROR" + error.message);
}, useMasterKey: true});
response.success('success');
});
// SEND PUSH NOTIFICATION FOR ANDROID
Parse.Cloud.define("pushAndroid", function(request, response) {
var user = request.user;
var params = request.params;
var someKey = params.someKey
var data = params.data
var recipientUser = new Parse.User();
recipientUser.id = someKey;
var pushQuery = new Parse.Query(Parse.Installation);
pushQuery.equalTo("userID", someKey);
Parse.Push.send({
where: pushQuery, // Set our Installation query
data: {
alert: data
}
}, { success: function() {
console.log("#### PUSH OK");
}, error: function(error) {
console.log("#### PUSH ERROR" + error.message);
}, useMasterKey: true});
response.success('success');
});
in xcode project do like that to send push notification
// Send Push notification
let pushStr = "#\(PFUser.current()![USER_USERNAME]!) | \n\(self.lastMessageStr)"
let data = [ "badge" : "Increment",
"alert" : pushStr,
"sound" : "bingbong.aiff",
] as [String : Any]
let request = [
"someKey" : self.userObj.objectId!,
"data" : data
] as [String : Any]
PFCloud.callFunction(inBackground: "push", withParameters: request as [String : Any], block: { (results, error) in
if error == nil {
print ("\nPUSH SENT TO: \(self.userObj[USER_USERNAME]!)\nMESSAGE: \(pushStr)\n")
} else {
print ("\(error!.localizedDescription)")
}
})
I'm trying to recive push from gcm on the ios.
I have registered my application there: https://developers.google.com/mobile/add?platform=ios&cntapi=gcm&cnturl=https:%2F%2Fdevelopers.google.com%2Fcloud-messaging%2Fios%2Fclient&cntlbl=Continue%20Adding%20GCM%20Support&%3Fconfigured%3Dtrue
uploaded p12 certificates, added obtained GoogleService-Info.plist to the root of ios xcode project.
var params = {
"android": {"senderID": APP.senderId},
"ios": {"alert": "true", "badge": "true", "sound": "true", "senderID": APP.senderId},
"windows": {}
};
var push = PushNotification.init(params);
push.on('registration', function(data) {
console.log(data);
...
});
push.on('notification', function(data) {
console.log(data);
...
});
push.on('error', function(data) {
console.log(data);
});
registration is successful.
Then I'm trying to send push notification from my server:
public boolean send(String recipient, PushNotification notification) {
LOG.info("sending message :" + recipient);
Message message = new Message.Builder()
.addData("title", notification.getTitle())
.addData("message", notification.getMessage())
.priority(Message.Priority.HIGH)
.build();
try {
Result result = sender.send(message, recipient, RETRIES_COUNT);
LOG.debug("result : " + result);
if (result.getSuccess() != null && result.getSuccess() > 0) {
return true;
}
if (result.getFailure() != null) {
LOG.error("Error: " + result.getErrorCodeName());
}
return false;
} catch (Exception e) {
LOG.error(e);
return false;
}
}
}
I get the following in the log :
16-06-06 20:28:32,868 INFO : [pool-2-thread-1] sending message :k3YUHDkEaR0:APA91bGSc9Wc3dvrS1-6EKBPL4Duu_xZDIujiUZjhkxaaHz3BS3n_ZciuQZLfFiEszXAX4kNUu7Oq0555jI3zzrgIVeNKtD0p63ftH6BPuhOXkCk-ujHlBTi9gSPRXJ6ixd_kTyCfrwS
16-06-06 20:28:33,246 DEBUG: [pool-2-thread-1] result : [ messageId=0:1465234217220513%ca70b4fbf9fd7ecd ]
But on the phone I get nothing. What I did wrong?
I am working on an app for iOS and Android, and I am using Cordovas File plugin to save some data to a file on the device.
This file will contain information about the user, so he/she does not have to log in every time they use the app, so the information has to be there even if the app is exited and relaunched.
On Android this works fine:
window.requestFileSystem(LocalFileSystem.PERSISTENT, 20*1024*1024, app.onFileSystemSuccess, fail);
On Android the data I save is there next time I open the app, but on iOS the file is empty (or not existing).
Does this not work for iOS?
Here is the whole source:
var app = {
initialize: function()
{
this.bindEvents();
},
bindEvents: function()
{
document.addEventListener('deviceready', this.onDeviceReady, false);
},
onDeviceReady: function()
{
app.initAPP();
},
initAPP: function()
{
window.requestFileSystem(LocalFileSystem.PERSISTENT, 20*1024*1024, app.onFileSystemSuccess, fail);
},
onFileSystemSuccess: function(FS)
{
FS.root.getDirectory('MyAPPDir', {create:true}, app.gotDir, fail);
},
gotDir: function(Dir)
{
app.appRoot = Dir;
try{
app.appRoot.getFile('authInfo.txt', {create:true}, function(fileEntry){
fileEntry.file(function(file){
alert('Got file authInfo.txt')
var Reader = new FileReader();
Reader.onloadend = function(e){
alert(e.target.result);
if( e.target.result == '' ){
$('#container').load('login.html');
}
else{
document.authInfo = e.target.result;
alert(e.target.result);
alert('You are now auhtorized');
}
}
Reader.readAsText(file);
})
})
} catch(e){
$('#container').load('login.html');
}
},
authUser: function()
{
email = $("#email").val();
password = $("#password").val();
$.ajax({
url: AUTH_URL,
type: 'POST',
data:{
email: email,
password: password
},
success: function(data){
resp = $.parseJSON(data);
if( resp.status == 'success' ){
try{
app.appRoot.getFile('authInfo.txt', {create:true}, function(fileEntry){
fileEntry.createWriter(function(writer){
writer.truncate(0);
writer.onwriteend = function(){
writer.write(resp);
writer.onwriteend = function(){
alert('Wrote '+JSON.stringify(resp)+' to '+fileEntry.toURL());
}
}
});
});
}
catch (e){
alert(e);
}
} else {
alert(resp.message);
}
}
});
}
};
function fail(a){
alert(a);
}
I get this message in iOS:
Wrote {.......myjson.....} to cdvfile://localhost/persistent/MyAPPDir/authInfo.txt
But then when it tries to read it on launch it seems like the file does'nt exist any more, or is empty?