I need to add one piece of functionality which relates to local storage when the app loads the first time after its downloaded from the app store. I have developed my app using only using jquerymobile and cordova
What I need is some code to achieve the following
Once the app is downloaded onto the iphone / ipad and its opened the app needs to check for the presence of a local storage value called AppID
If appID value does not exist in local storage the app needs to generate a random AppID that is in the following format xxx-xxx-xxx-xxx-xxx-xxx where the x values can be numbers or characters
The randomly generated AppId then needs to be placed in local storage so that it remains in place even after the iphone is rebooted and is only deleted if the app is deleted from the phone.
On the home page of the App I then need to have the AppID value retrieved from local storage displayed
If anyone can directly me to sample code that achieves the above or has already written code that achieves the above i would be very greatful
Use the following code in delegate class method didFinishLaunchingWithOptions
if ([[NSUserDefaults standardUserDefaults] intForKey:#"appID"]==0)
{
//first time launch
//generate an appID and save
[[NSUserDefaults standardUserDefaults] setInteger:app_id forKey:#"appID"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
else
{
//has launched before or app_id has been saved before, retrieve app_id
app_id=[[NSUserDefaults standardUserDefaults] intForKey:#"appID"]
}
As far as I know localStorage in not persistent with ios. ios can delete localStorage whenever it needs memory. You can just use a database like webSQL to save your app_id and check at the beginning of the app that it is set or not and if set,what it's value.
Related
I'm writing a feature of the language change. First screen of the app I'm writing is about setting the language. When he user presses the button, my app directs user to app settings. I expect user to set some language and show it to them as soon as they go back to my app.
But as soon as I click on some language option, my app xcode console says:
Message from debugger: Terminated due to signal 9
Additionally, as soon as I return to my app, it gets loaded as if I closed and reopened it.
I thought that maybe I can have some code that will set a global variables like "currentLanguage" and "previousLanguage" and if those are different - I will load localisation files for "currentLanguage" and will start downloading localised videos from my server.
System terminate your app when user change language in Settings. So you can use NSUserDefaults for persistent storage and compare this values on app start, to detect if language was changed.
Example:
NSString *currentLanguage = [[NSLocale preferredLanguages] firstObject];
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject: currentLanguage forKey:#"previousLanguage"];
NSString *previousLanguage = [userDefaults objectForKey:"previousLanguage"];
This is how it is accomplished in my app:
I keep the data in iCloud.
I need to do some time consuming thing for users who already used my app. But this will also start for completely new users... they just downloaded my app from AppStore for the first time. And this is not expected.
How can I determine that the app is running, but downloaded from AppStore for the first time?
What I could do, but I did not:
put any boolean data in the Keychain, and then check if it exists.
Since you have no non-app end to distinguish users Keychain as you mentioned is your only app persistance option that will survive app uninstalling. Only device Reset to factory settings would remove it.
you can use NSUserDefaults . if appDelegate.m ->didFinishLaunchingWithOptions use this code :
NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
if([userDefaults objectForKey:#"first_time"] == nil){
[userDefaults setObject:#"1" forKey:#"first_time"];
//do whatever you need
}
this condition will satisfied only once after installation .
I wondered if there are a way to be notified when a new application is installed in my device and trigger some treatment in my applications. Like PACKAGE_ADDED BroadcastReceiver in android
Thank's
Apple does not provide such information. Instead, you could add something like:
if([[NSUserDefaults standardUserDefaults] objectForKey:#"firstRun"] == nil)
{
//This is a first run
[[NSUserDefaults standardUserDefaults] setObject:#"NotFirstRun" forKey:#"firstRun"];
}
to applicationDidFinishLaunchingWithOptions:
update
If you instead want to detect the installation of another app the approach is different:
You can't detect when an app is installed, but for SOME apps you can detect if they are installed.
Doing your own app launch (or perhaps using a backgroundfetch process) you can see if an app is installed IF the app responds to a URL Scheme AND you know that scheme.
Take a look at
([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:customURL]])
If this methode returns YES for a given URL Scheme you know that app is installed. Here is a partial list of URL Schemes for some common apps or search http://handleopenurl.com/. You can of course also make your own URL Scheme if you want to detect your own app.
No. Apple doesn't give you this data.
What you can do, however, is maintain an user defaults data and manipulate in applicationDidFinishLaunchingWithOptions: delegate.
I've used a library called 'GBVersionTracking' for detect when is his first launch.
Importing this library you can send messages to the class 'GBVersionTracking' like:
[GBVersionTracking isFirstLaunchEver];
[GBVersionTracking isFirstLaunchForVersion];
[GBVersionTracking isFirstLaunchForBuild];
https://github.com/lmirosevic/GBVersionTracking
I'm trying to make a Core Data persistence store logic where:
If it's the first time the user is launching the app, all the data gets generated inside the app and gets loaded (and when he/she exists app, that's the first time the data is getting saved to the persistent store)
If it's not the first time the user is launching the app, then all the data gets loaded from existing Core Data persistent store.
How would you check if it's the first time the user is launching the app?
You can store the information with NSUserDefaults.
You set a variable to TRUE when the app launches for the first time.
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
if([userDefaults boolForKey:#"notFirstLaunch"] == false)
{
//do stuff on first launch.
[userDefaults setBool:YES forKey:#"notFirstLaunch"];
[userDefaults synchronize];
}
You can also store the app Version to update the database on App updates.
This tutorial is big help.
Core Data on iOS 5 Tutorial: How To Preload and Import Existing Data
Is there a way to get the date when the user downloaded / installed the first time an app via Facebook ? In my app , in the first launch you have to connect with Facebook. So I wonder , is there a way to get the date of this first connection.
I found nothing interesting during my research on internet..
Thanks !
Here is multiple way to do this.
1.> You can store it on server end along with device UUID at login time.When you login first time with facebook via a remote connection , at server end you can put a check that this UUID is aready exist or not. If it is not then you can insert this UUID along with current date and time into server end database.
2.> You can store it locally in NSUserDefaults . Whenever you login with facebook , just check NSUserDefaults value via this code
if (![[NSUserDefaults standardUserDefaults] valueForKey:#"firstTimeFaceBookLogin"])
{
[[NSUserDefaults standardUserDefaults] setValue:[NSDate date] forKey:#"firstTimeFaceBookLogin"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
NSLog(#"current date ===%#",[[NSUserDefaults standardUserDefaults] valueForKey:#"firstTimeFaceBookLogin"]);
Now NSUserDefaults value any where in your app via firstTimeFaceBookLogin key. But if you delete your application from device then this NSUserDefaults would be clear and again create a store new value after installing it in your device.
I hope you understand it. Please let me inform if you still having any confusion. Thanks