• iOS云存储:CloudKit 基本使用教程 增删改查(Swift)


    一、从iOS8开始,苹果为开发者提供了ClouKit,可以把我们的应用程序和用户数据存储在iCloud上,用于代替后台服务器,开发移动代码即可。

    二、设置

    (1)需要一个开发者账号,并且设置一个bundle id 包含 iCloud功能

    (2)设置xcode:

    (3)设置模拟器,setting-iCloud 登录账号(可以是非开发者账号)

    三、控制器增删改查 Swift 代码:

    import UIKit
    import CloudKit

    //创建一条记录的ID并指定一个唯一的名字,
    let  artworkRecordID = CKRecordID.init(recordName: "Cats")
    //        //创建一个记录对象
    let artworkRecord = CKRecord.init(recordType: "CatsTest", recordID:artworkRecordID)
    //1、创建一个容器(使用默认容器)
    let myContainer = CKContainer.defaultContainer()
    //2、创建数据库
    let publicDatabase = myContainer.publicCloudDatabase

    class ViewController: UIViewController {

        override func viewDidLoad() {
            super.viewDidLoad()
            
            //添加数据
            artworkRecord["job"] = "iOS开发2"
            artworkRecord["name"] = "聂小波2"
            artworkRecord["address"] = "上海2"
            
            self.fetchRecordData()
            
        }
        
         func creatRecord() {
            //将记录保存在数据库
            publicDatabase.saveRecord(artworkRecord) { (artworkRecord, error) in
                if (error != nil) {
                    print("creatRecord failure!")
                } else {
                    print("creatRecord success!")
                }
            }

        }

        
        func fetchRecordData() {
            //在代码中获取我们保存好的内容
            publicDatabase.fetchRecordWithID(artworkRecordID) { (artworkRecord, error) in
                if (error != nil) {
                    print("selectData failure!")
                } else {
                    print("selectData success!")
                    let job = artworkRecord!["job"];
                    let names = artworkRecord!["name"];
                    let address = artworkRecord!["address"];
                    
                    print("查询信息:" ,"job:",job ,"names:", names ,"address:" , address)
                }
                
            }

        }
        
        func unpdateData() {
            //修改数据
            artworkRecord["name"] = "聂小波3"
            //将记录保存在数据库
            publicDatabase.saveRecord(artworkRecord) { (artworkRecord, error) in
                if (error != nil) {
                    print("unpdateData failure!")
                } else {
                    print("unpdateData success!")
                }
            }
        }

        func deleteRecordData() {
            //将记录保存在数据库
            publicDatabase.deleteRecordWithID(artworkRecordID) { (artworkRecord, error) in
                if (error != nil) {
                    print("deleteRecord failure!")
                } else {
                    print("deleteRecord success!")
                }
            }
        }

    }

    四、查看实时数据

    进入苹果开发者iCloud数据库网页地址:https://icloud.developer.apple.com/dashboard/ 登录开发者账号

  • 相关阅读:
    uboot和内核分区的修改
    2440移植内核到uboot上,打印乱码
    启动新内核出现:No filesystem could mount root, tried: ext3 ext2 cramfs vfa
    启动新内核出现:Kernel panic
    移植最新版本3.4.2内核
    2017团体程序设计天梯赛大区赛 L3-3 球队“食物链”
    leetcode543 Diameter of Binary Tree
    CF599B Spongebob and Joke
    poj1930 Dead Fraction
    poj3040 Allowance
  • 原文地址:https://www.cnblogs.com/niexiaobo/p/5943183.html
Copyright © 2020-2023  润新知