iOS 實(shí)現(xiàn)配置/資源文件熱更新

實(shí)現(xiàn)思路
在服務(wù)器上放一個(gè) xxx.plist 里面保存了需要更新的文件名和MD5值,在客戶端讀取文件的方法里優(yōu)先讀取更新目錄下的文件,如果更新目錄沒有就讀取Resources下的原始文件,根據(jù)比較原始文件和服務(wù)器需要的更新文件的MD5值判斷是否需要下載文件

#define CHECKUPDATEURL   @"[http://192.168.1.110:10030/update/xxx.plist](http://192.168.1.110:10030/updatexml/xxx.plist)"
  1. 給 NSBundle 創(chuàng)建一個(gè)分類,擴(kuò)展以下方法:
#pragma mark - NSBundle
@interfaceNSBundle (NSUpdate)
-(NSString *)myResourcePath:(NSString *)files_;
-(NSString*)getDocumentDirectory;
-(NSString*)getFilePath:(NSString*)fileName;

@end

-(NSString*)getFilePath:(NSString*)fileName
 {
#ifdef RESOURCE_IN_SERVER
    return [self getDocumentResource]; 
#endif
    if ([[GXCheckUpdate shareGxCheckUpdate] getUpdateFileExist:fileName]) {
        NSString *resPath = [NSString stringWithFormat:@"%@/%@",[self getDocumentResource],fileName];
         if ([[NSFileManager defaultManager] fileExistsAtPath:resPath]){
             return [self getDocumentResource];
         }
     }
    return [self resourcePath];
}

-(NSString*)getDocumentResource 
{
#ifdef RESOURCE_IN_SERVER
    return [NSString stringWithFormat:@"%@/Resources/",[self getDocumentDirectory]];
#endif
    NSString *versionStr = [[Helper getInstance] getAppVersion];
    return [NSString stringWithFormat:@"%@/%@/",[self getDocumentDirectory],versionStr];
}

-(NSString *)myResourcePath:(NSString *)files_
{
   return [self getPathForResource:files_];
}

2.GXCheckUpdate 用于檢查更新,在進(jìn)入APP界面之前調(diào)用

[[GXCheckUpdate shareGXCheckUpdate] checkUpdateFile];  //檢查執(zhí)行更新

@interface GXCheckUpdate : NSObject 
{
   NSMutableDictionary *filePathDict;
   NSDictionary        *fileAllDict;
}

@property (nonatomic,retain) NSDictionary *fileAllDict;

+ (GXCheckUpdate *)shareGXCheckUpdate;
-(NSString *)getFilePath:(NSString *)_fileName;
-(BOOL)getUpdateFileExist:(NSString*)file_;
-(void)checkUpdateFile;

@end

3.在讀取文件的地方使用擴(kuò)展的獲取文件路徑的方法

NSString *npcPath = [[NSBundle mainBundle] myResourcePath:@"effect/effect"];

原文鏈接:http://keepmovingxin.com/archives/129

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容