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

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

ViewController.h文件:atom

#import <UIKit/UIKit.h>spa

@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;

@endcode

ViewController.m文件:orm

#import "ViewController.h"ci

@interface ViewController ()string

@endit

@implementation ViewControllerio

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


    // 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

相关文章
相关标签/搜索