我是iPhone新手,
我制作了应用程序,当用户下载任何内容时,会将其下载到DocumentDirectory
我想在DocumentDirectory
如何实现这一目标?
我以为我会将文件下载到我的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!");
}
}
我的日志显示Fail to Move!
我们将不胜感激。
答案 0 :(得分:8)
您应该直接将文件下载到文档目录的文件夹中,为此您需要创建目录为@iPhone Developer建议并在该目录中写入文件
例如:
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];
答案 1 :(得分:3)
您可以使用以下代码:
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"];
答案 2 :(得分:1)
你通过追加路径组件附加字符串而不是字符串来使用字符串这将附加路径,你的文件将移动到那里
享受编码
答案 3 :(得分:0)
Framework: ReactJS
Library: Material-UI
Version: 4.0
我尝试使用https://material-ui.com/api/button/ (在按钮的href中添加链接),并且可以在ipad / iphone / ios Safari浏览器上使用。
import Button from '@material-ui/core/Button';
<Button href={add_your_link_here} />