IOS temporarily save data during app background refresh - ios

I am building an app where we can record videos but it requires recording certain events due to which the app is closed and when the app is opened again , all the data is lost. Tiktok is using a similar approach where they save the data temporarily on the device storage and shows an alert when the app is opened again. Cant find the solution of what they are using to save data temporarily on device.
This is how they are doing it - tiktok method

The question isn't clear to me. But I assume that you want to record something and get back at the time of relaunching the application.
The best way you can do:
First, continue recording until app goes to the background
Second, in appDidEnterBackground
a) please save the file to a unique name in the document directory/temp directory with a unique name. The second one is the best place to do. Then save the filename to the userdefaults.
or
b) Instead of doing the file save, save the Data to the userdefaults.
Third, in didBecomeActive
a) For 2a, if there is a name, try to fetch the file from the directory.
b) For 2b, if there is data available in userdefaults, try to show an alert that there is something in the userdefaults.
You have thousands of answers in StackOverflow for all of those steps.

Related

how can I reset an iOS app to it's initial state when the user logs out

Is there a simple way for a user to restore an iOS app to it's original state on the device when the user signs out of the app?
I am currently manually deleting all the coreData objects and dismissing all viewControllers and clearing NSUserDefaults.
What I want to achieve is exactly the app state when the user first downloads and interacts with it. Is there a simpler way to do this?
There is no magic step to accomplish what you want , you have to manually clear any content that you have set , also you can search for the optimum way to do that for example instead of deleting the userdefauts key by key you can delete all the content with 1 line

Saving app settings - iOS

I have an iOS app which loads certain features depending on the settings that the user sets. Currently I am using NSUseDefaults to save and retrieve these settings and it works fine. But from what I understand anyone can view and edit them with a simple XML editor. You don't even need to jailbreak an iOS device to gain access to them. So they arn't very secure.
I was wandering if anyone could give me some advice on how I can go about saving app settings (these are NONE secure settings, no passwords, just simple things like ints and strings).
Here are a few ideas I had:
IDEA 1 Add a JSON file to the app NSBundle and then edit/save that JSON file every time you want to load/change the app settings.
IDEA 2 Use Keychain - it can store strings right? And it can't be accessed or edited by anyone. (hopefully even the NSA... lol). I could just store an array of strings in keychain for my app settings.
IDEA 3 Store the settings on a server and get the app to pull them down for the user every time they use the app.
IDEA 4 If NSUserDefaults supports this, then maybe locking the NSUserDefaults so that the end user can only view them but not edit them. Only the app will be able to edit them.
The main point is that I am worried that if I use NSUSerDefaults, the user may see them and edit them and then the app will not function properly. While I am not storing any kind of secure data, it would be nice if I can prevent the user from editing the app settings.
Are any of these approaches any good?
Thanks for your time, Dan.
All of them are good ideas, but just one issue with the first one:
You can not write or change files in the main bundle.
As long a the sure did not jailbreak their device the NSUserDefault can not easily be changed.
The keychain should only be used for password, token, etc..
In you case the NSUserDefault will do just fine, or just save the some file the documents directory of your is also an option. You can even create you now settings class that conforms the to NSCoding protocol and you can save it.

iOS Assets Library: permanent unique key for photos(identify photos between multiple runs)?

Say I have an app which allows user to pick some images from assets library.
When running for the 1st time, user picks some photo, at the 2nd run, when user wants to pick photos, I'd like to set the photos user picked last time as already selected.
How do I identify photos between multiple runs of my app?
iOS apps usually don't get terminated, but get sent to background - state will be unchanged when getting back to foreground
For case your app get's terminated anyway, you can store data in NSUserDefaults, CoreData, the file system or the keychain. In your case it sounds like NSUserDefaults would be a good choice. Don't forget to restore last states on app start.

What to do when the user enables or disables the iCloud support in the app Settings Bundle?

