Dota2App--第三天

时间:2023-03-10 01:51:49
Dota2App--第三天

一、要实现的功能

1、新特性页面

1.1、是否进入新特性界面的两种情况

1)第一次安装此APP,进入新特性界面

2)不是第一次安装,但是有版本更新,进入新特性界面

1.2、具体的代码实现

      //0.版本号的key
NSString * key = @"CFBundleVersion"; //1.取出沙盒中存储的上次使用软件的版本号
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *lastVersion = [defaults stringForKey:key]; //2. 获得当前软件的版本号
NSString *currentVersion = [NSBundle mainBundle].infoDictionary[key];
//3.判读版本号
if ([currentVersion isEqualToString:lastVersion]) {//如果当前版本和沙盒中的版本一样,则不进入新特性界面
self.window.rootViewController = [[FZHTabViewController alloc]init];
} else { // 如果不一样,则进入新特性界面
self.window.rootViewController = fzhNFVC;
// 更新当前版本到沙盒一遍下次使用
[defaults setObject:currentVersion forKey:key];
[defaults synchronize];
}

2、登录注册页面

2.1、登录界面的手机号判断

1)现在的APP大部分都会使用到手机号注册这一功能,有了这个功能就会有判断手机号有效性这一需求,如果我们不判断手机号的有效性的话既会降低用户体验又会丧失掉有用的用户信息。

2)实现思路:创建一个字符串的分类来判断。

2.1.1、具体代码实现

 + (BOOL)validatePhone:(NSString *)phone
{
/**
* 手机号码
* 移动:134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
* 联通:130,131,132,152,155,156,185,186
* 电信:133,1349,153,180,189
*/
NSString * MOBILE = @"^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$";
/**
10 * 中国移动:China Mobile
11 * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
12 */
NSString * CM = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d)\\d{7}$";
/**
15 * 中国联通:China Unicom
16 * 130,131,132,152,155,156,185,186
17 */
NSString * CU = @"^1(3[0-2]|5[256]|8[56])\\d{8}$";
/**
20 * 中国电信:China Telecom
21 * 133,1349,153,180,189
22 */
NSString * CT = @"^1((33|53|8[09])[0-9]|349)\\d{7}$";
/**
25 * 大陆地区固话及小灵通
26 * 区号:010,020,021,022,023,024,025,027,028,029
27 * 号码:七位或八位
28 */
// NSString * PHS = @"^0(10|2[0-5789]|\\d{3})\\d{7,8}$"; NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT]; if (([regextestmobile evaluateWithObject:phone] == YES)
|| ([regextestcm evaluateWithObject:phone] == YES)
|| ([regextestct evaluateWithObject:phone] == YES)
|| ([regextestcu evaluateWithObject:phone] == YES))
{
return YES;
}
else
{
return NO;
}

2.2、密码字符位数的限制

1)实现思路:如果大于6位,则在textfiled的代理方法里面截取字符串的前六位赋值给该textfiled,如果小于六位提示密码位数不符。

2.2.1、具体代码实现

1)大于6位

 #pragma mark - #pragma mark- UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
range.location = ;
range.length = ; if (textField.text.length > ) {
NSString * subString = [self.password.text substringWithRange:range];
self.password.text = subString;
}
return YES;
}

2)小于6位则弹出提示框

 [MBProgressHUD showError:@"手机号码不正确或密码位数不对"];

2.3、设置键盘类型

1)因为是手机号注册,所以当用户点击textfiled的时候键盘弹出的格式应该是数字键盘,这样就省去了用户改变键盘类型的操作,提高了用户体验。

2.3.1实现代码

 self.account.keyboardType = UIKeyboardTypeNumberPad;

2.4、本地用户信息的存储

1)数据持久化存储一共有四种方法:归档、plist、SQLite、CoreData。

2)我采用的是归档的方法归档,因为可以存储自己定义的数据类型而且比较简单。

2.4.1、实现思路

