iOS 方法 总有你要用到的
有很多方法,可能暂时用不到,但是要用的时候,又要找很久,所以在这里进行一些总结,进行归纳
1.正则表达式的使用
//1.创建正则表达式,[0-9]:表示‘0’到‘9’的字符的集合
NSString *pattern = @"[0-9]";
NSString *testString = @"a34sd231";
NSString *checkString = @"a34sd231";
//1.1将正则表达式设置为OC规则
NSRegularExpression *regular = [[NSRegularExpression alloc] initWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:nil];
//2.利用规则测试字符串获取匹配结果
NSArray *results = [regular matchesInString:testString options:0 range:NSMakeRange(0, checkString.length)];
NSLog(@"%ld",results.count);
另一种使用方法
//谓词判断是否匹配格式
NSString *pattern = @"^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$";
NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",pattern];
if ([pre evaluateWithObject:pattern]) {
//[pre evaluateWithObject:pattern]此处返回一个BOOL值
return YES;
}
2.URL scheme的使用--(链接打开指定APP)
1.打开info.plist文件

找到URL types属性
添加一个字典

选中item0,点添加

选中URL Schemes,将会自动添加一个item0

对item0进行编辑,输入你要使用的名字,进行编译保存

调用:thisisatest:// 相当于一个http头协议,必须要添加://,不然无法打开
3.打电话
1.判断是否插入SIM卡
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import <CoreTelephony/CTCarrier.h>
//使用的时候,需要导入两个头文件
CTTelephonyNetworkInfo *networkInfo = [[CTTelephonyNetworkInfo alloc] init];
CTCarrier *carrier = [networkInfo subscriberCellularProvider];
if (!carrier.isoCountryCode) {
NSLog(@"No sim present Or No cellular coverage or phone is on airplane mode.");
return NO;
}
return YES;
2.拨打电话
NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"tel:%@",phoneNumber];
UIWebView * callWebview = [[UIWebView alloc] init];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:str] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:1];
[callWebview loadRequest:request];
[self.view addSubview:callWebview];
4.获取当前时间
NSDate *now = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:now];
NSInteger year = [dateComponent year];
NSInteger hour = [dateComponent hour];
附录:常用正则表达式
1.校验密码强度
密码的强度必须是包含大小写字母和数字的组合,不能使用特殊字符,长度在8-10之间。
^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$
2.校验中文
字符串仅能是中文。
^[\u4e00-\u9fa5]{0,}$
3.由数字、26个英文字母或下划线组成的字符串
^\w+$
4.校验E-Mail地址
同密码一样,下面是E-mail地址合规性的正则检查语句。
[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?
5.校验身份证号码
下面是身份证号码的正则校验。15或18位。
15位:
^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$
18位:
^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$
6.校验日期
“yyyy-mm-dd“格式的日期校验,已考虑平闰年。
^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$
7.校验金额
金额校验,精确到2位小数。
^[0-9]+(.[0-9]{2})?$
8.校验手机号
下面是国内13、15、18开头的手机号正则表达式。(可根据目前国内收集号扩展前两位开头号码)
^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$
9.判断IE的版本
IE目前还没被完全取代,很多页面还是需要做版本兼容,下面是IE版本检查的表达式。
^.*MSIE[5-8](?:\.[0-9]+)?(?!.*Trident\/[5-9]\.0).*$
10.校验IP-v4地址
IP4正则语句。
\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b
11.校验IP-v6地址
IP6正则语句。
(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))
12.检查URL的前缀
应用开发中很多时候需要区分请求是HTTPS还是HTTP,通过下面的表达式可以取出一个url的前缀然后再逻辑判断。
if(!s.match(/^[a-zA-Z]+:\/\//))
{
s='http://'+s;
}
13.提取URL链接
下面的这个表达式可以筛选出一段文本中的URL。
^(f|ht){1}(tp|tps):\/\/([\w-]+\.)+[\w-]+(\/[\w-./?%&=]*)?
14.文件路径及扩展名校验
验证windows下文件路径和扩展名(下面的例子中为.txt文件)
^([a-zA-Z]\:|\\)\\([^\\]+\\)*[^\/:*?"<>|]+\.txt(l)?$
15.提取ColorHexCodes
有时需要抽取网页中的颜色代码,可以使用下面的表达式。
^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$
16.提取网页图片
假若你想提取网页中所有图片信息,可以利用下面的表达式。
\]*[src]*=*[\"\']{0,1}([^\"\'\>]*)
17.提取页面超链接
提取html中的超链接。
(]*)(href="https?:\/\/)((?!(?:(?:www\.)?'.implode('|(?:www\.)?',$follow_list).'))[^"]+)"((?!.*\brel=)[^>]*)(?:[^>]*)>
18.查找CSS属性
通过下面的表达式,可以搜索到相匹配的CSS属性。
^\s*[a-zA-Z\-]+\s*[:]{1}\s[a-zA-Z0-9\s.#]+[;]{1}
19.匹配HTML标签
通过下面的表达式可以匹配出HTML中的标签属性。
<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[\^'">\s]+))?)+\s*|\s*)\/?>
5.PCH文件的配置
new file(command + n)创建新文件 --->other --->pchfile

配置:target ---> build setting --->APPLE LLVM7.11 ---> Prefix Header --- >添加$(SRCROOT)/PrefixHeader.pch(后面是文件名)

6.Button防重复点击
在AppDelegate中添加 [[UIButton appearance] setExclusiveTouch:YES];
更多防止重复点击:http://www.jianshu.com/p/472a43b29b47
7.获取当前日期
NSDate *currentDate = [NSDate date];//获取当前时间,日期
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"YYYY/MM/dd hh:mm:ss SS"];
NSString *dateString = [dateFormatter stringFromDate:currentDate];
根据自己想要的格式修改DateFormat
(还有很多,后续慢慢补充)