iOS中,文件相對路徑、絕對路徑切換

最新更新了APP之後,發現了一個問題,上傳附件的時候一直報錯 No such file or directory。 但是,文件的的確確就在Documents目錄下好好躺着。奇怪奇怪。

仔細對比之後,發現,生成附件的時候,絕對地址是:

/var/mobile/Containers/Data/Application/44C5E71D-B418-4FD3-A82A-45404C19465F/Documents/58961732/58961732_20141210003945072_Q2_1.jpg

其中Application後面有一串符號,表示當前是哪個APP。這個符號每次安裝都不一樣。

所以當我使用1.05版本,生成的附件,把地址放到數據庫中。下次使用1.06的版本,上傳附件,按照這個地址去找附件的時候,的確找不到了。

/var/mobile/Containers/Data/Application/74119C27-0ADF-4DB7-AE7D-705AB964B94C/Documents/58961732/58961732_20141210003945072_Q2_1.jpg

然後,無意中看到了這篇文章(第24), 裏面講到了擴展路徑。原來通過擴展路徑可以自由的切換絕對路徑和相對路徑。

NSString *Path = @"~/NSData.txt";
 
NSString *absolutePath = [Path stringByExpandingTildeInPath];
 
NSLog(@"absolutePath:%@",absolutePath);
 
NSLog(@"Path:%@",[absolutePath stringByAbbreviatingWithTildeInPath]);

運行結果:

2014-12-10 10:08:16.494 Flysurvey[2807:1377884] absolutePath:/var/mobile/Containers/Data/Application/B268916C-BDFF-4550-8A1D-1C88478921E4/NSData.txt
2014-12-10 10:08:16.498 Flysurvey[2807:1377884] Path:~/NSData.txt


另外,經過測試發現,stringByAbbreviatingWithTildeInPath這個方法,會根據當前的APP符號去縮短,所以,如果是上一版本的APP符號,是沒有辦法縮短的。

例如:

 NSString *absolutePath = @"/var/mobile/Containers/Data/Application/AC672ACB-1BAB-4A6B-86DB-09E85FE3074F/NSData.txt";
    NSString *path = [[absolutePath stringByAbbreviatingWithTildeInPath] copy];
    
    NSLog(@"absolutePath:%@",absolutePath);
    
    NSLog(@"Path:%@",path);
    
    NSLog(@"hehe:%@ ",[path stringByExpandingTildeInPath] );
    
    
    NSString *path2 = @"~/aa.txt";
    NSLog(@"aa: %@", [path2 stringByExpandingTildeInPath]);

結果:

2014-12-10 10:20:15.637 Flysurvey[2886:1382167] absolutePath:/var/mobile/Containers/Data/Application/AC672ACB-1BAB-4A6B-86DB-09E85FE3074F/NSData.txt
2014-12-10 10:20:15.640 Flysurvey[2886:1382167] Path:/var/mobile/Containers/Data/Application/AC672ACB-1BAB-4A6B-86DB-09E85FE3074F/NSData.txt
2014-12-10 10:20:15.641 Flysurvey[2886:1382167] hehe:/var/mobile/Containers/Data/Application/AC672ACB-1BAB-4A6B-86DB-09E85FE3074F/NSData.txt 
2014-12-10 10:20:15.641 Flysurvey[2886:1382167] aa: /var/mobile/Containers/Data/Application/D880650F-CF70-4990-AB05-86608CDECE93/aa.txt

可以看到,當前是D88開頭的,上一次AC6開頭的並沒有被轉換成功。依然是絕對路徑。

爲了兼容以前已經按照絕對路徑保存的情況,所以還要自己截取Documents後面的路徑,然後根據當前的APP,去轉換正確的絕對路徑。

於是,項目代碼作下修改:

#import "Attachment.h"
#import "ZYFileUtil.h"
#import "ZYMyLog.h"

@implementation Attachment

@dynamic answer_index;
@dynamic answer_name;
@dynamic attachment_name;
@synthesize attachment_path = _attachment_path;
@dynamic attachment_type;
@dynamic project_id;
@dynamic upload_status;
@dynamic upload_time;

-(NSString *)attachment_path{
    
    NSString *attachment_path_copy = [_attachment_path copy];
    
    //2014-12-10 此版本(1.05.3)獲取路徑時,要處理以前絕對路徑的情況。同時要兼容以後版本相對路徑的情況。
    if ([attachment_path_copy rangeOfString:@"~"].location == NSNotFound) {
        //絕對路徑,去掉Documents前面的部分,增加當前的base路徑
        NSInteger documentIndex = [attachment_path_copy rangeOfString:@"/Documents"].location;
        NSMutableString *tempPath = [[attachment_path_copy substringFromIndex:documentIndex] mutableCopy];
        [tempPath insertString:@"~" atIndex:0];
        attachment_path_copy = [NSString stringWithFormat:@"%@",tempPath];
    }
    
    NSString *finalPath = [attachment_path_copy stringByExpandingTildeInPath];
    [[ZYMyLog sharedMyLog] appendString:[NSString stringWithFormat:@"讀取數據庫附件地址爲: %@, 處理後的地址爲%@", _attachment_path, finalPath]];
    return finalPath;
}

-(void)setAttachment_path:(NSString *)attachment_path{
    //2014-12-10 此版本(1.05.3)之後,存進去的都是相對路徑
    _attachment_path = [attachment_path stringByAbbreviatingWithTildeInPath];
    [[ZYMyLog sharedMyLog] appendString:[NSString stringWithFormat:@"原附件地址爲:%@, 附件地址入庫內容爲: %@", attachment_path, _attachment_path]];
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章