如何在新的 UIViewController 中打开我在 UIWebView 中的 mp3 链接?

How to open mp3 links I have in UIWebView, in new UIViewController?

我的问题是如何在新 tap/viewController 中打开我的 UIWebView 中的 mp3 链接?或者如何解决我遇到的问题(mp3 播放器不适合屏幕)。

使用 WebView 的委托拦截请求 URL。例如,您可以检查 URL 是否包含“.mp3”,然后加载您自己的 UIViewController 实例。

具体拦截方法

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType

这是一个关于您在 ObjC 中应该做什么的示例。

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{

if([request.URL.absoluteString containsString:@".mp3"]){

    UIViewController *playViewController = [[UIViewController alloc] init];
    [self.navigationController pushViewController:playViewController animated:YES];
    return NO;
}
else
    return YES; }

如下所示,使用您的 ViewController 而不是 settingVC

Objective C

Swift

 func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
                    if navigationType == UIWebViewNavigationType.LinkClicked {
    if request.URL.absoluteString.containsString(".mp3") {
    let vc = self.storyboard?.instantiateViewControllerWithIdentifier("settingsVC") as! SettingsViewController
                        self.presentViewController(vc, animated: true, completion: nil)
                        return false
                    }
    }           

                    return true
                }