月圆之夜,紫禁之巅,一剑西来,天外飞仙。

0%

最近在项目中用到了随机验证码,在这里简单做个记录。
效果图:

20181004164927131

主要代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
.h文件
#import <UIKit/UIKit.h>

typedef NS_ENUM(NSInteger,XBGetVerificationCodeType)

{

XBGetVerificationCodeOne = 0,//只数字

XBGetVerificationCodeTwo,//只字母

XBGetVerificationCodeThree,//数字字母混合
};

typedef void(^setShowCodeNumber)(NSString * codeNumber);

@interface XBGetVerificationCodeView : UIView

@property(nonatomic,assign)NSInteger verificationNumber;//验证码位数

@property(nonatomic,assign)XBGetVerificationCodeType tmpCodeType;

@property(nonatomic,strong)setShowCodeNumber setShowCodeNumber;//获取到的验证码

-(void)setCodeTitleColor:(UIColor *)color;// 设置验证码字体颜色

-(void)setCodeTitleFont:(UIFont *)font;//设置验证码字体大小

@end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
.m 文件
#import "XBGetVerificationCodeView.h"

@interface XBGetVerificationCodeView()

@property(nonatomic,strong)UIButton * btn;

@end

@implementation XBGetVerificationCodeView

-(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];

if(self)
{
self.verificationNumber = 6;

self.btn = [UIButton buttonWithType:UIButtonTypeCustom];

[self.btn setFrame:CGRectMake(0, 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame))];

[self.btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

[self.btn setTitle:@"获取验证码" forState:UIControlStateNormal];

[self.btn.titleLabel setFont:[UIFont systemFontOfSize:14.0]];

[self.btn addTarget:self action:@selector(setBtnAction:) forControlEvents:UIControlEventTouchUpInside];

[self addSubview:self.btn];
}

return self;
}

-(void)setBtnAction:(UIButton *)sender
{
if(sender && [sender isKindOfClass:[UIButton class]])
{
[self.btn setTitle:[NSString stringWithFormat:@"%@",[self getRandomStringWithNum:self.verificationNumber]] forState:UIControlStateNormal];

if(self.setShowCodeNumber)
{
self.setShowCodeNumber([NSString stringWithFormat:@"%@",[self getRandomStringWithNum:self.verificationNumber]]);
}

}
}

-(void)setCodeTitleColor:(UIColor *)color
{
[self.btn setTitleColor:color forState:UIControlStateNormal];
}
-(void)setCodeTitleFont:(UIFont *)font
{
[self.btn.titleLabel setFont:font];
}
-(NSString *)getRandomStringWithNum:(NSInteger)num
{

NSString *string = [[NSString alloc]init];

if(self.tmpCodeType == XBGetVerificationCodeOne)
{
for (int i = 0; i < num; i++) {

int figure = arc4random() % 10;

NSString *tempString = [NSString stringWithFormat:@"%d", figure];

string = [string stringByAppendingString:tempString];

}
return string;
}
else if(self.tmpCodeType == XBGetVerificationCodeTwo)
{
for (int i = 0; i < num; i++) {

int figure = (arc4random() % 26) + 97;

char character = figure;

NSString *tempString = [NSString stringWithFormat:@"%c", character];

string = [string stringByAppendingString:tempString];

}
return string;
}
else
{
for (int i = 0; i < num; i++) {
int number = arc4random() % 36;
if (number < 10) {
int figure = arc4random() % 10;
NSString *tempString = [NSString stringWithFormat:@"%d", figure];
string = [string stringByAppendingString:tempString];
}else {
int figure = (arc4random() % 26) + 97;
char character = figure;
NSString *tempString = [NSString stringWithFormat:@"%c", character];
string = [string stringByAppendingString:tempString];
}
}
return string;
}


}
@end

使用

1
2
3
4
5
6
7
8
9
10
11
12
XBGetVerificationCodeView * xbView = [[XBGetVerificationCodeView alloc]initWithFrame:CGRectMake(100, 100, 100, 40)];

[xbView setBackgroundColor:[UIColor orangeColor]];

xbView.tmpCodeType = XBGetVerificationCodeThree;

[xbView setSetShowCodeNumber:^(NSString *codeNumber) {

NSLog(@"验证码为:%@",codeNumber);
}];

[self.view addSubview:xbView];

demo地址:https://gitee.com/xiaobaidxg/VerificationCode.git

在微博登录中,出现 redirect_uri_mismatch 错误如下图:

201811302152077

原因是在回调地址不一致或没有设置造成的。
解决办法:登录微博开放平台,在我的应用—> 应用信息—> 高级信息中设置OAuth2.0 回调地址。
20181130220300566

在应用中需要微博登录的地方调用。

WBAuthorizeRequest *request = [WBAuthorizeRequest request];
    request.redirectURI = 授权回调地址;
    request.scope = @"all";
    request.userInfo = @{@"SSO_From": @"",
                         @"Other_Info_1": [NSNumber numberWithInt:123],
                         @"Other_Info_2": @[@"obj1", @"obj2"],
                         @"Other_Info_3": @{@"key1": @"obj1", @"key2": @"obj2"}};
    [WeiboSDK sendRequest:request];

