测试测试 | ✗ |
语言语言 | Obj-CObjective C |
许可证 | 自定义 |
发布最新发布 | 2015年6月 |
由 Stephen Darlington 维护。
同时存储NSUserDefaults和iCloud数据。
NSUbiquitousKeyValueStore的文档中说明
您不应用将键值存储作为数据源的唯一来源,相反,您应将其用作更新本地存储的配置值集的一种机制。
这正是SDCloudUserDefaults所做的。在您想要在iCloud中反映任何内容时,请将其用作NSUserDefaults的替代品。最好在应用启动时注册通知,否则您可能会丢失更新。
注意:根据广泛的要求,我在CocoaPods配置中启用了OS X的支持。这应该工作,但我还没有能力对其进行测试。如果它能工作,请告诉我。如果有必要,欢迎提交补丁。
有三种方法可以将SDCloudUserDefaults
添加到您的项目中
如果您使用的是iOS 5或更高版本且乐意使用ARC,只需将
将SDCloudUserDefaults .h和SDCloudUserDefaults.m复制到您的项目中。
与NSUserDefaults
或iCloud的等效工具不同,所有的SDUserDefaults
都是类方法,所以无需获取standardUserDefaults
(我是一个懒惰并且打字糟糕的人)。
大多数方法都很直观。例如
+(NSString*)stringForKey:(NSString*)aKey;
+(void)setString:(NSString*)aString forKey:(NSString*)aKey;
+(void)removeObjectForKey:(NSString*)aKey;
还有一些其他数据类型(bool、object、integer)的方法也类似。与NSUserDefaults
一样,还有一个用于确保您的更改已提交的synchronize
方法 —— 至少在本地,不可能保证iCloud已经看到了这些更改。
如果您想要从iCloud(您很可能需要)导入更改,您需要在应用的开始部分调用此方法
+(void)registerForNotifications;
/*
* Copyright 2011-2015 Wandle Software Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://apache.ac.cn/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/