从php网站解码附件网址

时间:2013-07-16 23:37:32

标签: php ios webview download

所以我有一个内置在php(IP板)的网站,我在我的ios应用程序中有一个下载管理器,可以检测zip,deb等文件扩展名。但是我找不到让它识别的方法董事会用来附加文件的网址。

我现在使用的代码:

//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&section=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&section=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&section=attach&attach_id=%@", id];

抛出此日志:

  

* WebKit在webView中丢弃了一个未捕获的异常:decisionPolicyForNavigationAction:request:frame:decisionListener:   委托:* 初始化方法   -initWithFormat:locale:arguments:无法发送到类NSPathStore2的抽象对象:创建具体实例!

1 个答案:

答案 0 :(得分:0)

通过调整服务器端的某些内容解决了这个问题,然后使用它来找到扩展名。

NSString *fileExtension = [[externalURL absoluteString] pathExtension];

当代码完全相同时,不知道为什么这被标记为已编辑..

  //My code
    NSString *fileExtension = [[externalURL absoluteString] pathExtension];