iOS一些常用功能 - Go语言中文社区

iOS一些常用功能


#pragma mark - 跳转到AppStore给应用评分

/**

*  跳转到AppStore给应用评分

*

*  @param appId APP ID

*/

+ (void)toAppStoreReview:(NSString *)appId

{

NSString *str = [NSString stringWithFormat:@"itms-apps://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=%@",appId];

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];

}

#pragma mark - 跳转到AppStore下载应用

/**

*  跳转到AppStore下载应用

*

*  @param appId APP ID

*/

+ (void)toAppStoreDownload:(NSString *)appId

{

NSString *str = [NSString stringWithFormat:@"itms-apps://itunes.apple.com/app/id%@",appId];

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];

}

#pragma mark - 调用系统拨打电话

/**

*  调用系统拨打电话

*

*  @param phoneNumber 电话号码

*/

+ (void)callTelephone:(NSString *)phoneNumber

{

NSURL *phoneURL = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@",phoneNumber]];

UIWebView *callWebview = [[UIWebView alloc] initWithFrame:CGRectZero];

[callWebview loadRequest:[NSURLRequest requestWithURL:phoneURL]];

[[[UIApplication sharedApplication]keyWindow] addSubview:callWebview];

}

#pragma mark - 获取NSCachesDirectory路径缓存文件总大小

/**

*  获取缓存文件大小,用来清理缓存

*

*  @return 返回缓存大小 MB(兆)

*/

+ (float)cachesFileSize

{

NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES) objectAtIndex:0];

NSFileManager *manager = [NSFileManager defaultManager];

if (![manager fileExistsAtPath:cachPath]) return 0.0;

NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:cachPath] objectEnumerator];

NSString *fileName;

long long folderSize = 0;

while ((fileName = [childFilesEnumerator nextObject]) != nil)

{

NSString *fileAbsolutePath = [cachPath stringByAppendingPathComponent:fileName];

if ([manager fileExistsAtPath:fileAbsolutePath])

{

folderSize += [[manager attributesOfItemAtPath:fileAbsolutePath error:nil] fileSize];

}

}

return folderSize / (1024.0 * 1024.0);

}

#pragma mark - 清理缓存NSCachesDirectory

/**

*  清理缓存

*

*  @param needAsync 是否需要异步清除

*/

+ (void)clearCaches:(BOOL)needAsync

{

if (needAsync) {

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];

NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachPath];

for (NSString *p in files) {

NSError *error;

NSString *path = [cachPath stringByAppendingPathComponent:p];

if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {

[[NSFileManager defaultManager] removeItemAtPath:path error:&error];

}

}

});

}else{

NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];

NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachPath];

for (NSString *p in files) {

NSError *error;

NSString *path = [cachPath stringByAppendingPathComponent:p];

if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {

[[NSFileManager defaultManager] removeItemAtPath:path error:&error];

}

}

}

}

#pragma mark - 判断是否有新版本

/**

*  判断是否有新版本

*

*  @param serverVersion 传入服务器版本号

*

*  @return 返回是否有新版本,有:YES

*/

+ (BOOL)isHaveNewVersion:(NSString *)serverVersion

{

NSString *currentVersion = [CKToolProgram getAppVersion];

//先全部加上2个点

//1.0 如果当前数值的点小于2个,那么,在最后一位数上加上个小数点和0 就是.0 ===> 1.0.0

//1.1 ===>1.1.0 然后去掉所有小数点  得到 100 110 101 1010 1010 1200 020 010

//1.0.1

//10.1.0

//10.1.0

//12.0.0

//0.2.0

//0.1

//NSString *str = [NSString stringWithFormat:@"http://itunes.apple.com/gb/app/id%@",Appid];

//[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];

//

int serverDotCount = [CKToolKit countChar:serverVersion cchar:'.'];

int currentDotCount = [CKToolKit countChar:currentVersion cchar:'.'];

if(serverDotCount != 2){

serverVersion = [serverVersion stringByAppendingString:@".0"];

}

if(currentDotCount != 2){

currentVersion = [currentVersion stringByAppendingString:@".0"];

}

int serverVersion_int = [[serverVersion stringByReplacingOccurrencesOfString:@"." withString:@""] intValue];

int currentVersion_int = [[currentVersion stringByReplacingOccurrencesOfString:@"." withString:@""] intValue];

if(serverVersion_int > currentVersion_int){

return YES;

}else {

return NO;

}

}

