4

I use UIWebView to present data, and a spinner to show the loading process. The data is an .mp3 file from my server.

I start the spinner when I start loading the webView. Then there is a delay until the audio file starts playing. I need to Stop the spinner quite at that moment.

Notta big deal, but just in case - the loading code:

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

Question: how could I catch the event when the webView is ready to play the audio (when there is enough data to start playing) ? I need it to stop the spinner.

WebViewDidFinishLoad is the only delegate method I could use and it's not good for me, because it notifies when ALL data is loaded. Even if I use it, it is not getting called when the audio file finishes loading (maybe it's not finishing, I don't know, I just see the loading progress gets to the end while the mp3 is playing). Just in case - the 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!

John Smith
  • 2,012
  • 1
  • 21
  • 33

5 Answers5

2

For that kind of control I would not recommend doing it via a webView - you can't get that level of interaction.

Chris
  • 1,637
  • 1
  • 14
  • 20
0

You may try da very popular ASIHTTPRequest class.

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

dklt
  • 1,703
  • 1
  • 12
  • 12
0

Don't worry it's easy !

the -999 for WebView is a normal error that apply when you are loading another content without letting the page really gets fully loaded.

so must of the time do that :

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

To your javascript event detection :

Execute Objective-C methods from JS

This is unfortunately slightly more complex, because there isn't the same windowScriptObject property (and class) that exists on Mac OSX allowing complete communication between the two.

However, you can easily call from javascript custom-made URLs, like:

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

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

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

And intercept it from Objective-C with this:

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

This is not as clean as it should be (or by using windowScriptObject) but it works.

Last solution :

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

Enjoy

xeonarno
  • 426
  • 6
  • 17
0

Instead of using the iOS activity indicator, implement the activity indicator inside the html. It is much simpler end also elegant solution. Just show/hide html element with animated gif image using the javascript.

Krešimir Prcela
  • 4,257
  • 33
  • 46
0

Use a separate class to manage the NSUrlConnection for the retrieval and storage of the .mp3 file. You could use a NSNotificationCenter and the postNotification message or a delegate for when the file is ready to be played

Ben Whiting
  • 153
  • 1
  • 9