CKDatabase的简单使用
这里需啊哟配合一使用,首先确定用户开启了iCloud,并且iCloud可用
//添加一条记录 func addRecord(){ let id = CKRecord.ID.init(recordName: "1")//设置索引 let volvo = CKRecord.init(recordType: "person", recordID: id) volvo.setObject("liu" as __CKRecordObjCValue, forKey: "name") volvo.setObject(18 as __CKRecordObjCValue, forKey: "age") volvo.setObject(false as __CKRecordObjCValue, forKey: "isMan") database.save(volvo) { (record, error) in if error == nil{ print("成功") }else{ print("失败") } } }
//查询一条记录 func queryRecordWithID(){ let id = CKRecord.ID.init(recordName: "1") database.fetch(withRecordID: id) { (record, error) in if error != nil{ print("查询失败") return } print("--------name:(String(describing: record?.object(forKey: "name"))) age:(String(describing: record?.object(forKey: "age"))) isMan:(String(describing: record?.object(forKey: "isMan")))") } }
//修改一条记录 func updateRecord(){ let id = CKRecord.ID.init(recordName: "1") database.fetch(withRecordID: id) {[weak self] (record, error) in if error != nil{ print("查询失败") return } record!.setObject("xiaoming" as __CKRecordObjCValue, forKey: "name") self!.database.save(record!, completionHandler: { (record, error) in if error != nil{ print("保存失败") return }else{ print("保存成功") } }) } }
//使用谓词查询数据 func queryRecordWithPredicate(){ var predicate = NSPredicate.init(format: "name = %@", "xiaoming1") var query = CKQuery.init(recordType: "person", predicate: predicate) database.perform(query, inZoneWith: nil) { (records, error) in if error != nil{ print("查询失败") return } var tempRecords = records as! [CKRecord] if(tempRecords.count < 1){ print("没有查询到") return; } let record = tempRecords[0] print("--------name:(String(describing: record.object(forKey: "name"))) age:(String(describing: record.object(forKey: "age"))) isMan:(String(describing: record.object(forKey: "isMan")))") } }
//获取用户信息 func queryUserRecordId(){ let container = CKContainer.default() //获取用户信息,必须要使用权限 container.status(forApplicationPermission: .userDiscoverability) { (status, error) in if error != nil{ return; } switch status{ case .initialState: print("需要授权") self.requestPermissionToAccessUserInfomation() case .couldNotComplete: print("获取权限出错") case .denied: print("拒绝访问") case .granted: print("已授予访问权限") self.findUserInfo() } } } func requestPermissionToAccessUserInfomation(){ let container = CKContainer.default() container.requestApplicationPermission(.userDiscoverability) { (status, error) in if error != nil{ return; } //授权 switch status{ case .initialState: print("需要授权") case .couldNotComplete: print("获取权限出错") case .denied: print("拒绝访问") case .granted: print("已授予访问权限") self.findUserInfo() } } } func findUserInfo(){ let container = CKContainer.default() container.fetchUserRecordID { (recordId, error) in if error != nil{ print("查询ID失败") return } print("recordName:(String(describing: recordId!.recordName)) (recordId)" ) container.discoverUserIdentity(withUserRecordID: recordId!, completionHandler: { (userInfo, error) in if error != nil{ print("查询失败") return } print("firstName:(userInfo!.nameComponents?.familyName)") }) } }