说起摇一摇屏幕截图这个功能的实现,我们可以把它分为两部分来看:

一、摇一摇功能的实现;

二、屏幕截图。


首先来实现摇一摇功能:这个网上方法很多,就不多说了直接上代码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 [UIApplication sharedApplication].applicationSupportsShakeToEdit = YES;

//在这里设置当前vc 为第一响应者。
[self becomeFirstResponder];


// 开始摇一摇
-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
NSLog(@"开始摇一摇");
}

//取消摇一摇
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
NSLog(@"取消摇一摇");
}

//结束摇一摇
-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
if(event.subtype == UIEventSubtypeMotionShake)
{
NSLog(@"摇动结束");
//这里实现屏幕截图
}
}

点击并拖拽以移动

其次实现屏幕截图功能:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
{
UIGraphicsBeginImageContextWithOptions(self.view.window.bounds.size, NO, [UIScreen mainScreen].scale);
}
else
{
UIGraphicsBeginImageContext(self.view.window.bounds.size);
}

[self.view.window.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

点击并拖拽以移动


最后效果如下图:

摇一摇截图


demo地址:https://github.com/xiaobai0134/shakeScreen.git

在平常的开发中,经常会用到UIAlertController,用的时候系统自带的样式不符合我们的需求,需要我们自己定义UIAlertController 标题和内容的文本样式。在这里我们通过kvc的思想来实现。在这里补充一点:在使用中发现这个方法只适用于iOS 12的系统,其他系统具体看UIAlertController的层级结构。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"这是标题" message:@"这是内容。\n1、这是内容一。\n2、这是内容二。\n3、这是内容三。\n4、这是内容四。" preferredStyle:(UIAlertControllerStyleAlert)];

UIView *subView1 = alert.view.subviews[0].subviews[0].subviews[0].subviews[0].subviews[0];


//打印 找到所对应的内容
NSLog(@"==========>%@",subView1.subviews);

UILabel * tmpMessageLabel =subView1.subviews[2];

//在这里设置内容为向左对齐
[tmpMessageLabel setTextAlignment:NSTextAlignmentLeft];

UIAlertAction * okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];

UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];

[alert addAction:cancelAction];

[alert addAction:okAction];

[self presentViewController:alert animated:YES completion:nil];

点击并拖拽以移动

打印的信息为:

1
2
3
4
5
6
7
8
在 subView5.subviews 中的内容
(
"<UIView: 0x10fc01380; frame = (0 0; 0 0); clipsToBounds = YES; layer = <CALayer: 0x282037d60>>",
"<UILabel: 0x10fc01560; frame = (0 0; 0 0); text = '\U63d0\U73b0\U89c4\U5219'; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x28033dc70>>",
"<UILabel: 0x10fc01850; frame = (0 0; 0 0); text = '1\U3001\U63d0\U73b0\U91d1\U989d\U9700\U5728255.00\U5143\U4ee5\U4e0a\Uff0c\U4e0d\U8db3255.0...'; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x28033ec10>>",
"<UIView: 0x10fc01b40; frame = (0 0; 0 0); clipsToBounds = YES; layer = <CALayer: 0x28202b0e0>>",
"<UIView: 0x10fc01d20; frame = (0 0; 0 0); clipsToBounds = YES; layer = <CALayer: 0x28202b120>>"
)

点击并拖拽以移动

在这里就找到了标题和内容所对应的label,为所需的内容进行设置样式。


仅个人见解,如有错误请见谅!

在项目开发中可能会遇到这样的问题,判断当前的时间是否在指定的时间段内。下面直接上代码:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
- (BOOL)isBetweenFromHour:(NSInteger)fromHour toHour:(NSInteger)toHour
{

NSDate *dateFrom = [self getCustomDateWithHour:fromHour];

NSDate *dateTo = [self getCustomDateWithHour:toHour];

NSDate *currentDate = [NSDate date];

if ([currentDate compare:dateFrom]==NSOrderedDescending && [currentDate compare:dateTo]==NSOrderedAscending)
{

SLog(@"该时间在 %ld:00-%ld:00 之间!", (long)fromHour, (long)toHour);

return YES;

}
else
{
SLog(@"不在该时间段 %ld:00-%ld:00 之间!", (long)fromHour, (long)toHour);

return NO;
}

}


- (NSDate *)getCustomDateWithHour:(NSInteger)hour
{

//获取当前时间

NSDate *currentDate = [NSDate date];

NSCalendar *currentCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

NSDateComponents *currentComps = [[NSDateComponents alloc] init];



NSInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;



currentComps = [currentCalendar components:unitFlags fromDate:currentDate];



//设置当天的某个点

NSDateComponents *resultComps = [[NSDateComponents alloc] init];

[resultComps setYear:[currentComps year]];

[resultComps setMonth:[currentComps month]];

[resultComps setDay:[currentComps day]];

[resultComps setHour:hour];



NSCalendar *resultCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

return [resultCalendar dateFromComponents:resultComps];

}

