Designing for Key-Value Data in iCloud

rusli
Site Admin
文章: 212
註冊時間: 週三 7月 07, 2010 9:49 pm

Designing for Key-Value Data in iCloud

文章rusli » 週三 4月 06, 2016 6:15 pm

主要是寫如何把 key-value 的資料丟到 iCloud 存起來, 當沒登入 iCloud 會自動存在local, 等到你設定 iCloud 時會自動同步到 iCloud


https://developer.apple.com/library/mac ... Cloud.html



代碼: 選擇全部

struct iCloud
{
    static var KeyStore: NSUbiquitousKeyValueStore? = NSUbiquitousKeyValueStore()
    static let TextKey = "iCloudText"
}

extension UIViewController
{
    func SaveToiCloud(dataSource: [String] )
    {
        iCloud.KeyStore?.setArray(dataSource, forKey: iCloud.TextKey)
        iCloud.KeyStore?.synchronize()
    }
   
    func GetCloudData() -> [String]
    {
        var result: [String] = [String]()
        if let savedString = iCloud.KeyStore?.arrayForKey(iCloud.TextKey) {
            result = savedString as! [String]
        }
       
        return result
    }
}

class iCloudSyncController: UITableViewController
{
    var dataSource: [String] = [String]()
   
    func reload()
    {
        dataSource = self.GetCloudData()
        self.tableView.reloadData()
    }
   
    override func viewDidLoad() {
        super.viewDidLoad()

        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(iCloudSyncController.keyValueStoreDidChange(_:)), name: NSUbiquitousKeyValueStoreDidChangeExternallyNotification, object: iCloud.KeyStore)

        self.navigationItem.setLeftBarButtonItems([self.editButtonItem()], animated: true)

        self.reload()
    }
   
    func keyValueStoreDidChange(notification: NSNotification) {
       self.reload()
    }

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
   
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataSource.count
    }
   
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let CellIdentifier: String = "cell"
        let cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(CellIdentifier, forIndexPath: indexPath)
        let note: String = dataSource[indexPath.row]
        cell.textLabel!.text = note
        return cell
    }
   
    // Override to support conditional editing of the table view.
    override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        // Return NO if you do not want the specified item to be editable.
        return true
    }
   
    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        switch editingStyle {
        case .Delete:
           
            // remove the deleted item from the model
            self.dataSource.removeAtIndex(indexPath.row)
            // remove the deleted item from the `UITableView`
            self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
           
            self.SaveToiCloud(self.dataSource)
           
        default:
            return
        }
    }
}



代碼: 選擇全部

class MesssageViewController: UIViewController
{
    @IBOutlet weak var testValue: UITextField!

    override func viewDidLoad()
    {
        super.viewDidLoad()
    }
   
    @IBAction func add(sender: AnyObject)
    {
        let keyword = self.testValue.text!
       
        if keyword.characters.count > 0
        {
            var dataSource: [String] = [String]()
           
            dataSource = self.GetCloudData()
            dataSource.insert(self.testValue.text!, atIndex: 0)
           
            self.SaveToiCloud(dataSource)
           
            NSNotificationCenter.defaultCenter().postNotificationName(NSUbiquitousKeyValueStoreDidChangeExternallyNotification, object: iCloud.KeyStore)
        }
       
        self.dismissViewControllerAnimated(true, completion: nil)
    }
   
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

回到「Swift」

誰在線上

正在瀏覽這個版面的使用者:沒有註冊會員 和 1 位訪客