iOS iCloud使用简易说明

1、iCloud类型于一个后台数据库(图片有空再添加)

2、开启Xcode里的iCloud权限,勾选CloudKit,第一个是键值存储,

3、添加Containers, 名字就iCloud+BundleID

3、点击CloudKit Console跳转打开后台数据库

4、选择项目的id下的数据库

5、Record Type -> 添加表 AGOrder 和字段

6、添加完 - Edit Indexes - 添加可查询Queryable, recordName也要添加可查询才能搜索

7、然后Data-Records那边添加数据 后可查询

Xcode项目查询代码

复制代码
#import <CloudKit/CloudKit.h>

NSDictionary *bundelDict  = [[NSBundle mainBundle]infoDictionary];
    NSString *bundleName = bundelDict[@"CFBundleName"];
    NSString *bundelId = bundelDict[@"CFBundleIdentifier"];
    NSString *identy = [NSString stringWithFormat:@"iCloud.%@",bundelId];
    CKContainer *container = [CKContainer containerWithIdentifier:identy];
    CKDatabase *publicDB = container.publicCloudDatabase;
    
    NSPredicate * predicate = [NSPredicate predicateWithFormat:@“name = %@", bundleName];
    CKQuery *query = [[CKQuery alloc] initWithRecordType:@"AGOrder" predicate:predicate];
    [publicDB performQuery:query inZoneWithID:nil completionHandler:^(NSArray<CKRecord *> * _Nullable results, NSError * _Nullable error) {
        
        if (error) {
            NSLog(@"查询失败%@",error);
        }else{
            if (results.count > 0) {
                CKRecord *item = results.firstObject;
                NSString *name = [item objectForKey:@"name"];
                NSLog(@"name = %@",name);
            }else{
                NSLog(@"未查找到数据");
            }
        }
    }];
复制代码

 

posted @   wq95230  阅读(388)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
点击右上角即可分享
微信分享提示