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(@"未查找到数据");
            }
        }
    }];