【非凡程序員】 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

相關文章
相關標籤/搜索