使用iOS在带有Javascript的网站上下载文件

使用iOS在带有Javascript的网站上下载文件,javascript,ios,xcode,download,Javascript,Ios,Xcode,Download,我正试图以编程方式下载一个文件,当您单击突出显示的下载按钮时,我发现下载, 它运行一个Javascript:document.getElementById'downLoad'。action='/downLoad.php?fileid=11024011';唐尼沙尔'0' 在我的Mac上,它运行良好,当我在Safari上运行它时,它会下载文件。但是当我使用 [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithForm

我正试图以编程方式下载一个文件,当您单击突出显示的下载按钮时,我发现下载, 它运行一个Javascript:document.getElementById'downLoad'。action='/downLoad.php?fileid=11024011';唐尼沙尔'0'

在我的Mac上,它运行良好,当我在Safari上运行它时,它会下载文件。但是当我使用

[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.getElementById('downLoad').action='/download.php?fileid=11024011'"]];
它不会返回任何东西

有人知道我为什么以及如何获得下载URL吗


谢谢。

嗯。。。下载url如下所示:


你也可以通过chrome中的“网络”选项卡或firefox中的firebug进行检查。但是当您尝试直接访问它时,它会重定向您。可能会对请求进行一些服务器端验证,如引用者url或其他内容。

您是否尝试过实现webView委托方法shouldStartLoadWithRequest以通过链接进行解析


- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
if ([[[request URL] absoluteString] hasPrefix:@"http://ishare.iask.sina.com.cn/download.php?fileid="])
//I believe this would download everything that has a link prefix of "http://ishare.iask.sina.com.cn/download.php?fileid="
//So you should create some sort of checker to make sure it only downloads the file they select
        // Determile cache file path
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *filePath = [NSString stringWithFormat:@"%@/%@", [paths objectAtIndex:0],@"index.html"];   
        // Download and write to file
        NSURL *url = [NSURL URLWithString:[[request URL] absoluteString]];
        NSData *urlData = [NSData dataWithContentsOfURL:url];
        [urlData writeToFile:filePath atomically:YES];

        // Load file in UIWebView
        [webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:filePath]]];

        return NO;
    }
    else {
        return YES;
    }

我想你错过了按钮动作的实际触发。你只是在设定它的行动。我猜这就是唐尼沙尔的“0”;是的。谢谢,但我尝试了[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@document.getElementById'downLoad'。action='/downLoad.php?fileid=11024011';downishare'0';];,但它仍然一无所获。