zoukankan      html  css  js  c++  java
  • 登录与注册

    简介

        沙盒文件中存储有原始用户信息,老用户可以直接登录。新用户需要进行注册,注册之后信息会永久保存。

    #import "AppDelegate.h"

    #import "LoginViewController.h"

    @interfaceAppDelegate ()

    @end

    @implementation AppDelegate

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

        // Override point for customization after application launch.

        self.window = [[UIWindow alloc] init];

        self.window.frame = [[UIScreen mainScreen] bounds];//配置主界面

        self.window.backgroundColor=[UIColorwhiteColor];

        [self.windowmakeKeyAndVisible];

        LoginViewController *login=[[LoginViewControlleralloc]init];

        self.window.rootViewController=login;

        returnYES;

    }

     

     

    登录界面

     

    #import <UIKit/UIKit.h>

     

    extern NSString *userName,*password;//用户输入的账户字符缓冲区

    extern NSString *rduserName,*rdpassword;//用户输入的密码字符缓冲区

     

    @interface LoginViewController : UIViewController

     

     

    @end

     

     

    #import "LoginViewController.h"

    #import "RegisteredViewController.h"

    #import "Readwrite.h"

    #import "SuccessfulViewController.h"

     

    NSString *userName,*password;//用户输入的账户字符缓冲区

    NSString *rduserName,*rdpassword;//用户输入的密码字符缓冲区

    @interfaceLoginViewController ()<UITextFieldDelegate>

     

    @end

     

    @implementation LoginViewController

    int n=1;

    int m=0;

    - (void)viewDidLoad {

        [superviewDidLoad];

        NSLog(@"原始账号:123 密码:456 从新注册,注册后可以永久保存");

        // Do any additional setup after loading the view.

    //    Readwrite *re=[[Readwrite alloc]init];

    //    [re clear];

        

        UILabel *la=[[UILabelalloc]init];

        la.frame=CGRectMake(0, 0, 375, 667);

        la.backgroundColor=[UIColorcolorWithPatternImage:[UIImageimageNamed:@"3.jpg"]];

        [self.view addSubview:la];

        /**********************************button按键的创建*************************************/

        NSArray *arr=@[@"登录",@"注册",@"找回密码"];

        

        for (int i=0; i<3; i++) {

            UIButton *btn=[UIButtonbuttonWithType:UIButtonTypeRoundedRect];

            btn.frame=CGRectMake(40+i*100, 480, 80, 50);

            [btn setTitle:arr[i] forState:UIControlStateNormal];

            [btn.layer setCornerRadius:10.0];

            btn.backgroundColor=[UIColor blueColor];

            [btn addTarget:selfaction:@selector(event:) forControlEvents:UIControlEventTouchUpInside];

            [self.view addSubview:btn];

        }

        

        /**********************************button按键的创建*************************************/

        

        /**********************************label-textfile*************************************/

        NSArray  *arr1=@[@"用户名",@"请输不多于20位字符",@"密码",@"请输不多于20位数字",@"",@"",@""];

        

        

        for (int i=0; i<2; i++) {

            UILabel *label=[[UILabel alloc]init];

            label.frame=CGRectMake(50, 300+i*60, 60, 50);

            label.text=arr1[m++];

            label.textAlignment=NSTextAlignmentCenter;//文字居中

            [self.view addSubview:label];

            UITextField *file=[[UITextFieldalloc]init];

            file.frame=CGRectMake(130, 300+i*60, 180, 50);

            file.backgroundColor=[UIColor grayColor];

            file.delegate=self;

            file.placeholder=arr1[m++];//设置提示文字

            [file.layer setCornerRadius:10.0];

            // file.clearsOnBeginEditing = YES;  //再次编辑就清空

            if ([file.placeholder isEqualToString:@"请输不多于20位数字"])

            {

                file.secureTextEntry = YES;//每输入一个字符就变成点用语密码输入

            }

            file.clearButtonMode = UITextFieldViewModeWhileEditing; //输入框中是否有个叉号,在什么时候显示,用于一次性删除输入框中的内容

            [file addTarget:selfaction:@ selector(chang:) forControlEvents:UIControlEventEditingChanged];//监听是否有文本输入

            file.font=[UIFont systemFontOfSize:15]; //设置文字大小

            [self.view   addSubview:file];

     

    }

        m=0;

    }

    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

    {

     

        [self.view endEditing:YES];

    }

    -(void) chang:(UITextField *) tf

    {

        if ([tf.placeholder isEqual:@"请输不多于20位字符"]) {

            userName = tf.text;//获取用户名

        }

        else if([tf.placeholder isEqual:@"请输不多于20位数字"])

            password = tf.text;//获取密码

     

    }

    -(void) event:(UIButton *) btn

    {

        if ([btn.titleLabel.text isEqual:@"注册"]) {

            RegisteredViewController *re=[[RegisteredViewControlleralloc]init];

            [UIApplicationsharedApplication].delegate.window.rootViewController=re;

        }

        if ([btn.titleLabel.text  isEqual:@"登录"]) {

            Readwrite *rw=[[Readwrite alloc]init];

            NSString *data=[rw readData];

            NSLog(@"%ld",data.length);

            //NSLog(@"1111111111111111");

            

            for (int i=0; i<data.length; i+=60) {

                //NSLog(@"22222222222222");

                rduserName=[data substringWithRange:NSMakeRange(i, 20)];

                rdpassword=[data substringWithRange:NSMakeRange(i+20, 20)];

                NSRange  range1=[rdpassword rangeOfString:@" "];

                NSRange range2=[rdpassword rangeOfString:@" "];

                rduserName=[rduserName substringToIndex:range1.location];

                rdpassword=[rdpassword substringToIndex:range2.location];

                NSLog(@"%@",userName);

                NSLog(@"%@",password);

                NSLog(@"%@",rduserName);

                NSLog(@"%@",rdpassword);

                if ([userNameisEqual: rduserName]&& [passwordisEqual:rdpassword]) {

                    NSLog(@"333333333333");

                    n=0;

                    SuccessfulViewController *su=[[SuccessfulViewControlleralloc]init];

                    [UIApplicationsharedApplication].delegate.window.rootViewController=su;

                }

              

            }if (n==1) {

                

                    UIAlertView *al = [[UIAlertViewalloc] initWithTitle:@"提示"message:@"请输入正确的账户和密码"delegate:nilcancelButtonTitle:nilotherButtonTitles:@"cancle", nil];

                    [al show];

            }

            

            

            

        }

     

     

    }

    - (void)didReceiveMemoryWarning {

        [superdidReceiveMemoryWarning];

        // Dispose of any resources that can be recreated.

    }

     

    @end

     

     

    注册界面

     

    #import <UIKit/UIKit.h>

    externint i;

    @interface RegisteredViewController : UIViewController

    @property (nonatomic,copy) NSString *userNamer;//用户名

    @property (nonatomic,copy) NSString *passwordr;//用户密码

    @property (nonatomic,copy) NSString *encrypted;//密保

    @end

     

    #import "RegisteredViewController.h"

    #import "Readwrite.h"

    #import "LoginViewController.h"

    #import "SuccessfulViewController.h"

     

    @interfaceRegisteredViewController ()<UITextFieldDelegate>

     

    @end

     

    @implementation RegisteredViewController

    int k,p,i;

    - (void)viewDidLoad {

        [superviewDidLoad];

        UILabel *la=[[UILabel alloc]init];

        la.frame=CGRectMake(0, 0, 375, 667);

        la.backgroundColor=[UIColorcolorWithPatternImage:[UIImageimageNamed: @"2.jpg"]];

        [self.view addSubview:la];

        // Do any additional setup after loading the view.

        /******************************label-textfile************************************/

        NSArray *arr2=@[@"用户名",@"请输入不低于10位字符",@"密码",@"请输入不低于6位字符",@"密保",@"请输入身份证后六位",@"",@""];

        for (int i=0; i<3; i++) {

            UILabel *label=[[UILabel alloc]init];

            label.frame=CGRectMake(50, 200+i*60, 60, 50);

            label.text=arr2[k++];

            label.textAlignment=NSTextAlignmentCenter;

            label.textColor=[UIColor redColor];

            [self.view addSubview:label];

            

            UITextField *file=[[UITextFieldalloc]init];

            file.frame=CGRectMake(130, 200+i*60, 180, 50);

            file.backgroundColor=[UIColor grayColor];

            file.placeholder=arr2[k++];

            file.delegate=self;

            [file.layer setCornerRadius:15.0];

            [file addTarget:selfaction:@selector(event:) forControlEvents:UIControlEventEditingChanged];

            file.font=[UIFont systemFontOfSize:15];

            [self.view addSubview:file];

        }

        k=0;

        /******************************label-textfile************************************/

        

        /**********************************button************************************/

        NSArray *arr3=[NSArray arrayWithObjects:@"OK",@"Cancel", nil];

        for (int i=0; i<2; i++) {

            UIButton *btn=[UIButtonbuttonWithType:UIButtonTypeRoundedRect];

            btn.frame=CGRectMake(80+150*i, 520, 80, 50);

            btn.backgroundColor=[UIColor blueColor];

            [btn.layer setCornerRadius:15.0];

            [btn setTitle:arr3[i] forState:UIControlStateNormal];

            [btn addTarget:selfaction:@selector(incident:) forControlEvents:UIControlEventTouchUpInside];

            [self.view addSubview:btn];

        }

        /**********************************button************************************/

    }

     

    - (void)didReceiveMemoryWarning {

        [superdidReceiveMemoryWarning];

        

    }

    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

    {

        [self.view endEditing:YES];

     

    }

    -(void) event:(UITextField *) tf

    {

        if ([tf.placeholder isEqual:@"请输入不低于10位字符"]) {

            self.userNamer=tf.text;

        }

        if ([tf.placeholder isEqual:@"请输入不低于6位字符"]) {

            self.passwordr=tf.text;

        }

        if ([tf.placeholder isEqual:@"请输入身份证后六位"]) {

            self.encrypted=tf.text;

        }

    }

    -(void) incident:(UIButton *) btn

    {

        if ([btn.titleLabel.text isEqual:@"OK"]) {

           

            Readwrite *rw=[[Readwrite alloc]init];

             NSLog(@"222222");

            NSString *data=[rw readData];

            for (int i=0; i<data.length; i+=60) {

                NSLog(@"111111");

                rduserName = [data substringWithRange:NSMakeRange(i, 20)];//读取账户包

                NSRange range1 = [rduserName rangeOfString:@" "];//获取无效信息的位置

                rduserName = [rduserName substringToIndex:range1.location];//提取有效账户名

            }

            if ([self.userNamerisEqual:rduserName]) {

                UIAlertView *al = [[UIAlertViewalloc] initWithTitle:@"提示"message:@"用户名已存在"delegate:nilcancelButtonTitle:nilotherButtonTitles:@"OK", nil];

                [al show];

            }

            else

            {

                Readwrite *rw=[[Readwrite alloc]init];

                

                NSArray *arr=@[_userNamer,_passwordr,_encrypted];

                for( i=0;i<3;i++)

                    [rw writeData:arr[i]];//处理各个数据

                NSLog(@"111111");

                self.userNamer=@"";//清理变量,用于下一次接受数据

                self.passwordr=@"";

                self.encrypted=@"";

             

                LoginViewController *lv = [[LoginViewControlleralloc] init];

                [UIApplicationsharedApplication].delegate.window.rootViewController = lv;

            }

        }

        if ([btn.titleLabel.text isEqual:@"Cancel"]) {

            

            LoginViewController *su=[[LoginViewControlleralloc]init];

            [UIApplicationsharedApplication].delegate.window.rootViewController=su;

        }

    }

    /*

    #pragma mark - Navigation

     

    // In a storyboard-based application, you will often want to do a little preparation before navigation

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

        // Get the new view controller using [segue destinationViewController].

        // Pass the selected object to the new view controller.

    }

    */

     

     

    @end

    #import <Foundation/Foundation.h>

     

    @interface Readwrite : NSObject

    {

     

        NSArray *documentPart;

       

    }

    -(void)writeData:(NSString*)data;//写数据

    -(NSString*)readData;//读数据

    -(void) clear;

    @end

    文件操作

    #import "Readwrite.h"

    #import "RegisteredViewController.h"

    /*

     Documents:保存应用运行时生成的需要持久化的数据,iTunes会自动备份该目录。苹果建议将程序中建立的或在程序中浏览到的文件数据保存在该目录下,iTunes备份和恢复的时候会包括此目录

     

     tmp:保存应用运行时所需的临时数据,使用完毕后再将相应的文件从该目录删除。应用没有运行时,系统也有可能会清除该目录下的文件,iTunes不会同步该目录。iphone重启时,该目录下的文件会丢失。

     

     Library:存储程序的默认设置和其他状态信息,iTunes会自动备份该目录。

     

     Libaray/Caches:存放缓存文件,iTunes不会备份此目录,此目录下文件不会在应用退出删除。一般存放体积比较大,不是特别重要的资源。

     

     Libaray/Preferences:保存应用的所有偏好设置,iosSettings(设置)应用会在该目录中查找应用的设置信息,iTunes会自动备份该目录。

     

     */

    //NSString *directoryPath = @"/Users/shangchengjiaoyu/Library/Developer/CoreSimulator/Devices/Documents";//文件夹路径

    //NSString *filePathsPath = @"/Users/shangchengjiaoyu/Library/Developer/CoreSimulator/Devices/Documents/1.txt";//文件路径

     

    @implementation Readwrite

    -(void)writeData:(NSString*) data

    {

        

        NSFileManager *fm=[NSFileManagerdefaultManager];

        documentPart = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

        NSString *path = [NSString stringWithFormat:@"%@%@",documentPart[0],@"/my"];

         NSString *paths =[NSString stringWithFormat:@"%@%@",documentPart[0],@"/my/4.txt"];

    //    NSLog(@"%@",documentPath[0]);

        if(i==0)

        {

        if (![fm fileExistsAtPath:path]) {

            NSLog(@"33333");

            bool f=[fm createDirectoryAtPath:path withIntermediateDirectories:YESattributes:nilerror:nil];

            //NSLog(@"%@",path);

            NSLog(@"%@",f==1?@"成功":@"失败");

             NSLog(@"%@",[fm createFileAtPath:paths contents:nilattributes:nil]==1?@"创建成功,数据已写入":@"创建失败,数据写入失败");

            NSLog(@"%@",paths);

        }

        else

        {   NSLog(@"%@",paths);

            NSLog(@"文件存在,数据已写入");

        }

        }

        long len = 20-data.length;//字符串长度不齐,进行增添空格

        for(int i=0;i<len;i++)

        {

            data = [data stringByAppendingString:@" "];

        }

        NSString *str=data;//定义一个将要写进去的数组

        NSString *read=[NSStringstringWithContentsOfFile:paths encoding:NSUTF8StringEncodingerror:nil];

        str=[read stringByAppendingString:str];//字符串的追加

        [str writeToFile:paths atomically:YESencoding:NSUTF8StringEncodingerror:nil];//将字符串写入文件

    }

    -(NSString *) readData

    {

     

       documentPart = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

       NSString *paths =[NSString stringWithFormat:@"%@%@",documentPart[0],@"/my/4.txt"];

       NSString *read1=[NSStringstringWithContentsOfFile:paths encoding:NSUTF8StringEncodingerror:nil];

        return read1;

    }

    -(void) clear

    {

       // NSFileManager *fm=[NSFileManager defaultManager];

      documentPart = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

        NSString *paths =[NSString stringWithFormat:@"%@%@",documentPart[0],@"/my/4.txt"];

     [@""writeToFile:paths atomically:YESencoding:NSUTF8StringEncodingerror:nil];

     

    }

     

    @end

    #import <UIKit/UIKit.h>

     

    @interface SuccessfulViewController : UIViewController

     

    @end

     

    #import "SuccessfulViewController.h"

    #import "LoginViewController.h"

     

    @interface SuccessfulViewController ()

     

    @end

     

    @implementationSuccessfulViewController

     

    - (void)viewDidLoad {

        [superviewDidLoad];

     

       

        UISegmentedControl *seg = [[UISegmentedControl alloc]initWithItems:@[@"风景",@"建筑",@"人物"]];

        seg.frame = CGRectMake(100, 10, 250, 50);

        seg.backgroundColor = [UIColor clearColor];

        [seg addTarget:self action:@selector(loop:) forControlEvents:UIControlEventValueChanged];

        [self.view addSubview:seg];

     

        // Do any additional setup after loading the view.

    }

     

    -(void) loop:(UISegmentedControl *) sg

    {

        NSInteger index = sg.selectedSegmentIndex;

     

        switch (index) {

            case 0:

                 self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"5.jpg"]];

                break;

            case 1:

                 self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"6.png"]];

                break;

            case 2:

                 self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"7.jpg"]];

                break;

            default:

                break;

        }

        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

        button.frame = CGRectMake(10, 10, 60, 40);

        button.backgroundColor = [UIColor whiteColor];

        [button.layer setCornerRadius:10.0];

        [button setTitle:@"退出" forState:UIControlStateNormal];

        [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

        [button addTarget:self action:@selector(event) forControlEvents:UIControlEventTouchUpInside];

        [self.view addSubview:button];

     

    }

    -(void) event

    {

        LoginViewController *vc = [[LoginViewController alloc]init];

        [UIApplication sharedApplication].delegate.window.rootViewController=vc;

     

    }

      

    - (void)didReceiveMemoryWarning

    {

        [super didReceiveMemoryWarning];

        // Dispose of any resources that can be recreated.

    }

     

    /*

    #pragma mark - Navigation

     

    // In a storyboard-based application, you will often want to do a little preparation before navigation

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

        // Get the new view controller using [segue destinationViewController].

        // Pass the selected object to the new view controller.

    }

    */

     

    @end

  • 相关阅读:
    Git上手:四种常见的Git协同工作方式
    Git上手:Git扫盲区
    理解web缓存
    浅谈对技术债的理解
    保护个人隐私,从我做起
    cookie注意事项
    了解JavaScript核心精髓(二)
    简单实现nodejs爬虫工具
    浅谈我所见的CSS组织风格
    利用正则表达式清除多余的空行
  • 原文地址:https://www.cnblogs.com/lcl15/p/4964926.html
Copyright © 2011-2022 走看看