2016-03-10 83 views
0

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

enter image description here enter image description here

+1

[打开一个新的UIViewController当一个UIWebView的链接是可能的复制点击(http://stackoverflow.com/questions/10959456/opening-a-new-uiviewcontroller-when-a-link-on-a-uiwebvi现在点击) –

+0

谢谢,但我用swift –

+0

阅读Objective-C几行应该很容易! – Eiko

回答

1

类似下面,而不是使用settingsVC您的ViewController

Objective C

斯威夫特

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 
       } 
+0

我用这个,但没有发生,当我点击MP3链接 –

+0

张贴一些代码如何呈现视图时链接cliked.How你设置webview委托? –

+0

现在我通过使webView作为deleget解决它,现在如何知道哪个链接点击 –

1

使用的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; } 
+0

谢谢,但你能解释更多的代码,因为我是新的快速 –

+0

我不熟悉斯威夫特。我对Objective C很熟悉。你想要一个Objective C样本吗? –

+0

好吧,我会转换它 –

相关问题