Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's a quick example of a simple implementation that can get you started.</p> <h2>example.html</h2> <pre><code>&lt;script type="text/javacript"&gt; function populateField(fieldId, fieldText) { $('#' + fieldId).val(fieldText); } var bridgeScheme = 'myapp'; (function($) { $('#my-textfield').on('focus', function() { var data = { 'action': 'focus', 'field-id': $(this).attr('id') }; var encodedData = encodeURIComponent(JSON.stringify(data)); $('#iframe').attr('src', bridgeScheme + encodedData); }); })(jQuery) &lt;/script&gt; &lt;form id="my-form"&gt; &lt;div&gt; &lt;input type="text" id="my-textfield"&gt; &lt;/div&gt; &lt;/form&gt; &lt;iframe src="" id="iframe"&gt;&lt;/iframe&gt; </code></pre> <p>A few things:</p> <ul> <li>The <code>iframe</code> is used to generate a request which will go through the <code>UIWebView</code>. We will be able to capture it by implementing <code>UIWebViewDelegate</code> in the controller (see after)</li> <li>The <code>bridgeScheme</code> variable is how you'll be able to detect that a request is coming from your own javascript code (see after)</li> <li>The <code>populateField</code> javascript function will be called from the native side to populate the text field with whatever value you want</li> </ul> <hr> <h2>ExampleViewController.m</h2> <pre><code>- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { // This needs to be the same value as used in the javascript NSString *bridgeScheme = @"myapp"; // That's where we capture the request if the request's scheme matches if ([request.URL.scheme isEqualToString:bridgeScheme]) { // Extract the part of the request url that contains the data we need NSString *dataString = [request.URL.absoluteString substringFromIndex:bridgeScheme.length + 1]; // The data was URL encoded dataString = [dataString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; // Here we transform the JSON string into a JSON object (dictionary) NSData *data = [dataString dataUsingEncoding:NSUTF8StringEncoding]; NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; // Extract the field id from the dictionary NSString *fieldId = dataDictionary[@"field-id"]; // Call the javascript method on the webview NSString *populateFieldJS = [NSString stringWithFormat:@"populateField('%@', '%@')", fieldId, @"Whatever text you want to put in there"]; [webView stringByEvaluatingJavaScriptFromString:populateFieldJS]; return NO; } return YES; } </code></pre> <p>Hope it helps!</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. This table or related slice is empty.
    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