美文网首页
iOS-OC监听进入后台或者从后台启动

iOS-OC监听进入后台或者从后台启动

作者: iOS苦逼开发 | 来源:发表于2017-06-19 18:06 被阅读1365次

    1、在AppDelegate中

    //进入后台的时候调用
    - (void)applicationDidEnterBackground:(UIApplication *)application {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
        NSLog(@"进入后台");
    }
    
    //从后台进入的时候调用
    - (void)applicationWillEnterForeground:(UIApplication *)application {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
        NSLog(@"回到app");
    }
    

    2、使用通知

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(hadEnterBackGround) UIApplicationDidEnterBackgroundNotification object:nil];
    
    - (void)hadEnterBackGround{
        NSLog(@"进入后台");
    }
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(hadEnterForeGround) name:UIApplicationDidBecomeActiveNotification object:nil];
    
    - (void)hadEnterForeGround{
        NSLog(@"回到app");
    }
    

    相关文章

      网友评论

          本文标题:iOS-OC监听进入后台或者从后台启动

          本文链接:https://www.haomeiwen.com/subject/axhnqxtx.html