IOS开发之——私人通讯录数据存储

一 概述

本文介绍私人通讯录数据存储相关的知识点:

  • 登陆界面中:记住用户名和密码及自动登陆

  • 添加联系人数据保存及更新结果保存

作为一个开发者,有一个学习的氛围跟一个交流圈子特别重要,这是一个我的iOS交流群:812157648,不管你是小白还是大牛欢迎入驻 ,分享BAT,阿里面试题、面试经验,讨论技术, 大家一起交流学习成长!

二 效果图

三 功能实现

3.1 登陆界面

保存登陆数据

//保存登陆数据
[UserDefaults setObject:_accountField.text forKey:AccountKey];
[UserDefaults setObject:_pwdField.text forKey:PwdKey];
[UserDefaults setBool:_rmbPwdS.isOn forKey:RmbPwdKey];
[UserDefaults setBool:_autoLoginS.isOn forKey:AutoLoginKey];
//同步:当前内存中的数据和沙盒同步
[UserDefaults synchronize];

从沙盒读取数据

<pre style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; padding: 8px; font-weight: 400; position: relative; white-space: pre-wrap; overflow-wrap: break-word; overflow-x: auto; font-family: Consolas, Inconsolata, Courier, monospace; font-size: 14px; line-height: 22px; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">//从沙盒里读取数据
 _accountField.text=[UserDefaults objectForKey:AccountKey];
 if (_rmbPwdS.on) {
     _pwdField.text=[UserDefaults objectForKey:PwdKey];
 }
 _rmbPwdS.on=[UserDefaults boolForKey:RmbPwdKey];
 _autoLoginS.on=[UserDefaults boolForKey:AutoLoginKey];
 if (_autoLoginS.on) {[self loginBtn:self.loginBtn];}</pre>

3.2 添加联系人和更新联系人

保存数据

<pre style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; padding: 8px; font-weight: 400; position: relative; white-space: pre-wrap; overflow-wrap: break-word; overflow-x: auto; font-family: Consolas, Inconsolata, Courier, monospace; font-size: 14px; line-height: 22px; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"> #define FilePath [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]stringByAppendingPathComponent:@"contact.data"]
 [NSKeyedArchiver archiveRootObject:self.contacts toFile:FilePath];</pre>

要保存的类实现NSCoding

<pre style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; padding: 8px; font-weight: 400; position: relative; white-space: pre-wrap; overflow-wrap: break-word; overflow-x: auto; font-family: Consolas, Inconsolata, Courier, monospace; font-size: 14px; line-height: 22px; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">- (instancetype)initWithCoder:(NSCoder *)coder
{
    if (self=[super init])
    {
        _name=[coder decodeObjectForKey:NameKey];
        _phone=[coder decodeObjectForKey:PhoneKey];
    }
    return self;
}
- (void)encodeWithCoder:(NSCoder *)coder
{
    [coder encodeObject:_name forKey:NameKey];
    [coder encodeObject:_phone forKey:PhoneKey];
}</pre>

读取联系人数据

<pre style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; padding: 8px; font-weight: 400; position: relative; white-space: pre-wrap; overflow-wrap: break-word; overflow-x: auto; font-family: Consolas, Inconsolata, Courier, monospace; font-size: 14px; line-height: 22px; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">- (NSMutableArray *)contacts
{
    if (_contacts==nil) {
        _contacts=[NSKeyedUnarchiver unarchiveObjectWithFile:FilePath];
        if (_contacts==nil) {
            _contacts=[NSMutableArray array];
        }

    }
    return _contacts;
}</pre>

更新联系人数据

<pre style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; padding: 8px; font-weight: 400; position: relative; white-space: pre-wrap; overflow-wrap: break-word; overflow-x: auto; font-family: Consolas, Inconsolata, Courier, monospace; font-size: 14px; line-height: 22px; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">//协议方法,成功更新了一个联系人
-(void)editViewController:(EditViewController *)edit didUpdateContact:(Contact *)contact
{
    //刷新表格
    [self.tableView reloadData];
    //归档
    [NSKeyedArchiver archiveRootObject:self.contacts toFile:FilePath];  
}</pre>

原文作者:PGzxc
原文地址:https://blog.csdn.net/Calvin_zhou/article/details/108957388

posted @ 2021-01-23 15:20  iOS发呆君  阅读(98)  评论(0编辑  收藏  举报