我正在开发一个使用Phonegap的原生iPhone应用程序,所以一切都是在超文本标记语言和JS中完成的。我正在使用Flurry进行分析,并希望使用
[FlurryAPI logEvent:@"EVENT_NAME"];方法来跟踪事件。有没有办法在Javascript中做到这一点?因此,在跟踪链接时,我会想像这样使用
<a onClick="flurryTrackEvent("Click_Rainbows")" href="#Rainbows">Rainbows</a>
<a onClick="flurryTrackEvent("Click_Unicorns")" href="#Unicorns">Unicorns</a>"FlurryAPI.h“包含以下内容:
@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方法。
提前感谢您的任何意见。
发布于 2010-04-29 07:30:44
为此,一种方法是在具有shouldStartLoadEvent的UIWebView上设置一个委托。在该事件中,您可以检查UIWebView试图导航到的URL。现在,要从JavaScript到Objective-C进行通信,您需要指定您自己的自定义锚点,这些锚点将触发不同的操作。例如,要记录某些内容,您可能决定使用锚"#FAPI_LogEvent_Click_Rainbows“。
在JavaScript中,您可以像这样定义方法:
function flurryTrackEvent(text) {
window.location.href = 'FAPI_LogEvent' + text;
}
function flurrySetUserID(userID) {
window.location.href = 'FAPI_SetUserID' + userID;
}接下来,在Objective-C中,您将实现shouldStartLoadEvent并“捕获”这些href导航,并告诉浏览器不要加载它们。您需要自己拆分字符串并调用适当的函数。下面是一些代码:
- (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或类似的动态函数调用。但是,实现起来要复杂得多,而且可能会带来安全风险。我建议实现上面代码中所示的路由逻辑。
发布于 2011-10-26 23:35:33
PhoneGap具有添加原生插件的功能,要为iOS添加Flurry日志事件插件,我会这样做:
添加一个PGFlurry PhoneGap插件类:
PGFlurry.h
#import <PhoneGap/PGPlugin.h>
@interface PGFlurry : PGPlugin
- (void)logEvent:(NSArray*)arguments withDict:(NSDictionary*)options;
@endPGFlurry.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在www文件夹中添加一个JavaScript插件助手:
Flurry.js
PhoneGap.addConstructor(function() {
if(!window.plugins) {
window.plugins = {};
}
window.plugins.flurry = {
logEvent: function(name) {
return PhoneGap.exec("PGFlurry.logEvent", name);
}
}
});通过将键和值都为"PGFlurry“的键/值对添加到”plugin“字典,将插件添加到PhoneGap.plist。
现在你应该可以像这样使用它了:
<!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>发布于 2011-11-17 18:42:25
您可以在以下位置找到我编写的Phonegap Flurry插件
https://stackoverflow.com/questions/2733676
复制相似问题