Downloading a file from url and saving to resources on iPhone

后端 未结 4 1275
感动是毒
感动是毒 2020-12-13 16:23

Is it possible to download a file (i.e. an sqlite database file) from the Internet into your iPhone application programmatically for later use within the application?

<
4条回答
  •  旧巷少年郎
    2020-12-13 16:50

    Try this code in a method and execute,

    NSURL *url=[NSURL URLWithString:@"http://en.wikipedia.org/wiki"];
    
    NSData *dbFile = [[NSData alloc] initWithContentsOfURL:url];   
    
    NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle]  resourcePath] stringByDeletingLastPathComponent]stringByAppendingPathComponent:@"Documents"]];
    
    NSString *filePath = [resourceDocPath stringByAppendingPathComponent:@"Text_file.txt"];  
    
    [dbFile writeToFile:filePath atomically:YES];
    

提交回复
热议问题