I am targeting iOS7 and I use iCloud with an UIManagedDocument. I would like to know what to do in these two cases:
1) First case: App first launch
It's the first launch, when we do not know yet if the user wants to use iCloud or not and we should assume he doesn't want to use it until we ask him. Once we'll find out, his choice will be stored in the app Settings Bundle and he will be able to change his mind later.
When my app launch, the first view that appears triggers the UIManagedDocument init with [[UIManagedDocument alloc] initWithFileURL:url]. It does this because it should know how many rows it has to display in its table.
At this point, it is the first launch, and I still have to ask to the user, I assume he does not want to use iCloud so the UIManagedDocument is initialized without the persistentStoreOptions set (NSPersistentStoreUbiquitousContentNameKey and NSPersistentStoreUbiquitousContentURLKey).
The file is then created on the disk with
[UIManagedDocument saveToURL: URL
forSaveOperation: UIDocumentSaveForCreating
completionHandler: ^(BOOL success){
if(success){
//Do something with the document
}
}];
then the view appears. Since it is the first launch, I should ask the user if he wants to use iCloud or not.
I show an UIAlertView from [viewDidAppear:animated]
If he answers "NO", everything is already ok. But what should I do if he says "YES"??
Should I close the UIManagedDocument and migrate the DB or is it enough to set the NSPersistentStoreUbiquitousContentNameKey and NSPersistentStoreUbiquitousContentURLKey in the document's persistentStoreOptions?
2) Second case: a couple of days later the user changes his mind
The user did choose to enable iCloud but after some day he changes his mind. He switches to NO the iCloud option in the app settings bundle. When the app launches I check for a change in this option and I find that it has switched from YES to NO. As I understood from the hints I got on SO, the correct path should be to display an UIAlertView asking him if he wants:
a) Keep the documents on the device
b) Delete the documents from the device
c) Keep using iCloud
What should i do for the options a) and b)?
A DB migration for option a) and recreate the DB from zero for option b)?
The way you're using the term "migration" is a little confusing, as incrementing the Core Data model from one version to a new one is also called migration. I assume you're just talking about "migrating" from iCloud to local only or vice versa.
In your first case, I wouldn't create the UIManagedDocument until the user has made a choice. If the user wants to use iCloud, all you have to do is take that and open it and you don't have to worry about closing/removing the local document (which is now pointless).
In the second case, if the user chooses option A, copy the file from iCloud to local, then remove it from iCloud. Option B means "remove from iCloud, I don't want it anymore". If you still want to continue using Core Data locally, create a new document for it. C, of course, would do nothing, just dismiss the alert.
Typically there are two places to check whether iCloud settings have changed and/or to check or open any files:
1) in the AppDelegate method application:didfinishlaunchingwithoptions, and
2) in the AppDelegate method applicationwillenterforeground
In addition if you register for iCloud account change notifications you can respond to them.
This allows you to organise things before your view starts trying to display any core data items. Opening or Migrating a file may take some time so you may have to display some other activity indicator view while this is being performed.
I would not create the document in the view that displays data because the call to open the document is performed asynchronously and until the completion handler runs you don't know when it's going to complete. Rather you should use the completion handler to launch this view because then you know it's safe to start accessing the core data objects.
More details on how to achieve UIManagedDocument & iCloud Integration here.

Where store User Settings iOS - App Store

in the message of the resolution center (App Store) I received the following message:
"The iOS Data Storage Guidelines indicate that only content that the user creates using your app, e.g., documents, new files, edits, etc., may be stored in the /Documents directory - and backed up by iCloud.".
In my iOS application I store the "Settings" in a SQLite file. That I create just one time and gets modified by the user when he requires to do those changes. The size of this file is about 20kb and I'm storing it in Documents (as Critical Data). It is correct to do this?.
Yes this is correct. If you reply to them and tell them what you've said above, they'll probably allow you to pass the review. Just make it clear that it's user created/modified and needs to be backed up.
Source: This happened to us as well and we challenged and got accepted.
Note: You could save settings in the NSUserDefaults instead if you don't want to have to challenge them.

Resources