iOS开发-iCloud的使用 apple云储存的使用

iOS开发-iCloud的使用 apple云储存的使用

前言

  • iOS开发中为了防止用户将app卸载,再安装的时候丢失数据,所以关于apple提供的沙盒本地存储外,还提供了云存储iCloud。

开发准备

  • 开启key-value storage
    在这里插入图片描述

代码

  • 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

猜你喜欢

转载自blog.csdn.net/weixin_41732253/article/details/110287875