点击并拖拽以移动

用法:

1
2
//在 9 - 20 点之间
[self isBetweenFromHour:9 toHour:20];

点击并拖拽以移动


判断手机型号代码如下:

1
#import <sys/utsname.h>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#pragma mark -- 判断手机型号
+(NSString*)judgeIphoneType {

struct utsname systemInfo;

uname(&systemInfo);

NSString * phoneType = [NSString stringWithCString: systemInfo.machine encoding:NSASCIIStringEncoding];

// simulator 模拟器

if ([phoneType isEqualToString:@"i386"]) return @"Simulator";

if ([phoneType isEqualToString:@"x86_64"]) return @"Simulator";

// 常用机型 不需要的可自行删除

if([phoneType isEqualToString:@"iPhone1,1"]) return @"iPhone 2G";

if([phoneType isEqualToString:@"iPhone1,2"]) return @"iPhone 3G";

if([phoneType isEqualToString:@"iPhone2,1"]) return @"iPhone 3GS";

if([phoneType isEqualToString:@"iPhone3,1"]) return @"iPhone 4";

if([phoneType isEqualToString:@"iPhone3,2"]) return @"iPhone 4";

if([phoneType isEqualToString:@"iPhone3,3"]) return @"iPhone 4";

if([phoneType isEqualToString:@"iPhone4,1"]) return @"iPhone 4S";

if([phoneType isEqualToString:@"iPhone5,1"]) return @"iPhone 5";

if([phoneType isEqualToString:@"iPhone5,2"]) return @"iPhone 5";

if([phoneType isEqualToString:@"iPhone5,3"]) return @"iPhone 5c";

if([phoneType isEqualToString:@"iPhone5,4"]) return @"iPhone 5c";

if([phoneType isEqualToString:@"iPhone6,1"]) return @"iPhone 5s";

if([phoneType isEqualToString:@"iPhone6,2"]) return @"iPhone 5s";

if([phoneType isEqualToString:@"iPhone7,1"]) return @"iPhone 6 Plus";

if([phoneType isEqualToString:@"iPhone7,2"]) return @"iPhone 6";

if([phoneType isEqualToString:@"iPhone8,1"]) return @"iPhone 6s";

if([phoneType isEqualToString:@"iPhone8,2"]) return @"iPhone 6s Plus";

if([phoneType isEqualToString:@"iPhone8,4"]) return @"iPhone SE";

if([phoneType isEqualToString:@"iPhone9,1"]) return @"iPhone 7";

if([phoneType isEqualToString:@"iPhone9,2"]) return @"iPhone 7 Plus";

if([phoneType isEqualToString:@"iPhone10,1"]) return @"iPhone 8";

if([phoneType isEqualToString:@"iPhone10,4"]) return @"iPhone 8";

if([phoneType isEqualToString:@"iPhone10,2"]) return @"iPhone 8 Plus";

if([phoneType isEqualToString:@"iPhone10,5"]) return @"iPhone 8 Plus";

if([phoneType isEqualToString:@"iPhone10,3"]) return @"iPhone X";

if([phoneType isEqualToString:@"iPhone10,6"]) return @"iPhone X";

if([phoneType isEqualToString:@"iPhone11,8"]) return @"iPhone XR";

if([phoneType isEqualToString:@"iPhone11,2"]) return @"iPhone XS";

if([phoneType isEqualToString:@"iPhone11,4"]) return @"iPhone XS Max";

if([phoneType isEqualToString:@"iPhone11,6"]) return @"iPhone XS Max";

return phoneType;

}

mac 中提示应用“已损坏,打不开” 是因为在系统“通用”中没有选择“任何来源”

1
2
3
4
5
6
7
在mac 中显示 “任何来源”的方法:

在控制台中执行:
sudo spctl --master-disable 显示任何来源

在控制台中执行:
sudo spctl --master-enable 隐藏任何来源

我是在mac本上配置ionic环境的,具体如下:

  1. 下载nodejs:https://nodejs.org/download/ 并双击安装。

  2. Cordova and Ionic command-line tools 安装,在终端使用命令sudo npm install -g cordova ionic安装
    注意操作系统用户要有密码哦,不然安装过程中提示你输入密码,直接回车是通过不了的。

  3. 创建ionic工程
    在合适的位置执行命ionic start myFirstApp创建第一个ionic工程。

  4. 如果工程创建成功,我们cd myFirstApp目录下,执行命令ionic serve,会自动打开浏览器显示页面。

  1. ionic iOS环境搭建:

    因为是在mac系统下进行配置,所以容易多了

    1、安装ios模拟器

    sudo npm install -g ios-sim
    2、添加ios平台、编译、模拟器运行

1
2
3
ionic platform add ios
ionic build ios
ionic emulate ios

需要的拿走(> _ <)

最近在项目中遇到了一个问题:点击按钮,复制一段指定的文本,发现自己不知道百度一番找见了在这里记录下来。

1
2
3
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];

pasteboard.string = @"要复制的内容";