iOS app saved data lost after updating - ios

I have an iPhone app that I developed with Xamarin and am publishing on HockeyApp. Whenever I put a new version of the app on HockeyApp and someone updates their current installation on their phone, they lose the saved data. Is there any way to prevent this?
EDIT
I have an entitlement that let's me share the data with my widget too. Could that be the problem? This is how I'm writing/reading the data:
this.nsUserDefaults = new NSUserDefaults("myGroupId", NSUserDefaultsType.SuiteName);
// Write data:
this.nsUserDefaults.SetString("myValue", "myKey");
this.nsUserDefaults.Synchronize();
// Read data:
string myValue = this.nsUserDefaults.StringForKey("myKey");
EDIT
After changing the above code to the following, it now persists saved data after updating:
// Write data:
NSUserDefaults.StandardUserDefaults.SetString("myValue", "myKey");
NSUserDefaults.StandardUserDefaults.Synchronize();
// Read data:
string myValue = NSUserDefaults.StandardUserDefaults.StringForKey("myKey");
But now I won't be able to share data with my widget...how can I solve this while still being able to share the data with my widget?

If you try to read the data before calling this.nsUserDefaults.Synchronize();, you won't get the data.
So if you do:
this.nsUserDefaults = new NSUserDefaults("myGroupId", NSUserDefaultsType.SuiteName);
// Read data:
string myValue = this.nsUserDefaults.StringForKey("myKey");
You won't get the data. But if you call the Synchronize() method before the read you will get the data:
this.nsUserDefaults = new NSUserDefaults("myGroupId", NSUserDefaultsType.SuiteName);
this.nsUserDefaults.Synchronize();
// Read data:
string myValue = this.nsUserDefaults.StringForKey("myKey");

Related

Best solution for storing JSON data in iPhone

I am working on developing a civic engagement app that provides information on bills making their way through a particular state house. The API I am using updates every hour, if a user accesses the information within that hour, I would like to archive the most recent JSON data on their phone (and update it on the hour as well). What is the best solution for storing JSON data? Core Data, NSCoding, or UserDefaults
Core Data will be the best option to cache your data locally. UserDefaults is least recommendable as if any particular value, corresponding to the key you are trying to save, comes Nil, Your application will crash.
The most simple way is to save Serialization data as plist file:
let plistData = try PropertyListSerialization.data(fromPropertyList: jsonDict, format: .xml, options: PropertyListSerialization.WriteOptions(0))
try plistData.write(to: urlToLocalFile, options: .atomic)
And then read this file:
if let dictionary = NSDictionary(contentsOf: urlToLocalFile) {
//do something with dictionary
}

NSUserDefaultsDidChangeNotification not sent

