iOS development-iCloud use apple cloud storage use
Preface
In iOS development, in order to prevent users from uninstalling the app and losing data when reinstalling it, in addition to the sandbox local storage provided by Apple, cloud storage iCloud is also provided.
Development preparation
Turn on key-value storage
Code
#import "ViewController.h"
static NSString * const kKey = @"123" ;
@implementation ViewController
- ( void ) viewDidLoad {
[ super viewDidLoad] ;
[ self saveData: @"hello" ] ;
}
- ( void ) touchesBegan: ( NSSet< UITouch * > * ) touches withEvent: ( UIEvent * ) event {
NSLog ( @"get keyString %@" , [ self getKeyString] ) ;
}
- ( NSString * ) getKeyString {
NSUbiquitousKeyValueStore * key = [ NSUbiquitousKeyValueStore defaultStore] ;
NSString * test = [ key objectForKey: kKey] ;
return test;
}
- ( void ) saveData: ( NSString * ) saveString {
NSUbiquitousKeyValueStore * key = [ NSUbiquitousKeyValueStore defaultStore] ;
[ key setObject: saveString forKey: kKey] ;
[ key synchronize] ;
}
- ( void ) removeAllData {
NSUbiquitousKeyValueStore * key = [ NSUbiquitousKeyValueStore defaultStore] ;
[ key removeObjectForKey: kKey] ;
}
@end