Note that there are some explanatory texts on larger screens.

plurals
  1. POiOS Calling Objective-C from Javascript: Some calls are ignored?
    primarykey
    data
    text
    <p>I've implemented what seems to be the only way of communicating from javascript to objective-c on iOS using the UIWebView delegate shouldStartLoadWithRequest() method. </p> <p>It seemed to work fine at first, but now I notice that if I make multiple calls from javascript to objective-c within a short period of time, the second call is usually ignored (The application is a piano keyboard, each keypress triggers a call to native code, when dealing with multiple touches the native code doesn't get called for every finger).</p> <p>This is my objective-c code to respond to javascript calls. It's pretty derpy I know but I just wanted something that works for now.</p> <pre><code>- (BOOL)webView:(UIWebView *)webView2 shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { // Intercept custom location change, URL begins with "js-call:" NSString * requestString = [[request URL] absoluteString]; if ([requestString hasPrefix:@"js-call:"]) { // Extract the selector name from the URL NSArray * components = [requestString componentsSeparatedByString:@":"]; NSString * functionCall = [components objectAtIndex:1]; NSArray * params = [functionCall componentsSeparatedByString:@"%20"]; NSString * functionName = [params objectAtIndex:0]; // Parse playnote event if ([functionName isEqualToString:@"playNote"]) { NSString * param = [params objectAtIndex:1]; NoteInstanceID note_id = [m_audioController playNote:[param intValue]]; NSString * jscall = [NSString stringWithFormat:@"document.PlayNoteCallback(%i);", note_id]; NSLog(@"playNote: %i", (int)note_id); [m_webView stringByEvaluatingJavaScriptFromString:jscall]; } // Parse stopnote event if ([functionName isEqualToString:@"stopNote"]) { NSString * param = [params objectAtIndex:1]; NoteInstanceID note_id = [param intValue]; NSLog(@"stopNote: %i", (int)note_id); [m_audioController stopNote:note_id]; } // Parse log event if ([functionName isEqualToString:@"debugLog"]) { NSString * str = [requestString stringByReplacingOccurrencesOfString:@"%20" withString:@" "]; NSLog(@"%s", [str cStringUsingEncoding:NSStringEncodingConversionAllowLossy]); } // Cancel the location change return NO; } // Accept this location change return YES; } </code></pre> <p>From javascript I call objective-c methods by setting the src attribute of a single hidden iframe. This will trigger the delegate method in objective-c and then the desired native code will get called.</p> <pre><code>$("#app_handle").attr("src", "js-call:playNote " + key.data("pitch")); </code></pre> <p>app_handle is the id of the iframe in question. </p> <p>To sum up, the basics of my method work, but multiple calls within a short period of time do not work. Is this just an artifact of the terrible method in which we are forced to communicate from javascript to objective-c? Or am I doing something wrong? I know PhoneGap does something similar to achieve the same goal. I'd rather not use PhoneGap, so if they don't have this problem then I'd love to figure out what they are doing to make this work.</p> <p>Update:</p> <p>I just found this: <a href="https://stackoverflow.com/questions/5671742/send-a-notification-from-javascript-in-uiwebview-to-objectivec">send a notification from javascript in UIWebView to ObjectiveC</a> Which confirms my suspicions about calls made in quick succession getting lost. Apparently I need to either lump my calls together or manually delay the calls so that the url request has returned by the time I make another call. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload