所以我有一个内置 php(IP 板)的网站,我的 ios 应用程序中有一个下载管理器,可以检测 zip、deb 等文件扩展名。但是我找不到让它识别 url 的方法板用于附加文件。
我现在使用的代码:
//Download manager
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if(navigationType == UIWebViewNavigationTypeLinkClicked) {
NSURL *theRessourcesURL = [request URL];
NSString *fileExtension = [theRessourcesURL pathExtension];
NSLog(@"fileExtension is: %@", fileExtension);
if ([fileExtension hasSuffix:@"zip"] || [fileExtension hasSuffix:@"deb"] || [fileExtension hasSuffix:@"rar"] || [fileExtension hasSuffix:@"mp3"] || [fileExtension hasSuffix:@"pdf"] || [fileExtension hasSuffix:@"exe"] || [fileExtension hasSuffix:@"mp4"] || [fileExtension hasSuffix:@"flv"] || [fileExtension hasSuffix:@"torrent"] || [fileExtension hasSuffix:@"png"] || [fileExtension hasSuffix:@"jpg"] || [fileExtension hasSuffix:@"jpeg"]) {
NSError *error = nil; //error setting
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"downloads"];
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
[[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error]; //Create folder
HCDownloadViewController *dlvc = [[HCDownloadViewController alloc] init];
[dlvc downloadURL:theRessourcesURL userInfo:nil];
[self.navigationController setNavigationBarHidden:NO];
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0/255.0f green:0/255.0f blue:0/255.0f alpha:1];
[self.navigationController pushViewController:dlvc animated:YES];
dlvc.delegate = self;
return NO;
}
else {
}
}
return YES;
}
目前该网站上传的附件提供了这样的链接:
http://mysite.com/index.php?app=core&module=attach§ion=attach&attach_id=38354
实现这一目标的最佳方法是什么?
谢谢您的帮助。
更新 7/17:
从我试图收集的内容来看,我正在尝试使用如下所示的内容。但我似乎无法将其链接到下载管理器以启动下载视图控制器。
//New method I'm trying
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *userId= [prefs stringForKey:@"userIdkey"];
NSString *urlStr = [NSString stringWithFormat:@"http://mysite.com/index.php?app=core&module=attach§ion=attach&attach_id=%@",userId];
//Need to push this view controller like the file extension code above
HCDownloadViewController *dlvc = [[HCDownloadViewController alloc] init];
[dlvc downloadURL:theRessourcesURL userInfo:nil];
更新 7/18:
在这里尝试其他东西,但仍然没有推动视图控制器。
NSURL *url = [request URL];
NSString *id = @"12212323";
NSString *fullURL = [[url pathExtension] initWithFormat:@"www.mysite.com/index.php?app=core&module=attach§ion=attach&attach_id=%@", id];
抛出此日志:
* WebKit 在 webView:decidePolicyForNavigationAction:request:frame:decisionListener:delegate: 中丢弃了一个未捕获的异常: *初始化方法 -initWithFormat:locale:arguments: 不能发送到类 NSPathStore2 的抽象对象:创建一个具体实例!