0
當您將iOS應用程序移至後臺時發送事件時,Google Analytics的行爲如何?這些事件是否能夠提供服務?我們是否需要專門調用[[GAI sharedInstance] dispatch];在那些情況下?在iOS中的應用程序處於後臺時發送Google Analytics事件
當您將iOS應用程序移至後臺時發送事件時,Google Analytics的行爲如何?這些事件是否能夠提供服務?我們是否需要專門調用[[GAI sharedInstance] dispatch];在那些情況下?在iOS中的應用程序處於後臺時發送Google Analytics事件
首先,你絕對可以做你的建議,以:
[[GAI sharedInstance] dispatch];
上有谷歌Analytics(分析)背景第二調度here,基本上給你這個方法:
// This method sends any queued hits when the app enters the background.
- (void)sendHitsInBackground {
__block BOOL taskExpired = NO;
__block UIBackgroundTaskIdentifier taskId =
[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
taskExpired = YES;
}];
if (taskId == UIBackgroundTaskInvalid) {
return;
}
__weak AppDelegate *weakSelf = self;
self.dispatchHandler = ^(GAIDispatchResult result) {
// Send hits until no hits are left, a dispatch error occurs, or
// the background task expires.
if (result == kGAIDispatchGood && !taskExpired) {
[[GAI sharedInstance] dispatchWithCompletionHandler:weakSelf.dispatchHandler];
} else {
[[UIApplication sharedApplication] endBackgroundTask:taskId];
}
};
[[GAI sharedInstance] dispatchWithCompletionHandler:self.dispatchHandler];
}
覆蓋applicationDidEnterBackground ,如下所示:
- (void)applicationDidEnterBackground:(UIApplication *)application {
[self sendHitsInBackground];
}
and overri de applicationWillEnterForeground,像這樣:
- (void)applicationWillEnterForeground:(UIApplication *)application {
// Restores the dispatch interval because dispatchWithCompletionHandler
// has disabled automatic dispatching.
[GAI sharedInstance].dispatchInterval = 120;
}