代码之家  ›  专栏  ›  技术社区  ›  Grant Paul

正在检测uiwebview中的下载

  •  3
  • Grant Paul  · 技术社区  · 15 年前

    我有一个程序化的箱子 UIWebView ,它用于浏览存储在我的服务器上的iPhone风格的站点。在这个网站上,有几个链接指向用户可以下载到我的应用程序中的文件。现在,我正在尝试用以下方法检测:

    - (BOOL) webView:(UIWebView *) webView shouldStartLoadWithRequest:(NSURLRequest *) request navigationType:(UIWebViewNavigationType) navigationType
    {
        url = [request URL];
        NSString *mimeType = [request valueForHTTPHeaderField:@"Content-Type"];
        NSLog(@"Content-type: %@", mimeType);
        if(mimeType == @"application/zip" || mimeType == @"application/x-zip" || mimeType == @"application/octet-stream")
        {
            NSLog(@"Downloading file!");
            [NSThread detachNewThreadSelector:@selector(download:) toTarget:self withObject:@"/tmp/file.ipa"];
            return NO;
        }
        return YES;
    }
    

    但是,当调用此方法时,内容类型头几乎总是(空),因此我永远无法下载文件。

    你如何正确地做到这一点?

    3 回复  |  直到 7 年前
        1
  •  3
  •   Ben Gottlieb    15 年前

    您试图从尚未发出的nsurlrequest中检测内容类型。在使用nsurlconnection发出请求之前,您不会知道内容类型。在本例中,我可能只查看URL路径的文件扩展名。

        2
  •  0
  •   Alex    15 年前

    问题是: UIWebView 不会下载它无法显示的任何内容,也不知道如何显示zip文件。它总是在填充内容类型之前失败。

    那么,该怎么办?我不知道你的服务器端应用是否运行在iPhone上,但是你可以注册一个自定义的URL方案,链接如下 myapplication://example.com/stuff/yourhexurlgoeshere .您可以为 myapplication 方案。几秒钟的谷歌搜索产生 this site 这就解释了怎么做。

    这还有一个额外的好处,因为如果你,比方说,通过电子邮件向另一个用户发送了这样的链接,他们可以在邮件中点击它,并在你的应用程序中打开它。

        3
  •  0
  •   Jack    7 年前

    ---------- 斯威夫特4 + -----

    音频/MP3检测示例-

    步骤1: 使用委托

    class ViewController : WKUIDelegate,WKNavigationDelegate {
    

    步骤2: 设置WebKIT

     func setWebView() {
            let webConfiguration = WKWebViewConfiguration()
            webView = WKWebView(frame: .zero, configuration: webConfiguration)
            webView.uiDelegate = self
            webView.navigationDelegate = self
            view = webView
    
            let myURL = URL(string: "https://www.bossmobi.guru/files/download/type/320/id/197255")//your audio url
            let myRequest = URLRequest(url: myURL!)
            webView.load(myRequest)
        }
    

    步骤3: 从WebKit委托获取音频MIME类型。

     func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) {
            print( #function + "url is \(String(describing: webView.url))"  + "Mimetype" + "\(navigationResponse.response.mimeType ?? "NotAvailable")")
            if let _ = navigationResponse.response.mimeType?.range(of: "audio/mpeg") {
                print("MP3 is audio url \(String(describing: webView.url))")
                webView.stopLoading()
            }
            decisionHandler(.allow)
        }
    

    ---------- Objc ---------

    WKWebVIEW设置

      NSString *urlString = @"https://www.bossmobi.guru/files/download/type/320/id/197255";
    
        WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init];
        WKWebView *_demoWKWebView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:theConfiguration];
        _demoWKWebView.navigationDelegate = self;
        _demoWKWebView.UIDelegate = self;
        NSURL *nsurl=[NSURL URLWithString:urlString];
        NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
        [_demoWKWebView loadRequest:nsrequest];
        [self.view addSubview:_demoWKWebView];
    

    WKWebView委托

    -(void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler {
        //NSLog(@"decidePolicyForNavigation---Response  %@",webView.URL);
        if ([navigationResponse.response.MIMEType isEqualToString:@"audio/mpeg"]) {
            NSLog(@"MP3  audio url is %@",webView.URL);
        }
        decisionHandler(WKNavigationResponsePolicyAllow);
    }