Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yes, there is a way to bridge javascript from a UIWebView to Obj-C code.</p> <p>Set your UIWebView delegate, and in your delegate paste in the following method</p> <pre><code>// // Map links starting with file:// // ending with #action // with the action of the controller if it exists. // // Open other links in Safari. - (BOOL)webView: (UIWebView*)webView shouldStartLoadWithRequest: (NSURLRequest*)request navigationType: (UIWebViewNavigationType)navigationType { NSString *fragment, *scheme; if (navigationType == UIWebViewNavigationTypeLinkClicked) { [webView stopLoading]; fragment = [[request URL] fragment]; scheme = [[request URL] scheme]; if ([scheme isEqualToString: @"file"] &amp;&amp; [self respondsToSelector: NSSelectorFromString(fragment)]) { [self performSelector: NSSelectorFromString(fragment)]; return NO; } [[UIApplication sharedApplication] openURL: [request URL]]; } return YES; } </code></pre> <p>Then you can write Obj-C methods that'll handle the request. For example. In your webview you might have a button that has a link tag like</p> <pre><code>&lt;a href="file://myGreatApp.com/saveImage"&gt;Click Me to Save Image &lt;/a&gt; </code></pre> <p>The scheme is "file" the fragment is "saveImage". You can now write an Obj-C method</p> <pre><code>-(void)saveImage; </code></pre> <p>That will be called every time a user clicks on the link.</p> <p><strong>Edit:</strong> If you want to pass string parameters in your method, simply append them to your fragment url using javascript. So instead you might have</p> <pre><code>&lt;a href="file://myGreatApp.com/saveImage*asdgo8asdgl35lkjasgd807ll12"&gt;Click Me to Save Image &lt;/a&gt; </code></pre> <p>Then in your Obj-C code, split the fragment on the char '*'. Use the first half as your selector and the second half as the parameter.</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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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