1

I store the device token value with NSUserdefaults, and then, send this value to my server. My app checks if this value exists or not. If the value doesn't exist, my app tries to call the registerdevicetoken method.

But I found out an issue.

If a user restores his/her iPhone via iTunes, the NSUserdefaults values are recovered. However the stored device token is not a valid device token for a new device, but my app can't recognize that the phone has been reinstalled via iTunes.

How can I collect the correct device token, when users restore their iPhone?

Julian E.
  • 4,687
  • 6
  • 32
  • 49
o242
  • 51
  • 1
  • 7

1 Answers1

1

Okay. Let's take the document's folder approach. Firstly, let's take a look at the anatomy of an iOS app. As you see below there are 3 key folders. Apple Docs The complete Documents and Libray folders get backed up, unless you specifically call a command on a file not to. That's what we will do. Create a file that you can call firstLaunch.txt for example. Put this file into Library/Application support/. And set the noBackUp tag using: -[NSURL setResourceValue:forKey:error:] using the NSURLIsExcludedFromBackupKey key. This will prevent the file from backing up. Now, you can have a simple if-else statement checking if the file exists. If it does, that means that the app was already run and the token is up to date. If it does not, create a new user token and now create the file again, put it into the correct folder. To be clear, this is the only time you should be creating the file, as it will only get called on the first launch and after being restored. You can find more info and this image at the Apple Docs.

Hope that helps, Julian.

Julian E.
  • 4,687
  • 6
  • 32
  • 49