#pragma mark -  获取APP应用名称

/**

*  获取APP应用名称

*

*  @return APP应用名称

*/

+ (NSString *)getAppName

{

NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];

NSString *appName = [infoDic objectForKey:@"CFBundleDisplayName"];

return appName;

}

#pragma mark -  获取APP版本号Version

/**

*  获取APP版本号Version 第一个整数代表重大修改的版本,如实现新的功能或重大变化的修订。第二个整数表示的修订,实现较突出的特点。第三个整数代表维护版本

*

*  @return 发布版本号 如当前上架版本为1.1.0  之后你更新的时候可以改为1.1.1

*/

+ (NSString *)getAppVersion

{

NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];

NSString *appVersion = [infoDic objectForKey:@"CFBundleShortVersionString"];

return appVersion;

}

#pragma mark -  获取iOS系统版本

+ (NSString *)getIOSVersion

{

return [[UIDevice currentDevice] systemVersion];

}

#pragma mark -  获取APP开发版本号Build

/**

*  获取APP开发版本号Build

*

*  @return  内部标示,用以记录开发版本的,每次更新的时候都需要比上一次高

*/

+ (NSString *)getAppBuildVersion

{

NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];

NSString *appBuild = [infoDic objectForKey:@"CFBundleVersion"];

return appBuild;

}

#pragma mark -  获取APP Bundle Id

/**

*  获取APP Bundle Id

*

*  @return com.company.appName

*/

+ (NSString *)getAppBundleId

{

return [[NSBundle mainBundle] bundleIdentifier];

}

#pragma mark -  判断是否第一次使用该版本app

/**

*  判断是否第一次使用该版本app

*

*  @param application AppDelegate的didFinishLaunchingWithOptions:方法的application

*

*  @return 返回是否是第一次启动YES:是 | NO:不是

*/

+ (BOOL)isFirstUsed:(UIApplication *)application

{

// 1.从Info.plist中取出版本号

NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];

NSString *version = [NSString stringWithFormat:@"%@(%@)", infoDic[@"CFBundleShortVersionString"], infoDic[@"CFBundleVersion"]];

// 2.从沙盒中取出上次存储的版本号

NSString *saveVersion = [[NSUserDefaults standardUserDefaults] objectForKey:@"SJSystemVersion"];

if ([version isEqualToString:saveVersion]) { // 不是第一次使用这个版本

// 显示状态栏

application.statusBarHidden = NO;

// 直接进入启动页

return NO;

} else { // 版本号不一样:第一次使用新版本

// 将新版本号写入沙盒

[[NSUserDefaults standardUserDefaults] setObject:version forKey:@"SJSystemVersion"];

[[NSUserDefaults standardUserDefaults] synchronize];

// 显示版本新特性界面

return YES;

}

}

/**

*  保存信息到userDefault种

*

*  @param obj 保存的对象

*  @param key 对应的key

*

*  @return 成功与否

*/

+(BOOL)userDefaultSaveWithObject:(id)obj Key:(NSString *)key{

[[NSUserDefaults standardUserDefaults] setObject:obj forKey:key];

BOOL isSuccess = [[NSUserDefaults standardUserDefaults] synchronize];

return isSuccess;

}

/**

*  从userDefault读取关键字key的obj

*

*  @param key 对应的key

*

*/

+(id)userDefaultReadWithKey:(NSString *)key{

return [[NSUserDefaults standardUserDefaults] objectForKey:key];

}

版权声明:本文来源简书,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://www.jianshu.com/p/4637eed07ac6
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-01-09 00:07:29
  • 阅读 ( 1057 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