2014-02-24 88 views
1

在我的應用程序中,我有UIWebView,我想檢測MP3文件是否加載(下載)。 所以我用這個UIWebView委託方法:UIWebView檢測mp3文件加載

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

    NSURL *url = [request URL]; 


    if ([[[url pathExtension] lowercaseString] isEqualToString:@"mp3"]) { 
     [self userDidClickUrl:url]; 
     return NO; 
    } 

    return YES; 
} 

的問題是,有時網址是沒有MP3串內部,UIWebView的打開本地播放。有可能檢測到它?我想要檢測何時開始加載mp3文件。

+0

您是否嘗試過allowsInlineMediaPlayback屬性設置爲YES? – Injectios

+0

它應該做什麼? – MTA

+0

Swift 4&ObjC [answer here](https://stackoverflow.com/a/48072230/7576100) – Jack

回答

0

嘗試檢測MIMEType根據您的請求 - 檢查MIMEType從響應是這些種類之一:
audio/mp3 ||音頻/ mpeg3 ||音頻/ x-mp3 ||音頻/ X-MPEG3

更新:檢查這已經回答了:

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

NSURL *url = request.URL; 
NSURLRequest *req = [NSURLRequest requestWithURL:url]; 
NSURLConnection *conn = [NSURLConnection connectionWithRequest:req delegate:self]; 
[conn start]; 
return YES; } 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{ 

NSString *mime = [response MIMEType]; 
NSLog(@"%@",mime); } 

全波紋管鏈接:

UIWebView Delegate get MIME Type

+0

如何從UIWebView獲取響應對象? – sage444

+0

@ user3344236在這個http://musicplanet.in/bollywood-dl/24181-de-di-permission-ritu-pathak-mp3-song-download.html網站,當我點擊立即下載。播放器是開放的,歌曲播放,但我不能得到MIMEType,如音頻/ MP3 ||音頻/ mpeg3 ||音頻/ x-mp3 || audio/x-mpeg3它給我MIMEType text/html所以我怎麼能得到一個MIMEType –

+0

如果服務器沒有返回MIME類型,你可以嘗試搜索它的響應的第一個字節,你可以在這裏檢查我的答案http:// stackoverflow .com/questions/31916714/how-to-differentiate-if-nsdata-is-xls-ppt-or-doc-on-objective-c/34083139#34083139 –