1、手机号转****
+ (NSString*)numberSuitScanf:(NSString*)number{
if(number.length>5) {
NSString *numberString = [number stringByReplacingCharactersInRange:NSMakeRange(3, 4) withString:@"****"];
return numberString;
}
return number;
}
2、从URL中获取参数字典
+ (NSDictionary*)dictionaryWithUrlString:(NSString*)urlStr{
if(urlStr && urlStr.length&& [urlStr rangeOfString:@"?"].length==1) {
NSArray *array = [urlStr componentsSeparatedByString:@"?"];
if(array && array.count==2) {
NSString*paramsStr = array[1];
if(paramsStr.length) {
NSMutableDictionary *paramsDict = [NSMutableDictionary dictionary];
NSArray*paramArray = [paramsStr componentsSeparatedByString:@"&"];
for(NSString*param in paramArray) {
if(param && param.length) {
NSArray*parArr = [param componentsSeparatedByString:@"="];
if(parArr.count==2) {
[paramsDict setObject:parArr[1]forKey:parArr[0]];
}
}
}
return paramsDict;
}else{
return nil;
}
}else{
return nil;
}
}else{
return nil;
}
}
3、比较本地版本和服务器版本大小
+ (NSInteger)compareVersion:(NSString*)v1 to:(NSString*)v2 {
// 都为空,相等,返回0
if(!v1 && !v2) {
return0;
}
// v1为空,v2不为空,返回-1
if(!v1 && v2) {
return-1;
}
// v2为空,v1不为空,返回1
if(v1 && !v2) {
return1;
}
// 获取版本号字段
NSArray *v1Array = [v1 componentsSeparatedByString:@"."];
NSArray *v2Array = [v2 componentsSeparatedByString:@"."];
// 取字段最少的,进行循环比较
NSIntegersmallCount = (v1Array.count> v2Array.count) ? v2Array.count: v1Array.count;
for(inti =0; i < smallCount; i++) {
NSIntegervalue1 = [[v1Array objectAtIndex:i]integerValue];
NSIntegervalue2 = [[v2Array objectAtIndex:i]integerValue];
if(value1 > value2) {
// v1版本字段大于v2版本字段,返回1
return1;
}elseif(value1 < value2) {
// v2版本字段大于v1版本字段,返回-1
return-1;
}
// 版本相等,继续循环。
}
// 版本可比较字段相等,则字段多的版本高于字段少的版本。
if(v1Array.count> v2Array.count) {
return 1;
}elseif(v1Array.count< v2Array.count) {
return -1;
}else{
return 0;
}
return0;
}
4、判断APP是否打开接收通知功能
+ (BOOL)isUserNotificationEnable {// 判断用户是否允许接收通知
BOOL isEnable =NO;
UIUserNotificationSettings *setting = [[UIApplication sharedApplication] currentUserNotificationSettings];
isEnable = (UIUserNotificationTypeNone== setting.types) ?NO:YES;
return isEnable;
}
5、去设置打开APP的通知接收功能
// 如果用户关闭了接收通知功能,该方法可以跳转到APP设置页面进行修改 iOS版本 >=8.0 处理逻辑
+ (void)goToAppSystemSetting {
UIApplication *application = [UIApplication sharedApplication];
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if([application canOpenURL:url]) {
if([application respondsToSelector:@selector(openURL:options:completionHandler:)]) {
if(@available(iOS10.0, *)) {
[application openURL:urloptions:@{}completionHandler:nil];
}else{
[application openURL:url];
}
}else{
[application openURL:url];
}
}
}
6、去App Store评价APP
+ (void)gotEvaluation{
NSString *str = [NSString stringWithFormat: @"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=%@",@"你APP的APPID"];
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:str]]) {
if(@available(iOS10.0, *)) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str] options:@{UIApplicationOpenURLOptionsSourceApplicationKey : @YES} completionHandler:nil];
}else{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
}
}
}
7、获取APP缓存的大小返回M
+ (NSUInteger)getSize {
NSUIntegersize =0;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString * cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSDirectoryEnumerator*fileEnumerator = [fileManagerenumeratorAtPath:cachePath];
for(NSString*fileNameinfileEnumerator) {
NSString*filePath = [cachePath stringByAppendingPathComponent:fileName];
NSDictionary*attrs = [fileManager attributesOfItemAtPath:filePatherror:nil];
size += [attrs fileSize];
}
CGFloat cacheM = size/1024/1024.0;
return cacheM;
}
8、清除APP的缓存
+ (void)clearFile
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString * cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSDirectoryEnumerator*fileEnumerator = [fileManager enumeratorAtPath:cachePath];
for(NSString*fileName in fileEnumerator) {
NSString*filePath = [cachePath stringByAppendingPathComponent:fileName];
[fileManager removeItemAtPath:filePatherror:nil];
}
}
网友评论