1、從iOS8開始,蘋果爲開發者提供了ClouKit,能夠把咱們的應用程序和用戶數據存儲在iCloud上,用於代替後臺服務器,開發移動代碼便可。數據庫
2、設置xcode
(1)須要一個開發者帳號,而且設置一個bundle id 包含 iCloud功能服務器
(2)設置xcode:app
(3)設置模擬器,setting-iCloud 登陸帳號(能夠是非開發者帳號)ide
3、控制器增刪改查 Swift 代碼:fetch
import UIKit
import CloudKit
//建立一條記錄的ID並指定一個惟一的名字,
let artworkRecordID = CKRecordID.init(recordName: "Cats")
// //建立一個記錄對象
let artworkRecord = CKRecord.init(recordType: "CatsTest", recordID:artworkRecordID)
//一、建立一個容器(使用默認容器)
let myContainer = CKContainer.defaultContainer()
//二、建立數據庫
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!")
}
}
}
}
spa
4、查看實時數據3d
進入蘋果開發者iCloud數據庫網頁地址:https://icloud.developer.apple.com/dashboard/ 登陸開發者帳號
code