首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在UIWebView中从Javascript中调用Objective-C方法?

如何在UIWebView中从Javascript中调用Objective-C方法?
EN

Stack Overflow用户
提问于 2010-04-29 06:59:00
回答 4查看 7.5K关注 0票数 1

我正在开发一个使用Phonegap的原生iPhone应用程序,所以一切都是在超文本标记语言和JS中完成的。我正在使用Flurry进行分析,并希望使用

代码语言:javascript
复制
[FlurryAPI logEvent:@"EVENT_NAME"];

方法来跟踪事件。有没有办法在Javascript中做到这一点?因此,在跟踪链接时,我会想像这样使用

代码语言:javascript
复制
<a onClick="flurryTrackEvent("Click_Rainbows")" href="#Rainbows">Rainbows</a>
<a onClick="flurryTrackEvent("Click_Unicorns")" href="#Unicorns">Unicorns</a>

"FlurryAPI.h“包含以下内容:

代码语言:javascript
复制
@interface FlurryAPI : NSObject {
}

+ (void)startSession:(NSString *)apiKey;
+ (void)logEvent:(NSString *)eventName;
+ (void)logEvent:(NSString *)eventName withParameters:(NSDictionary *)parameters;
+ (void)logError:(NSString *)errorID message:(NSString *)message exception:(NSException *)exception;

+ (void)setUserID:(NSString *)userID;
+ (void)setEventLoggingEnabled:(BOOL)value;
+ (void)setServerURL:(NSString *)url;
+ (void)setSessionReportsOnCloseEnabled:(BOOL)sendSessionReportsOnClose;

@end

我只对logEvent方法感兴趣。如果到目前为止还不清楚,我对JS很满意,但它是一个正在恢复的Obj-C新手。我读过Apple docs,但这里描述的示例都是针对新声明的方法的,我想这可能会更容易实现,因为已经定义了Obj-C方法。

提前感谢您的任何意见。

EN

回答 4

Stack Overflow用户

发布于 2010-04-29 07:30:44

为此,一种方法是在具有shouldStartLoadEvent的UIWebView上设置一个委托。在该事件中,您可以检查UIWebView试图导航到的URL。现在,要从JavaScript到Objective-C进行通信,您需要指定您自己的自定义锚点,这些锚点将触发不同的操作。例如,要记录某些内容,您可能决定使用锚"#FAPI_LogEvent_Click_Rainbows“。

在JavaScript中,您可以像这样定义方法:

代码语言:javascript
复制
function flurryTrackEvent(text) {
  window.location.href = 'FAPI_LogEvent' + text;
}
function flurrySetUserID(userID) {
  window.location.href = 'FAPI_SetUserID' + userID;
}

接下来,在Objective-C中,您将实现shouldStartLoadEvent并“捕获”这些href导航,并告诉浏览器不要加载它们。您需要自己拆分字符串并调用适当的函数。下面是一些代码:

代码语言:javascript
复制
- (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
  }
}

在Objective-C中已经定义了事件这一事实并没有多大帮助,因为您需要实现自己的路由行为来调用适当的Objective-C方法。利用Objective-C中已经定义的方法并避免硬编码路由逻辑的唯一方法是使用Objective-C中提供的@selectors或类似的动态函数调用。但是,实现起来要复杂得多,而且可能会带来安全风险。我建议实现上面代码中所示的路由逻辑。

票数 3
EN

Stack Overflow用户

发布于 2011-10-26 23:35:33

PhoneGap具有添加原生插件的功能,要为iOS添加Flurry日志事件插件,我会这样做:

添加一个PGFlurry PhoneGap插件类:

PGFlurry.h

代码语言:javascript
复制
#import <PhoneGap/PGPlugin.h>

@interface PGFlurry : PGPlugin
- (void)logEvent:(NSArray*)arguments withDict:(NSDictionary*)options;
@end

PGFlurry.m

代码语言:javascript
复制
#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

www文件夹中添加一个JavaScript插件助手:

Flurry.js

代码语言:javascript
复制
PhoneGap.addConstructor(function() {
  if(!window.plugins) {
    window.plugins = {};
  }

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

通过将键和值都为"PGFlurry“的键/值对添加到”plugin“字典,将插件添加到PhoneGap.plist

现在你应该可以像这样使用它了:

代码语言:javascript
复制
<!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
EN

Stack Overflow用户

发布于 2011-11-17 18:42:25

您可以在以下位置找到我编写的Phonegap Flurry插件

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2733676

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档