While working with data sharing between iOS app and Today Extension, I faced the problem that the NSUserDefaultsDidChangeNotification is never sent from either main app or extension when I change UserDefaults. The thing is that I can read and write data to the UserDefaults successfully for the App Group I created. So the data is actually shared by the app and extension. But the notification of the UserDefaults change is never fired (or detected). Can somebody tell me what can be an issue?
The writing of the data in the UserDefaults
NSUserDefaults defaults = new NSUserDefaults("group.com.name1.name2",NSUserDefaultsType.SuiteName);
defaults.SetString("UPDATE " + DateTime.Now.Minute, "data");
defaults.Synchronize();
The notification handler
NSNotificationCenter.DefaultCenter.AddObserver(
NSValueTransformer.UserDefaultsDidChangeNotification, (notification) => {
NSUserDefaults defaults = new NSUserDefaults("group.com.name1.name2",NSUserDefaultsType.SuiteName);
string str = defaults.StringForKey("data");
});
You can use CFNotificationCenter from your container app to post a cross-process notification to your extension app.
Shared Constants:
const string id = "group.sushihangover";
const string key = "LastUpdateTime";
Container app / Setup an observer on your NSUserDefaults object:
var todayWidgetUserDefaults = new NSUserDefaults(id, NSUserDefaultsType.SuiteName);
NSValueTransformer.Notifications.ObserveUserDefaultsDidChange(todayWidgetUserDefaults,(sender, e) =>
{
CFNotificationCenter.Darwin.PostNotification(id, todayWidgetUserDefaults, null, true, true);
});
Today Extension App:
var todayWidgetUserDefaults = new NSUserDefaults(id, NSUserDefaultsType.SuiteName);
void ObserverAction(string notificationId, NSDictionary userInfo)
{
if (notificationId == id)
{
Console.WriteLine(todayWidgetUserDefaults.StringForKey(key));
}
}
var observerToken = CFNotificationCenter.Darwin.AddObserver(id, todayWidgetUserDefaults, ObserverAction, CFNotificationSuspensionBehavior.DeliverImmediately);
Note: App Group entitlements must be setup in both your container app and the Today Extension App.
So, SushiHangover provided perfectly working piece of code.
However, the problem was in the Info.plist file. In order to be able to exchange the notification the Background Mode should be enable for the app with Remote Notification feature. It sounds like a very obvious thing to do, but none of the tutorials for exchanging data between Extension and app with User Defaults that I read mentioned this. Perhaps, it is an obvious thing to do. But I am writing this just in case somebody might have missed this thing too.

iOS 7+: Unique identifier per device<->Store-ID (user) combination

I've spent two days no reading and testing as there is a lot of info about this topic.
Unfortunately I've found no solution yet. I can't implement my own authentication as this doesn't help with the issue I want to solve (see Backgrounding at the end of the question).
Here is my current best approach:
I'm generating a UUID thanks to https://stackoverflow.com/a/8677177/1443733 and storing it in the KeyChain as suggested with SwiftKeychainWrapper (https://github.com/jrendel/SwiftKeychainWrapper)
The short nice and sweet code for that is:
let stored = KeychainWrapper.stringForKey("UUID")
if stored != nil {
Helper.log(TAG, msg: "retrieved from keychain: \(stored!)")
} else {
let theUUID = CFUUIDCreate(nil)
let str = CFUUIDCreateString(nil, theUUID)
Helper.log(TAG, msg: "generated UUID: \(str)")
let ret = KeychainWrapper.setString(str, forKey: "UUID")
Helper.log(TAG, msg: "setkeychain: \(ret)")
}
But the UUID stored in the keychain seems to be per device and not per store ID as well.
When I store the UUID like above and login with a different Store ID on the device KeychainWrapper.stringForKey("UUID")still returns the value of the other user.
Isn't their a way to store a value in a store-id keychain?
It seems that I'm so close so I hope someone could point me in the right direction.
If that approach (with a keychain) can't succeed please let me know as well.
I reckon you can ask a different question as well: Is there some cryptic data I can read/generate or a store which changes with the Store Id currently used on a device?
Ohh... and Swift examples preffered ;)
Backgroundinfo:
I use IAPs in my app and want to save the Store-Id of the user once a refresh of the receipt is valid.
On each start of the app I check if the current Store-Id is the same as the saved one. If not I trigger immediately a refresh of the receipt. If it fails I fall back to the free version of the app.
iOS devices do not support multiple users.
If you want to differentiate between users you will have to do that in your app, perhaps with a user login. Then save a UUID per userID in the Keychain.
As NSUserdefaults temporarily stores the UUID.so,after unistalling the app and again installing,the UID changes. So, UUID must be stored in keychain

How to pass Core Data objectID and use it with Continuity

