【问题标题】:Sending Google Analytics events when the app is in the background on iOS在 iOS 上的应用处于后台时发送 Google Analytics 事件
【发布时间】:2017-09-29 07:52:47
【问题描述】:

当您在 iOS 应用程序移至后台时发送事件时,谷歌分析的行为是什么。这些事件是否会影响服务?我们是否必须专门调用 [[GAI sharedInstance] dispatch];在那些情况下?

【问题讨论】:

    标签: ios objective-c google-analytics


    【解决方案1】:

    首先,您绝对可以按照您的建议进行操作:

    [[GAI sharedInstance] dispatch];
    

    Google Analytics 后台有第二个 dispatch 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];
    }
    

    并覆盖 applicationWillEnterForeground,如下所示:

    - (void)applicationWillEnterForeground:(UIApplication *)application {
      // Restores the dispatch interval because dispatchWithCompletionHandler
      // has disabled automatic dispatching.
      [GAI sharedInstance].dispatchInterval = 120;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-13
      相关资源
      最近更新 更多