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.
}
}