1)创建自己的用户模型

2)将数据以模型的方式存储在沙盒中

2.4.2、具体的代码实现

1)数据模型的.h文件

 //账号密码
@property (nonatomic,strong)NSString * user_account;
@property (nonatomic,strong)NSString * user_password; +(instancetype)accountWithDict:(NSDictionary *)dict;
-(instancetype)initWithDict:(NSDictionary *)dict;

2)数据模型的.m文件

 +(instancetype)accountWithDict:(NSDictionary *)dict
{
return [[self alloc]initWithDict:dict];
}
-(instancetype)initWithDict:(NSDictionary *)dict
{
if (self = [super init]) {
[self setValuesForKeysWithDictionary:dict];
}
return self;
}
/**
* 从文件中解析对象的时候调
*/
- (id)initWithCoder:(NSCoder *)decoder
{
if (self = [super init]) {
self.user_account = [decoder decodeObjectForKey:@"user_account"];
self.user_password = [decoder decodeObjectForKey:@"user_password"];
}
return self;
} /**
* 将对象写入文件的时候调用
*/
- (void)encodeWithCoder:(NSCoder *)encoder
{
[encoder encodeObject:self.user_account forKey:@"user_account"];
[encoder encodeObject:self.user_password forKey:@"user_password"];
}

3)存储模型的代码

                 //1.存储用户信息
NSString * filepath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"account.data"];
NSMutableDictionary * dict = [NSMutableDictionary dictionary]; dict[@"user_account"] = self.account.text;
dict[@"user_password"] = self.password.text;
FZHAccountModel * account = [[FZHAccountModel alloc]initWithDict:dict];
[NSKeyedArchiver archiveRootObject:account toFile:filepath];

4)调取模型数据的代码

 NSString * filepath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"account.data"];
FZHAccountModel *account = [NSKeyedUnarchiver unarchiveObjectWithFile:filepath];

2.5、界面间反向传值

2.5.1、需求分析

当用户在注册界面或者登陆界面输入完信息之后当跳转到上一页面时,应该讲用户账号传递过来,不用用户再次输入账号,以此来提高用户体验。

2.5.2、实现思路

1)代理-协议,自己写一个协议来传递信息

2)block传值

2.5.3、具体代码实现(block),详细讲解:http://www.cnblogs.com/fengzhihao/p/5200527.html

1)定义block

@property (nonatomic,copy)void (^phoneBlock)(NSString * phoneNumber);

2)实现block

//block反向传值
if (self.phoneBlock) {
self.phoneBlock(self.account.text);
}

3)调用block

  FZHLoginViewController * loginVC = [[FZHLoginViewController alloc]init];
loginVC.phoneBlock = ^ (NSString * phoneNumber){
NSLog(@"%@",phoneNumber);
[self.toLoginBtn setTitle:phoneNumber forState:UIControlStateNormal]; };

二、总结

1、键盘类型具体样式

UIKeyboardTypeDefault:

Dota2App--第三天

UIKeyboardTypeASCIICapable:

Dota2App--第三天

UIKeyboardTypeNumbersAndPunctuation:

Dota2App--第三天

UIKeyboardTypeURL:

Dota2App--第三天

UIKeyboardTypeNumberPad:

Dota2App--第三天

UIKeyboardTypePhonePad:

Dota2App--第三天

UIKeyboardTypeNamePhonePad:

Dota2App--第三天

UIKeyboardTypeEmailAddress:

Dota2App--第三天

UIKeyboardTypeDecimalPad:

Dota2App--第三天

UIKeyboardTypeTwitter:

Dota2App--第三天

UIKeyboardTypeWebSearch:

Dota2App--第三天

UIKeyboardTypeAlphabet:

Dota2App--第三天

三、参考博客

1、键盘类型:http://blog.****.net/crazyzhang1990/article/details/39965931

四、demo下载地址

https://github.com/fengzhihao123/FZHDota2