Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/111.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中下载文件_Ios_Objective C_Nsfilemanager - Fatal编程技术网

无法在ios中下载文件

无法在ios中下载文件,ios,objective-c,nsfilemanager,Ios,Objective C,Nsfilemanager,这是我下载一本书的代码 - (void)connectionDidFinishLoading:(NSURLConnection *)connection { NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle]resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@

这是我下载一本书的代码

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{
    NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle]resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents"]];

    NSString *filePath= [resourceDocPath stringByAppendingPathComponent:_bookname];//in _bookname i am already  getting the value ,say abcd.pdf

    NSLog(@"The bookfilepath is %@",filePath);

    NSLog(@"the lenght of recieved data is %d", _recievedData.length);//here i get the correct file size also 

    [_recievedData writeToFile:filePath atomically:YES];

    NSFileManager *filemgr;

    filemgr = [NSFileManager defaultManager];

    if ([filemgr fileExistsAtPath: filePath] == YES)
    {
        flag=1;
        NSLog (@"File exists");
    }
    else
    {
        NSLog (@"File not found");  //always this happens in the log
    }
}
文件路径中不存在任何文件。为什么?
有趣的是_receiveddata.length返回了正确的文件大小。

因为您无法写入应用程序包[[NSBundle mainBundle]resourcePath]


改为写入documents文件夹。

这段代码似乎在获取资源路径,剥离最后一个路径组件,然后添加文档。这不是进入Documents文件夹的正确方式。相反,请使用NSSearchPathForDirectoriesInDomains:


如果[filemgr fileExistsAtPath:filePath]==是,则从不写入。真实条件可以产生除“是”以外的许多值。fileExistsAtPath:已是布尔值。如果[filemgr fileExistsAtPath:filePath],在iOS上,还应研究文档以确定应使用哪个目录。有NSDocumentDirectory、NSApplicationSupportDirectory、NSCachesDirectory和NSTemporaryDirectory,它们都有不同的用途。我同意。他应参考并确认文件是否为正确的文件夹。我从他的代码示例中推断出他试图访问文档,所以我只想说明正确的方法。OP正在尝试写入文档文件夹。问题是,问题中的代码在过去是有效的,尽管它从来都不是访问Documents文件夹的正确方式,但在较新的iOS版本中不再有效。@rmaddy No,它不会写入Documents文件夹。Oops-我收回这一点。使用stringByDeletingLastPathComponent实际上会给出错误的路径。无论如何,我的观点是OP的代码表明他们认为他们试图写入Documents文件夹。他们只是做得不对。所以你的回答对他们帮助不大。像罗布的回答一样,最好向他们展示正确的方法,而不是仅仅告诉他们做他们认为自己正在做的事情。
NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString *filePath      = [documentsPath stringByAppendingPathComponent:_bookname];