【非凡程序员】 OC第十七节课 文件操作三 (归档和解档实例--QQ登陆)

谁说我不能喝 提交于 2020-03-01 16:33:59

这是一个QQ登陆的程序,要求注册账号,可以进行登陆,而且账号信息能够记忆保存,下次可以继续进行登陆

ViewController.h文件:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
- (IBAction)Register:(id)sender;
- (IBAction)Cancel:(id)sender;

@property (weak, nonatomic) IBOutlet UILabel *xinxi;
- (IBAction)Go:(id)sender;
@property (weak, nonatomic) IBOutlet UITextField *password;
@property (weak, nonatomic) IBOutlet UITextField *number;

@end

ViewController.m文件:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _xinxi.text=@"你好,如果第一次注册,请输入密码";
    dict2 = [NSMutableDictionary dictionaryWithCapacity:10];
   
    _number.enabled=YES;                    //账号可写性
    _password.secureTextEntry=YES;     //密码的隐身功能


    // Do any additional setup after loading the view, typically from a nib.
//    UIAlertView *al=[[UIAlertView alloc]initWithTitle:@"登录信息" message:@"成功" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
//    [al show];
  }

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

static
NSMutableDictionary * dict2;

//注册按钮

- (IBAction)Register:(id)sender {  
  
   // NSString * number = _number.text;
    NSString * password = _password.text;
  
    if ([password length]<6)
    {
         _xinxi.text=@"你好,密码和账号都至少六位数字";
    }
    else
    {
        //将当前的时间戳给为qq账号
        NSDate *dataaa=[NSDate new];
        NSInteger longlo=(long)[dataaa timeIntervalSince1970];
        NSString *number=[NSString stringWithFormat:@"%ld",longlo];
        _number.text=number;
       
        //解档
        NSData *nutabdata = [[NSData alloc]initWithContentsOfFile:@"/Users/feifanchengxuyuan/Desktop/qqqq.plist"];
        NSKeyedUnarchiver *unkeyed=[[NSKeyedUnarchiver alloc]initForReadingWithData:nutabdata];
        dict2=[unkeyed decodeObjectForKey:@"suisui"];
        NSLog(@"Nasrr---%@", dic2);

        [dict2 addEntriesFromDictionary: @{number:password}];

        //归档
        NSMutableData *data=[[NSMutableData alloc]init];
        NSKeyedArchiver *keyde=[[NSKeyedArchiver alloc]initForWritingWithMutableData:data];
        [keyde encodeObject:dict2 forKey:@"suisui"];
        [keyde finishEncoding];
       
        [data writeToFile:@"/Users/feifanchengxuyuan/Desktop/qqqq.plist" atomically:YES];
       
        _xinxi.text=@"恭喜,注册成功";
      
    }
}

//取消按钮

- (IBAction)Cancel:(id)sender {
   
    _number.text=@"";
    _password.text=@"";
    _xinxi.text=@"";
   
}

//登陆按钮

- (IBAction)Go:(id)sender {

    //解档
    NSData *nutabdata = [[NSData alloc]initWithContentsOfFile:@"/Users/feifanchengxuyuan/Desktop/qqqq.plist"];
    NSKeyedUnarchiver *unkeyed=[[NSKeyedUnarchiver alloc]initForReadingWithData:nutabdata];
    dict2=[unkeyed decodeObjectForKey:@"suisui"];
    NSLog(@"Nasrr---%@" ,dic2);

    if ([_password.text isEqual: dict2[_number.text]]) {
        _xinxi.text=@"登录成功";

    }
    else{
         _xinxi.text=@"登录失败";
 
    }
   
}

@end

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!