Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/43.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
将文件下载到iPhone中的文件夹_Iphone_Ios_Ipad_Nsfilemanager_Nsdocumentdirectory - Fatal编程技术网

将文件下载到iPhone中的文件夹

将文件下载到iPhone中的文件夹,iphone,ios,ipad,nsfilemanager,nsdocumentdirectory,Iphone,Ios,Ipad,Nsfilemanager,Nsdocumentdirectory,我不熟悉iPhone 我制作了一个应用程序,当用户下载任何东西时,它会被下载到DocumentDirectory 我想在我在DocumentDirectory 如何实现这一点? 我想我会将文件下载到我的文档目录,然后将该文件移动到文档目录内的文件夹中 这是一种良好的做法吗? 这是我的代码片段,但我仍然无法移动我的文件 - (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navi

我不熟悉iPhone

我制作了一个应用程序,当用户下载任何东西时,它会被下载到
DocumentDirectory

我想在我在
DocumentDirectory

如何实现这一点?

我想我会将文件下载到我的
文档目录
,然后将该文件移动到
文档目录
内的文件夹中

这是一种良好的做法吗?

这是我的代码片段,但我仍然无法移动我的文件

- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {

    NSString *file = [NSString stringWithString:[url absoluteString]];
    NSURL *fileURL = [NSURL URLWithString:file];

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


    NSString *DestPath=[[[SourcePath stringByAppendingString:@"/"]stringByAppendingString:BookCateg]stringByAppendingString:@"/"];

                   if ([fm copyItemAtPath:SourcePath toPath:DestPath error:&error]) 
                   {
                      NSLog(@"Success !");
                   }
                  else{
                      NSLog(@"Fail to Move!");
                   }

}
我的日志显示
无法移动


任何帮助都将不胜感激。

您可以使用以下代码:

NSString *stringPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"/Your Folder Name"];

    error = nil;
    if (![[NSFileManager defaultManager] fileExistsAtPath:stringPath])
        [[NSFileManager defaultManager] createDirectoryAtPath:stringPath withIntermediateDirectories:NO attributes:nil error:&error];
NSString *DestPath=[stringPath stringByAppendingString:@"/Your file Name"];

您正在通过追加字符串来使用字符串,而不是通过追加路径组件来使用字符串。这将追加路径,并且您的文件将移动到该路径


享受编码的乐趣

您应该直接将文件下载到文档目录的文件夹中,为此,您需要按照@iphonedeveloper的建议创建目录,并在该目录中写入文件

例如:

  NSString *stringPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"/File"];
  error = nil;
  if (![[NSFileManager defaultManager] fileExistsAtPath:stringPath])
    [[NSFileManager defaultManager] createDirectoryAtPath:stringPath withIntermediateDirectories:NO attributes:nil error:&error];

  NSURL *url = [NSURL URLWithString:@"http://www.ndqatar.mobi/18December/admin/rington/133/QatarDay13.mp3"];
  NSData *data = [NSData dataWithContentsOfURL:url];
  if(data)
  {
    stringPath = [stringPath stringByAppendingPathComponent:[url lastPathComponent]];
    [data writeToFile:stringPath atomically:YES];
  }
如果需要,可以按如下方式移动文件:

 NSString *destPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"/Dest"];
  error = nil;
  if (![[NSFileManager defaultManager] fileExistsAtPath:destPath])
    [[NSFileManager defaultManager] createDirectoryAtPath:destPath withIntermediateDirectories:NO attributes:nil error:&error];
  destPath = [destPath stringByAppendingPathComponent:[stringPath lastPathComponent];];

 [[NSFileManager defaultManager] copyItemAtPath:stringPath toPath:destPath error:&error];
我尝试使用 (在按钮的href中添加链接),它可以在ipad/iphone/ios safari上运行

import Button from '@material-ui/core/Button';
<Button href={add_your_link_here} />
从“@material ui/core/Button”导入按钮;
import Button from '@material-ui/core/Button';
<Button href={add_your_link_here} />