在开发过程中,app之间的跳转,最通用的方法是通过urlScheme。iOS9以后增加了这样一个弹窗:如果是首次打开某一个第三方app,会弹出如下图的一个Alert,这是系统自动弹出,开发人员无法控制该Alert。
如果想捕捉到用户行为,应该采用何种策略呢?
1.如果用户点击“打开”,自然而然的就会进入到第三方app,app的生命周期就会如下:
弹出Alert=>applicationWillResignActive=>点击打开=>applicationDidEnterBackground=>返回=>applicationWillEnterForeground=>applicationDidBecomeActive
2.如果用户点击“取消”,alert弹窗会消失,app的生命周期如下所示:
弹出Alert=>applicationWillResignActive=>点击取消=>applicationDidBecomeActive
通过上述不同交互会有不同的生命周期,可以用来判断用户的不同操作。对于iOS9以上的系统,通过一个变量记录app的四个生命周期的状态,如果从
applicationWillResignActive
直接跳到applicationDidBecomeActive
,那就是点击来取消按钮;如果中间经历了其他生命周期状态回到applicationDidBecomeActive
,那就是从其他app返回来了。部分代码如下:
@interface AppDelegate ()
@property (nonatomic, assign) int appLifeCycle;
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.appLifeCycle = 5;//app运行时候标志位
NSLog(@"didFinishLaunchingWithOptions");
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
self.appLifeCycle = 0;
NSLog(@"applicationWillResignActive");
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
self.appLifeCycle++;
NSLog(@"applicationDidEnterBackground");
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
self.appLifeCycle++;
NSLog(@"applicationWillEnterForeground");
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
self.appLifeCycle++;
NSLog(@"applicationDidBecomeActive");
if (self.appLifeCycle == 1 && [[[UIDevice currentDevice] systemVersion] floatValue] >= 9.0) {
NSLog(@"it is the first to cancel opening other application above iOS9");
}else if (self.appLifeCycle != 6){
NSLog(@"back to myself");//返回到自己app
}
}
网友评论