How do I call an Objective-C method from Javascript in UIWebView?

前端 未结 4 1255
太阳男子
太阳男子 2021-01-25 03:21

I\'m developing a native iPhone app using Phonegap, so everything is done in HTML and JS. I am using the Flurry SDK for analytics and want to use the

[FlurryAPI         


        
相关标签:
4条回答
  • 2021-01-25 03:45

    Don't use their objective-c library, use their js library and you won't have to worry about objective-c. :)

    0 讨论(0)
  • 2021-01-25 03:46

    You can find the Phonegap Flurry Plugin written by me at

    https://github.com/designerkamal/Phonegap-Flurry-Plugin

    0 讨论(0)
  • 2021-01-25 03:48

    PhoneGap has functionality for adding native plugins, to add a Flurry log event plugin for iOS I would do something like this:

    Add a PGFlurry PhoneGap plugin class:

    PGFlurry.h

    #import <PhoneGap/PGPlugin.h>
    
    @interface PGFlurry : PGPlugin
    - (void)logEvent:(NSArray*)arguments withDict:(NSDictionary*)options;
    @end
    

    PGFlurry.m

    #import "PGFlurry.h"
    #import "FlurryAPI.h"
    
    @implementation PGFlurry
    // if you init Flurry somewhere else you can remove this method
    - (PGPlugin*) initWithWebView:(UIWebView*)theWebView {
      self = [super init];
      if (self == nil) {
        return nil;
      }
    
      // init and start Flurry
      [FlurryAPI startSession:@"API key"];
    
      return self;
    }
    
    - (void)logEvent:(NSArray*)arguments withDict:(NSDictionary*)options {
      [FlurryAPI logEvent:[arguments objectAtIndex:0]];
    }
    @end
    

    Add a JavaScript plugin helper to the www folder:

    Flurry.js

    PhoneGap.addConstructor(function() {
      if(!window.plugins) {
        window.plugins = {};
      }
    
      window.plugins.flurry = {
        logEvent: function(name) {
          return PhoneGap.exec("PGFlurry.logEvent", name);
        }
      }
    });
    

    Add the plugin to PhoneGap.plist by adding a key/value pair with both the key and value being "PGFlurry" to the "plugins" dictionary.

    Now you should be able to use it like this:

    <!DOCTYPE html>
    <html>
      <head>
        <script src="phonegap.js" type="text/javascript"></script>
        <script src="flurry.js" type="text/javascript"></script>
        <script type="text/javascript">
          document.addEventListener("deviceready", function() {
            window.plugins.flurry.logEvent("Testing");
          }, false);
        </script>
      </head>
      <body>
      </body>
    </html>
    
    0 讨论(0)
  • 2021-01-25 03:56

    One way to do this is to setup a delegate on the UIWebView which has the shouldStartLoadEvent. Inside that event, you check what URL the UIWebView is trying to navigate to. Now to communicate from JavaScript to Objective-C, you need to specify your own custom anchors which will trigger different actions. For example, to log something, you might decide to use the anchor "#FAPI_LogEvent_Click_Rainbows".

    In JavaScript, you could have methods defined like such:

    function flurryTrackEvent(text) {
      window.location.href = 'FAPI_LogEvent' + text;
    }
    function flurrySetUserID(userID) {
      window.location.href = 'FAPI_SetUserID' + userID;
    }
    

    Next, in Objective-C, you would implement the shouldStartLoadEvent and "capture" these href navigations, and tell the browser not to load them. You will need to split the string up yourself and call the appropriate function. Here's some code:

    - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType () {
      NSString *theAnchor = [[request URL] fragment];
      if ([theAnchor hasPrefix:@"FAPI_LogEvent"]) {
        NSString *textToLog = [theAnchor substringFromIndex:[@"FAPI_LogEvent" length]];
        [FlurryAPI logEvent:textToLog];
        return NO; // prevent the UIWebView from navigating to this anchor
      } else if ([theAnchor hasPrefix:@"FAPI_SetUserID"]) {
        NSString *userID = [theAnchor substringFromIndex:[@"FAPI_SetUserID" length]];
        [FlurryAPI setUserID:userID];
        return NO; // prevent the UIWebView from navigating to this anchor
      }
    }
    

    The fact that the events are already defined in Objective-C doesn't really help much since you need to implement your own routing behavior to call the appropriate Objective-C method. The only way you could take advantage of the fact that the methods are already defined in Objective-C and avoid hard coding the routing logic, would be using @selectors or similar dynamic function calling which is available in Objective-C. However, this is much more complicated to implement and probably presents a security risk. I would recommend implementing the routing logic like is shown in the code above.

    0 讨论(0)
提交回复
热议问题