防止NSUserDefaults来自iCloud备份 [英] Preventing NSUserDefaults from iCloud Backup

查看:258
本文介绍了防止NSUserDefaults来自iCloud备份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有三种方法可以将数据持久写入iOS中的设备:

There are three ways to write persistently to the device in iOS that I'm aware of:

  1. NSUserDefaults
  2. 自定义对象-已归档并写入NSDocuments中的PATH
  3. SQLite

Apple提供了一种机制,以防止使用#2进行iCloud备份

Apple provide a mechanism to prevent iCloud backups with #2 i.e.

- (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL

我使用NSUserDefaults存储三个图像,但要遵守iOS数据存储准则- 如何防止使用带有NSUserDefaults的iCloud进行备份?

I use NSUserDefaults to store three images but to adhere to iOS Data Storage Guidelines - How do I prevent Backups with iCloud with NSUserDefaults?

这个问题已经在SO上问过好几次了,但是还没有明确的综合答案.

This question has been asked several times on SO but there is no clear comprehensive answer yet.

是否有这样的功能,或者我必须使用方法2更改存储图像.我希望有一些方便的东西,例如:

Is there a such a function or do I have to change storing images using method #2. I was hoping something convenient like:

- (BOOL)addSkipBackAttributeForStandardUserDefaultsKey:(NSString *)

存在.

推荐答案

没有为此的机制. NSUserDefaults不适用于保存应用程序数据.可以在其中保存应用程序所需的用户首选项和其他设置,但并不是要用作完整的数据持久性系统.

There's no mechanism for this. NSUserDefaults is not intended to be used for saving app data. It's there to hold user preferences and other settings the app needs, but isn't intended as a full data persistence system.

不能准确地说这种"... [Apple的存储数据的机制不能遵守其自身的准则" ,因为这不是该API的目的.特别是在这种情况下,如果不应该备份这些图像,则绝对可以证明用户默认值是错误的存储位置.

It's not accurate to say that this "...[Apple's] mechanism for storing data can't adhere to it's own guidelines" because that's not the purpose of this API. In this case specifically, if those images should not be backed up, then that's absolute evidence that user defaults is the wrong place to store them.

您应该将图像保存到文件中.如果您有UIImage,这很简单. UIImage符合NSCoding,因此很容易与NSData进行转换.并且NSData包括方便的方法来将对象读/写到文件中.如果文件名可能更改,则可以合理地将文件名设置为用户默认值.如果文件名是已知的并且不能更改,则没有理由存储它们.

You should save the images to files. If you have UIImage, this is simple. UIImage conforms to NSCoding, so it's easy to convert to/from an NSData. And NSData includes convenience methods to read/write objects to files. If the file names might change, you could reasonably put the file names in user defaults. If the file names are known in advance and can't change, then there's no reason to store them.

这篇关于防止NSUserDefaults来自iCloud备份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