2016-10-22 84 views
0

我使用這段代碼加載本地sqlite數據庫。從URL下載sqlite數據庫

- (id)init 
{ 
    self = [super init]; 
    if (self) { 
     NSString *path = [[NSBundle mainBundle] pathForResource:@"db" ofType:@"sqlite3"]; 
     _db = [[MDDatabase alloc] initWithPath:path]; 
     _HTMLRenderer = [[MDHTMLRenderer alloc] init]; 
    } 
    return self; 
} 

我想使數據庫聯機並讓應用程序下載數據庫。我改變了代碼:

NSData *dbFile = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://www.someurl.com/DatabaseName.sqlite"]]; 

     NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents"]]; 

     NSString *filePath = [resourceDocPath stringByAppendingPathComponent:@"Database.sqlite"]; 

     [dbFile writeToFile:filePath atomically:YES]; 
     _db = [[MDDatabase alloc] initWithPath:filePath]; 
     _HTMLRenderer = [[MDHTMLRenderer alloc] init]; 

Editted: 我改變了我的代碼遵循但它的墜毀。

- (id)init 
{ 
    self = [super init]; 
    if (self) { 
     [self performSelectorOnMainThread:@selector(downalod) withObject:nil waitUntilDone:YES]; 
     NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents"]]; 

     NSString *filePath = [resourceDocPath stringByAppendingPathComponent:@"Database.sqlite"]; 
     _db = [[MDDatabase alloc] initWithPath:filePath]; 
     _HTMLRenderer = [[MDHTMLRenderer alloc] init]; 
    } 
    return self; 
} 

-(void)download 
{ 

    NSData *dbFile = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://www.someurl.com/DatabaseName.sqlite"]]; 

    NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents"]]; 

    NSString *filePath = [resourceDocPath stringByAppendingPathComponent:@"Database.sqlite"]; 

    [dbFile writeToFile:filePath atomically:YES]; 
} 
+0

有用於獲取正確的文檔文件夾中引用的例子不勝枚舉。請在'NSDocumentDirectory'上搜索。 – rmaddy

回答

0

很長一段時間的搜索後,終於我解決了它自己:

- (id)init 
{ 
    self = [super init]; 
    if (self) { 
     NSData *fetchedData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.someurl.com/db.sqlite3"]]; 
     NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; 
     NSString *filePath = [documentsPath stringByAppendingPathComponent:@"db.sqlite3"]; 
     [fetchedData writeToFile:filePath atomically:YES]; 
     _db = [[MDDatabase alloc] initWithPath:filePath]; 
     _HTMLRenderer = [[MDHTMLRenderer alloc] init]; 
    } 
    return self; 
} 
0
  1. 你在你的文件有問題。檢查與正確的網址。它是一個編碼問題。試試這個(它會工作):http://spaceflight.nasa.gov/gallery/images/apollo/apollo17/hires/s72-55482.jpg
  2. 添加應用程序傳輸安全設置(允許任意負載是)在你的plist。
  3. 啓用背景模式。
  4. 聲明此宏的.m

    #define DocumentsDirectory [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] 
    

現在打電話,你會在你的本地文件。

調用,比如:

[self performSelectorOnMainThread:@selector(downalod) withObject:nil waitUntilDone:NO]; 

    _db = [[MDDatabase alloc] initWithPath:filePath]; 
    _HTMLRenderer = [[MDHTMLRenderer alloc] init]; 

-(void)download 
{ 

    NSString *stringURL =[NSString stringWithFormat: @"url"]; 
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",[stringURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]]; 
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url]; 
NSOperationQueue *queue = [[NSOperationQueue alloc] init]; 

NSString *filePath = [DocumentsDirectory stringByAppendingPathComponent:[url lastPathComponent]]; 
NSLog(@"success--222,%@", filePath); 

[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) 
{ 

    NSLog(@"success--222,%@", filePath); 


    if (error) 
    { 
     NSLog(@"success--not---Error,%@", [error localizedDescription]); 
    } 
    else 
    { 
     NSLog(@"success--yes... %@", [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]); 

     [data writeToFile:filePath atomically:YES]; 

    } 
}]; 
} 
+0

我應該在哪裏放置'self performSelectorOnMainThread'代碼?它是否在' - (id)init'下? – user2872856

+0

是的。讓我知道。 –

+0

我不知道誰倒下了它。解釋你投票的原因。@某人。 –