How to send events from HTML running inside a UIWebView to native Objective-C code?

我的未来我决定 提交于 2019-12-03 09:38:37

问题


I want to integrate a full HTML framework (ie HTML/CSS/JavaScript) inside an iOS app and make the UIWebView in charge of running the HTML content being able to communicate with the rest of the native Objective-C source code.

Direction 1: from Objective-C to HTML inside a UIWebView

The way to make the rest of the source send messages to the HTML content is pretty straightforward: i can just call stringByEvaluatingJavaScriptFromString: on the Objective-C side and implement JavaScript methods the right way.

Direction 2: from HTML inside a UIWebView to Objective-C

This is the way I can't really figure out. My only idea so far is to make my app a local web server and make the HTML request stuff to it. But I have no idea how to do that although I suppose it can be bone as I believe apps such as Things, VLC or 1Password might use this kind of features.

Any idea to make this direction 2 work or any new perspective to make events inside HTML content being sent to Objective-C code is welcome.


回答1:


I have done this using jQuery and UIWebViewDelegate:

JavaScript (jQuery mobile):

$("#bloodType").change(function() {
    url = $("#bloodType option:selected").text();
    url = "donordialog:bloodTypeChanged(" + url + ")";
    window.location = url;
});

So, the resulting URL looks like: donordialog:bloodTypeChanged(typeAB-)

In my objc code:

-(BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSURL *URL = [request URL];
    if ([[URL scheme] isEqualToString:@"donordialog"])
    {
        // now we need to figure out the function part
        NSString *functionString = [URL resourceSpecifier];

        if ([functionString hasPrefix:@"bloodTypeChanged"])
        {
            // the blood type has changed, now do something about it.
            NSString *parameter = [functionString stringByReplacingOccurrencesOfString:@"bloodTypeChanged" withString:@""];

            // remove the '(' and then the ')'
            parameter = [parameter stringByReplacingOccurrencesOfString:@"(" withString:@""];
            parameter = [parameter stringByReplacingOccurrencesOfString:@")" withString:@""];

            // log the paramter, as I don't know what to do with it right now
            NSLog(@"%@", parameter);
        }

        return NO;
    }

    return YES;
}

This code was copied verbatim from a project I am currently working on, and can verify that this works.




回答2:


I just created a library that will do this for you. It supports two-way communication between your web app and iOS about through JSON, relying heavily on this method. Check it out: https://github.com/tcoulter/jockeyjs




回答3:


The way you usually talk back from JavaScript is by opening a fictional URL (by window.location), and then implementing UIWebViewDelegate's -webView:shouldStartLoadWithRequest:navigationType: to ignore the navigation and handle what needs to be done instead.

(On Mac OS X WebKit, you can supply Objective-C objects that have JavaScript functions to the web site, but this is not available in iOS.)




回答4:


PhoneGap was built exactly for this



来源:https://stackoverflow.com/questions/9888142/how-to-send-events-from-html-running-inside-a-uiwebview-to-native-objective-c-co

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!