Just trying to update some Core Data apps with Continuity and have run into a bit of an issue with using the selected objects ID in the userInfo dictionary to display the correct data on the continuing device.
My first thought was to use the ObjectID, however on the receiving device this would never find a corresponding object in the Core Data store.
As it turns out the URL representation of the objectID contains the UUID of the store itself, and because the two stores UUID's are different this is obviously going to fail.
So I guess I could replace the Core Data store's UUID in the URL with the continuing devices UUID and use this, and no doubt it would work.
The Url seems to be of the following format
Does anyone know what the correct way would be to pass a reference to an object between two devices with core data stores that are synchronised via iCloud?
I'll answer this one myself and see if there are any better answers...
I pass the url of the objectID (from objectID.URIRepresentation) using Continuity API and on the receiving device create a new URL using the following:
url is the url passed in the NSUserActivity.userInfo dictionary
let storeUUID = self.identifierForStore()
// Switch the host component to be the local storeUUID
let newURL = NSURL(scheme: url.scheme!, host: storeUUID, path: url.path!)
func identifierForStore()->NSString? {
if let store = self.persistentStoreCoordinator?.persistentStores[0] as? NSPersistentStore {
return store.identifier
} else {
return nil
}
}
This seems to work just fine - hope it helps someone

NSArray to NSData encoding formatting

I am putting together a program that reads the sensors within a cell phone and saves the sensor data to a core-data SQLite model, with each set of readings pertaining to a particular session
The program provides the user with the option to email a .csv file of a particular session.
Having never done this before, I approached the issue by initializing a delegate and context, and searching the core data for entities that pertain to a specified session. The entities that satisfy the session attribute then have their data fields (gps, mag, accel, gyro) read and put into a string. Then the string is appended to an array. All done in swift.
After the entities are searched and the array is created, I attempt to create a csv file for attachment to an email. The file is attached successfully, but my encoding technique is presenting additional data prepended and appended to the file.
I want to save a file on the phone and email a copy to the user.
Here is what I have to change the Array to NSArray before converting again to NSData:
let paths: NSArray = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true);
let path = paths[0].stringByAppendingPathComponent("SessionData.csv")
if !NSFileManager.defaultManager().fileExistsAtPath(path)
{
NSFileManager.defaultManager().createFileAtPath(path, contents: nil, attributes: nil)
}
else
{
NSFileManager.defaultManager().createFileAtPath(path, contents: nil, attributes: nil)
}
var handle: NSFileHandle = NSFileHandle(forWritingAtPath: path)
handle.truncateFileAtOffset(handle.seekToEndOfFile())
var arrayToWriteNS = (arrayToWrite as NSArray)
var dataNS: NSData = NSKeyedArchiver.archivedDataWithRootObject(arrayToWrite as NSArray)
handle.writeData(dataNS)
mc.setSubject(emailTitle)
mc.addAttachmentData(dataNS, mimeType: "text/csv", fileName: "SessionData.csv")
Here is the prepended and appended data:
bplist00‘()T$topX$objectsX$versionY$archiver—TrootĨ
!U$null“
V$classZNS.objectsÄ©ÄÄÄÄÄÄÄÄ Ä
"My Data"
“"#$'X$classesZ$classname¢%&WNSArrayXNSObjectWNSArray܆_NSKeyedArchiver(25:<IOT[fhrtvxz|~ÄÇÑ·Ø}KÁµÉQV_jmu~Üã*ù
In a large data session with 28,000 entities there may be ~750 lines of prepended data.
Any help that you can provide would be appreciated.
I'm new to iOS, Obj-C, and swift, thus I'm positive there is a better way to do this, I just haven't discovered a better method yet.
Thank you.
UPDATE: Ended up just using the NSString data encoding and writing to my file in increments:
handle.truncateFileAtOffset(handle.seekToEndOfFile())
var stringToWriteNS = (stringToWrite as NSString).dataUsingEncoding(NSUTF8StringEncoding)
handle.writeData(stringToWriteNS!)
You do not want NSKeyedArchiver, it is for archiving and restoring classes.
You need to go through the array and create a text representation of each item in a format you what to present to the user.
A quick search of CocoaPods reveals several projects that may fit you needs to generate csv format data.
This one might be what you need.
csv is fairly simple so it would be reasonable to format your data to csv by writing your own code.

Resources