1

I'm working on an iOS app that uses HTML for a section -- the bulk of the app is native. I am using the shouldStartLoadWithRequest/custom url scheme mechanism described in great detail all over SO. It's been working great. The issue I've found, however, involves text/textarea fields. In a standalone browser environment, typing text in a text element and then tapping a checkbox (or radio) will fire the on change event for both the text element and the checkbox/radio button. However, I'm finding that in the UIWebView, I'm only getting the onchange when giving the checkbox/radio button focus.

I am trying to stay away from jQuery as much as possible due to the overhead in the UIWebView and the lack of a speedy JavaScript interpreter.

Thanks in advance for any assistance.

Here is my JavaScript:

var allTextFields = document.querySelectorAll("input[type=text]");
for (var i = 0; i < allTextFields.length; i++)
{
    allTextFields[i].onchange = function(){
                                            var scheme = document.getElementById('scheme').value;
                                            value = this.value;
                                            url = scheme + ':' + this.id + ':' + value;
                                            window.location = url;
                                            }

}

var allTextAreas = document.querySelectorAll("textarea");
for (var i = 0; i < allTextAreas.length; i++)
{
    allTextAreas[i].onchange = function(){
        value = this.value;
        var scheme = document.getElementById('scheme').value;
        url = scheme + ':' + this.id + ':' + value;
        window.location = url;
    }

}

var allSelects = document.querySelectorAll("select");
for (var i = 0; i < allSelects.length; i++)
{
    allSelects[i].onchange = function(){
        value = this.value;
        var scheme = document.getElementById('scheme').value;
        url = scheme + ':' + this.id + ':' + value;
        window.location = url;
    }

}

var allCheckboxFields = document.querySelectorAll("input[type=checkbox]");
for (var i = 0; i < allCheckboxFields.length; i++)
{
    allCheckboxFields[i].onchange = function(){
                                                value = this.value;
                                                var scheme = document.getElementById('scheme').value;
                                                url = scheme + ':' + this.id + ':'+ this.checked;
                                                window.location = url;
                                                }
}

and here is the Objective-C

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSURL *url = [request URL];
    if ([[url scheme] isEqualToString:@"file"])
    {
        return YES;
    }
    else
    {
        NSString *valueString = [url resourceSpecifier];

        NSArray *values = [valueString componentsSeparatedByString:@":"];

        [self updateDatabase:[url scheme] fieldName:values[0] value:values[1]];
    }

    return NO;

}
voodoobilly
  • 405
  • 7
  • 18
  • Finally found a solution to this issue here: http://stackoverflow.com/questions/2934789/triggering-shouldstartloadwithrequest-with-multiple-window-location-href-calls – voodoobilly Mar 04 '14 at 18:26
  • Having a similar problem with programmatically creating a select field and it not firing off the onchange in the webview. Works fine in a browser, but not in the webview. – Wedge Martin Mar 10 '14 at 00:55
  • @Wedge Martin -- I haven't had an issue with the selects. Did you try the example in the code above? – voodoobilly Mar 11 '14 at 00:35

0 Answers0