如何识别用户在iOS中正在进行的最后一项活动

时间:2019-02-19 06:24:57

标签: ios objective-c

假设用户位于应用程序内部,并且用户在再次打开应用程序时将应用程序最小化,则该应用程序不在用户上一次执行的活动中。

如何获得用户最近的活动?

1 个答案:

答案 0 :(得分:0)

您想要获取用户最终使用哪个ViewController

可以通过UIApplicationDelegate提供的实现方法。

代码实现:

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // yzh_findTod Mothod is a class static method that I'm extending on my UIViewController.
    UIViewController* lastActionViewController = [UIViewController yzh_findTopViewController];
    NSUserDefaults* userDefault = [NSUserDefaults standardUserDefaults];
    [userDefault setValue:lastActionViewController forKey:@"lastActionVC"];   
}

+ (UIViewController *)yzh_findTopViewController{

    UIViewController* currenViewController = [self yzh_rootViewController];
    BOOL runLoopFind = YES;
    while (runLoopFind) {
        if (currenViewController.presentedViewController) {
            currenViewController = currenViewController.presentedViewController;
        } else {
            if ([currenViewController isKindOfClass:[UINavigationController class]]) {
                currenViewController = [currenViewController.childViewControllers lastObject];
            } else if ([currenViewController isKindOfClass:[UITabBarController class]]) {
                UITabBarController* tabBarController = (UITabBarController* )currenViewController;
                currenViewController = tabBarController.selectedViewController;
            } else {
                if (currenViewController.childViewControllers.count > 0) {
                    currenViewController = [currenViewController.childViewControllers lastObject];
                } else {
                    return currenViewController;
                }
            }
        }
    }

    return currenViewController;
}

+ (UIViewController *)yzh_rootViewController{

    UIWindow* window = YZHAppWindow;
    return window.rootViewController;
}

我希望我的回答对您有帮助