Managing the UIWebView Download Process

I use UIWebView to represent data and a counter to show the loading process. The data is a .mp3 file from my server.

I start the counter when I start loading the webView. Then there is a delay until the audio file starts playing. I need to stop the turn at that moment.

Notta is a big deal, but just in case, the download code:

[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://blablabla.mp3"]]]; 

Question : how can I catch an event when webView is ready to play sound (when there is enough data to start playback)? I need this to stop the counter.

WebViewDidFinishLoad is the only delegate method I could use, and this is not good for me, because it notifies when ALL data is loaded. Even if I use it, it is not called when the audio file finishes downloading (maybe it doesn’t end, I don’t know, I just see that the download progress ends during mp3 playback). Just in case - an error:

 Error Domain=NSURLErrorDomain Code=-999 "The operation couldn’t be completed. (NSURLErrorDomain error -999.)" UserInfo=0x1d39d0 {NSErrorFailingURLKey=http://blablabla.mp3, NSErrorFailingURLStringKey=http://blablabla.mp3} 

Any help / tut / link is appreciated. Thanks in advance!

+4
source share
5 answers

For such control, I would not recommend doing this through webView - you cannot get this level of interaction.

+2
source

You can try da very popular ASIHTTPRequest class.

There is a delegate [didReceiveData:], perhaps suitable for your testing.

0
source

Don’t worry, it’s easy!

-999 for WebView is a common error that occurs when loading other content, preventing the page from really loading completely.

so do this over time:

 if([error code] != NSURLErrorCancelled) return; 

To your javascript event detection:

Run Objective-C Methods from JS

This, unfortunately, is a bit more complicated because the same windowScriptObject property (and class) does not exist on Mac OSX, which provides a complete connection between them.

However, you can easily invoke custom URLs from javascript, for example:

 window.location = yourscheme://callfunction/parameter1/parameter2?parameter3=value 

load the jquery library and use the document.ready event:

  $(document).ready(function() { window.location = yourscheme://callfunction/parameter1/parameter2?parameter3=value }); 

And intercept it with Objective-C as follows:

 - (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType { NSURL *URL = [request URL]; if ([[URL scheme] isEqualToString:@"yourscheme"]) { // parse the rest of the URL object and execute functions } } 

It is not as clean as it should be (or using windowScriptObject), but it works.

Last solution:

Javascript Bridge https://github.com/marcuswestin/WebViewJavascriptBridge

Enjoy

0
source

Instead of using the iOS activity indicator, implement an activity indicator inside html. This is a much simpler and more elegant solution. Just show / hide the html element with an animated gif using javascript.

0
source

Use a separate class to manage NSUrlConnection to extract and store the .mp3 file. You can use the NSNotificationCenter and the postNotification message or delegate when the file is ready for playback.

0
source

Source: https://habr.com/ru/post/1432074/


All Articles