iOS development-iCloud use apple cloud storage use

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
    Insert picture description here

Code

  • ViewController.m
#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 {
    
    
	// 卸载app后重装,还可以读取到
    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 {
    
     //清空key的字符
    NSUbiquitousKeyValueStore *key = [NSUbiquitousKeyValueStore defaultStore];
    [key removeObjectForKey:kKey];
}

@end

Guess you like

Origin blog.csdn.net/weixin_41732253/article/details/110287875