Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/107.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 从url下载文件并保存到iPhone上的资源_Ios_Cocoa Touch_Nsurlconnection - Fatal编程技术网

Ios 从url下载文件并保存到iPhone上的资源

Ios 从url下载文件并保存到iPhone上的资源,ios,cocoa-touch,nsurlconnection,Ios,Cocoa Touch,Nsurlconnection,是否可以通过编程方式将文件(即sqlite数据库文件)从Internet下载到iPhone应用程序中,以便以后在应用程序中使用 我正在尝试使用NSURLConnection,但无法保存文件 下面是我正在尝试的示例代码: - (void)viewDidLoad { [super viewDidLoad]; NSString *file = [NSString stringWithFormat:@"http://en.wikipedia.org/wiki/Text_file"];

是否可以通过编程方式将文件(即sqlite数据库文件)从Internet下载到iPhone应用程序中,以便以后在应用程序中使用

我正在尝试使用
NSURLConnection
,但无法保存文件

下面是我正在尝试的示例代码:

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSString *file = [NSString stringWithFormat:@"http://en.wikipedia.org/wiki/Text_file"];
    NSURL *fileURL = [NSURL URLWithString:file];

    NSURLRequest *req = [NSURLRequest requestWithURL:fileURL];
    NSURLConnection *conn = [NSURLConnection connectionWithRequest:req delegate:self];  
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [self.fileData setLength:0];
    self.totalFileSize = [NSNumber numberWithLongLong:[response expectedContentLength]];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
        [self.fileData appendData:data];        
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSArray *dirArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,    NSUserDomainMask, YES);
    NSLog(@"%@", [dirArray objectAtIndex:0]);

    NSString *path = [NSString stringWithFormat:@"%@/blah.text", [dirArray objectAtIndex:0]];

    if ([self.fileData writeToFile:path options:NSAtomicWrite error:nil] == NO) {
        NSLog(@"writeToFile error");
    }
    else {
        NSLog(@"Written!");
    }
}

什么是文件数据?它是如何定义和初始化的

  fileData = [NSMutableData data];
应在以下位置之后:

NSURLConnection *conn = [NSURLConnection connectionWithRequest:req delegate:self];
您必须初始化文件数据并按照内存管理规则保留它


试试我的答案。它起作用了

您需要使用
stringByAppendingPathComponent

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"bookdb.sql"];

在方法中尝试此代码并执行

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];

这对你很有用

NSString *stringURL = @"http://www.somewhere.com/thefile.png";
NSURL  *url = [NSURL URLWithString:stringURL];
NSData *urlData = [NSData dataWithContentsOfURL:url];
if ( urlData )
{
  NSArray       *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  NSString  *documentsDirectory = [paths objectAtIndex:0];  

  NSString  *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"filename.png"];
  [urlData writeToFile:filePath automically:YES];
}

请注意,这是一个
writeToFile
方法,它是同步的,因此它将停止/阻止您的UI。

这里到底是什么失败?如果([self.fileData writeToFile:path options:NSAtomicWrite error:nil]==NO){NSLog(@“writeToFile error”);}我得到了“writeToFile error”,我想从Internet下载数据库(存储在某台服务器上的某个地方)没关系,您仍然需要扩展~Yes,它在头文件中是这样定义的。我在上面的viewDidLoad中没有看到它。它在哪里初始化???fileData=[NSMutableData];按照我在回答(viewDidLoad)中所述初始化fileData,它就会工作。您需要:fileData=[[NSMutableData alloc]initWithLength:0];或fileData=[NSMutableData];在-(void)viewDidLoad中。选择此作为答案。重新询问有关数据库的问题。指向文档目录的URL以这种方式更好->NSURL*documentsDirectory=[[[NSFileManager默认管理器]URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask]lastObject];您可以这样[URL path]来获取路径