文件管理类
#import "FileManager.h"
#import "NSString+Hashing.h"
@implementation FileManager
+ (NSString *)getFileWithName:(NSString *)name
{
//缓存文件存放在沙盒路径的Libaray/Cache
NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
//在cache路径下创建一个myCache文件夹
NSString *myCachePath = [cachePath stringByAppendingPathComponent:@"myCache"];
if (![[NSFileManager defaultManager] fileExistsAtPath:myCachePath]) {
//如果不存在,创建
[[NSFileManager defaultManager] createDirectoryAtPath:myCachePath withIntermediateDirectories:YES attributes:nil error:nil];
}
//通过MD5加密一下,转化为唯一的十六进制
NSString *fileName = [name MD5Hash];
NSString *fullName = [myCachePath stringByAppendingPathComponent:fileName];
return fullName;
}
//判断文件是否过期
+ (BOOL)fileIsTimeout:(NSString*)fullName ExpandTime:(double)timeInterval
{
NSDictionary *dict = [[NSFileManager defaultManager] attributesOfItemAtPath:fullName error:nil];
NSDate *lastDate = dict.fileModificationDate;
NSTimeInterval fileInterval = [lastDate timeIntervalSinceNow];
if (fileInterval < 0) {
fileInterval = -fileInterval;
}
if (fileInterval > timeInterval) {
//超时
return YES;
}
return NO;
}
取缓存数据条件
//去缓存文件里取数据,三个条件都满足的时候回去缓存中取数据
//1.缓存文件存在
//2.文件没有过期
//3.不是下拉刷新
BOOL isExist = [[NSFileManager defaultManager] fileExistsAtPath:[FileManager getFileWithName:url]];
BOOL isTimeout = [FileManager fileIsTimeout:[FileManager getFileWithName:url] ExpandTime:10*60];
if (isExist&&!isTimeout&&(self.isRefreshing == NO)) {
请求数据时,满足条件写入数据到缓存文件
if ([type[@"refresh"] isEqual: @YES] || [type[@"currentPage"]integerValue] == 1)
//把数据写到缓存文件
[[NSFileManager defaultManager] createFileAtPath:[FileManager getFileWithName:url] contents:responseObject attributes:nil];
不同应用之间的跳转
//打开当前应用在app store中的下载页面
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:model.itunesUrl]];
友盟分享
导入头文件
import “UMSocial.h”
NSString *str = [NSString stringWithFormat:@"来自爱限免的分享:%@",model.itunesUrl];
[UMSocialSnsService presentSnsIconSheetView:self appKey:@"507fcab25270157b37000010" shareText:str shareImage:[UIImage imageNamed:@"account_candou"] shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToWechatTimeline,UMShareToQzone,UMShareToEmail, nil] delegate:self];
收藏
把数据存储到数据库里面
数据库管理类代码如下:
#import "DBManager.h"
#import <FMDB.h>
@interface DBManager ()
@property (nonatomic) FMDatabase *db;
@end
@implementation DBManager
+ (instancetype) defaultManager
{
static DBManager *manager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
manager = [[DBManager alloc] init];
});
return manager;
}
- (instancetype)init
{
self = [super init];
if (self) {
NSString *path = [NSString stringWithFormat:@"%@/Documents/myDB.sqlite",NSHomeDirectory()];
_db = [[FMDatabase alloc] initWithPath:path];
if (_db) {
[_db open];
[self createTable];
}
}
return self;
}
- (void)createTable
{
NSString *sql = @"create table if not exists appInfo(serial integer Primary Key Autoincrement,appName Varchar(1024),appId Varchar(1024),currentPrice Varchar(1024),lastPrice Varchar(1024),iconUrl Varchar(1024),recordType Varchar(1024),priceType Varchar(1024))";
[_db executeUpdate:sql];
}
- (void)add:(AppModel *)model RecordType:(NSString *)recordType
{
NSString *sql = @"insert into appInfo(appName,appId,currentPrice,lastPrice,iconUrl,recordType,priceType) values (?,?,?,?,?,?,?)";
[_db executeUpdate:sql,model.name,model.applicationId,model.currentPrice,model.lastPrice,model.iconUrl,recordType,model.priceTrend];
}
- (BOOL)appIsExist:(NSString *)appId RecordType:(NSString *)recordType
{
NSString *sql = @"select * from appInfo where appId = ? and recordType = ?";
FMResultSet *rs = [_db executeQuery:sql,appId,recordType];
if ([rs next]) {//查看是否存在 下条记录 如果存在 肯定 数据库中有记录
return YES;
}else{
return NO;
}
}