1

In my application i have UIWebView and i want to detect if a MP3 file is load(download). So i use this UIWebView Delegate method:

- (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;
}

The problem is that sometimes the URL is without mp3 string inside,and the UIWebView open the Native player. It's possible to detect it? I want to detect when a mp3 file is start loading.

YosiFZ
  • 7,792
  • 21
  • 114
  • 221

1 Answers1

1

Try to detect MIMEType based on your request - check if MIMEType from response is either one of those kind:
audio/mp3 || audio/mpeg3 || audio/x-mp3 || audio/x-mpeg3

Update: check this already answered:

- (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); } 

Full link bellow:

UIWebView Delegate get MIME Type

Community
  • 1
  • 1
ares777
  • 3,590
  • 1
  • 22
  • 23
  • how to get response object from UIWebView? – sage444 Feb 24 '14 at 17:45
  • @user3344236 in this http://musicplanet.in/bollywood-dl/24181-de-di-permission-ritu-pathak-mp3-song-download.html site when i click on download Now. Player is open and song is play but i can not get MIMEType such as audio/mp3 || audio/mpeg3 || audio/x-mp3 || audio/x-mpeg3 it give me MIMEType text/html so how i can get a MIMEType – Chirag Dj Dec 15 '14 at 07:07
  • You can try searching it by the response's first bytes if the server is not returning mime type, you can check my answer here http://stackoverflow.com/questions/31916714/how-to-differentiate-if-nsdata-is-xls-ppt-or-doc-on-objective-c/34083139#34083139 – Bryan P Dec 09 '15 at 06:48
  • Swift 4 & ObjC [answer here](https://stackoverflow.com/a/48072230/7576100) – Jack Jan 03 '18 at 06:28