几个常用的功能
2016-09-22 本文已影响25人
Luy7788
点击跳转到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]];
}
点击跳转到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]];
}
调用系统拨打电话
/**
* 调用系统拨打电话
*
* @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];
}
清除缓存功能
/**
* 获取缓存文件大小,用来清理缓存
*
* @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);
}
/**
* 清理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];
}
}
}
}
获取APP应用名称
/**
* 获取APP应用名称
*
* @return APP应用名称
*/
+ (NSString *)getAppName
{
NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
NSString *appName = [infoDic objectForKey:@"CFBundleDisplayName"];
return appName;
}
获取当前设备iOS系统版本
+ (NSString *)getIOSVersion
{
return [[UIDevice currentDevice] systemVersion];
}
获取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;
}
获取APP开发版本号Build版本
/**
* 获取APP开发版本号Build
*
* @return 内部标示,用以记录开发版本的,每次更新的时候都需要比上一次高
*/
+ (NSString *)getAppBuildVersion
{
NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
NSString *appBuild = [infoDic objectForKey:@"CFBundleVersion"];
return appBuild;
}
获取APP Bundle Id
/**
* 获取APP Bundle Id
*
* @return com.company.appName
*/
+ (NSString *)getAppBundleId
{
return [[NSBundle mainBundle] bundleIdentifier];
}
判断是否第一次使用该版本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
/**
* 保存信息到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];
}