-1

I want to check whether or not my application was previously installed on a particular device. Is there any programmatic way to do so?

Stonz2
  • 6,306
  • 4
  • 44
  • 64
Ronak Chaniyara
  • 5,335
  • 3
  • 24
  • 51
  • you want to check inside app or server side can involve ? – MOHAMMAD ISHAQ Feb 19 '16 at 12:06
  • 3
    You can keep a flag in the keychain. – Desdenova Feb 19 '16 at 12:11
  • @Desdenova's recommendation is technically correct. In comparison to the `NSUserDefaults` being cleared when the app is uninstalled, the Keychain items for the app are NOT being cleared. However, the Keychain was not designed with this purpose in mind. – Alex Feb 19 '16 at 12:12
  • If you use case is that you want to perform something once for each user (like a discount coupon or whatever) you can do the following: Implement authentication with a server. Have the user login on the iOS device. If the login happens for the first time (you can check this on the server) then it's safe to perform your 1 time only action. Using this doesn't let you know if the same user logged in the 2nd time from a different device than he used the first time. Also, if you just want to know that it's the first time the user installed the app on a device, you cannot use this. – Alex Feb 19 '16 at 12:57
  • yes, as suggested by Radu Matei but it will require internet connection and also it is not device specific but, thanks@Alex @Radu Matei – Ronak Chaniyara Feb 19 '16 at 13:07

3 Answers3

2

OPTION #1: You can use NSUserDefaults or save the flag to keychain, in order to not lose it after app uninstall, and check in application:didFinishLaunchingWithOptions: method:

BOOL installFlag = get it from user defaults or keychain;
if (installFlag)
{
    //App was installed
}
else
{
    //This is the first install ever
    installFlag = YES;
    //Here save the YES value to user defaults or keychain
}

If you will save the flag to keychain, then use a Keychain Wrapper, like this one.

The weak part of this option is that even if keeping the flag in keychain, the value could be lost, after reseting device settings, that's why I would prefer the following option:

OPTION #2: Keep the flag remotely on a server database, as a key you can use device identifier:

NSString *uniqueIdentifier = [[[UIDevice currentDevice] identifierForVendor] UUIDString];

And you will have to check, if there is a record in your database with current device identifier, then app was already installed, if no then add the flag and the identifier to the database. (But you will need internet connection every time).

iOS Dev
  • 4,143
  • 5
  • 30
  • 58
  • 3
    If i uninstall App NSUserDefault gets cleared.so it will be not useful – Rohit Pradhan Feb 19 '16 at 12:10
  • 3
    This is incorrect if the scenario is app was installed, uninstalled and then installed again. The NSUserDefaults will be cleared when the app is uninstalled. – Alex Feb 19 '16 at 12:11
  • It wasn't specified in question nothing about uninstalling, then instead of user defaults, you can save the BOOL in the keychain. – iOS Dev Feb 19 '16 at 12:12
  • i want to check app installed previously or not, your solutions doesn't work for that@Radu Matei – Ronak Chaniyara Feb 19 '16 at 12:14
  • can you please provide some code snippet to save flag in Keychain? @Radu Matei – Ronak Chaniyara Feb 19 '16 at 12:21
  • what if i use the advertisingIdentifier and not showing any advertisements in app. can apple reject app?@Radu Matei – Ronak Chaniyara Feb 19 '16 at 12:31
  • @RonakChaniyara, yes, they do that very often – iOS Dev Feb 19 '16 at 12:34
  • @RaduMatei i think identifierForVendor will not same every time it only same when user have atleast one application of that vendor. if there is no application of that vendor then uniqueIdentifier for same application will be different after reinstall. – Jaydeep Patel Feb 19 '16 at 13:12
2

You can use keychain. but when user reset his/her phone this value will be lost . I think there is no option is available to such case except you have to use iAd in your app. apple provide unique id for that that will remain constant after reinstall application.

Jaydeep Patel
  • 1,699
  • 17
  • 30
  • 1
    A comment on this answer http://stackoverflow.com/a/3884925/2616185 confirms that the keychain gets reset when the user goes into Settings -> General -> Reset -> Erase all Content and Settings – Alex Feb 19 '16 at 12:29
1

You can use keychain to keep log of your app previously installed or not.

  • can you please provide some code snippet to save flag in Keychain? @Akshay Hastekar – Ronak Chaniyara Feb 19 '16 at 12:21
  • 1
    @RonakChaniyara Check Apple's sample code here https://developer.apple.com/library/ios/samplecode/GenericKeychain/Introduction/Intro.html regarding using the Keychain – Alex Feb 19 '16 at 12:23