首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在杀死应用程序时使用两个UIApplicationDidEnterBackgroundNotification

在杀死应用程序时使用两个UIApplicationDidEnterBackgroundNotification
EN

Stack Overflow用户
提问于 2014-09-15 20:13:29
回答 1查看 1.6K关注 0票数 0

在iOS模拟器7.1中,下面是简单的代码:

  1. command+shift+H模拟按下家庭按钮,输出逻辑为:
    • 激活
    • 辞职
    • 背极接地

  1. 双command+shift+H来模拟双击主页按钮,然后用滑动键杀死应用程序,输出是“奇怪”:
    • 激活
    • 辞职
    • 背极接地
    • 背极接地

出于什么原因,在第二种情况下,搁浅被称为两次?请注意,在iOS 8.0的情况下,2.显示:-激活-辞职-回退

代码语言:javascript
复制
#import "ViewController.h"

@interface ViewController ()
@end

@implementation ViewController {}

- (void)backgrounding {
    NSLog(@"backgrounding");
}

- (void)resigning {
    NSLog(@"resigning");
}

- (void)activing {
    NSLog(@"activing");
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(backgrounding) name:UIApplicationDidEnterBackgroundNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(resigning) name:UIApplicationWillResignActiveNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(activing) name:UIApplicationDidBecomeActiveNotification object:nil];
}

@end
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-09-15 21:16:21

iOS 7(至少在模拟器中)发送两个回退事件。您只需编写您的应用程序,以处理多个后台事件优雅。

我通过创建一个记录每个事件的自定义UIApplication子类以及它的内部GraphicsServices事件(如果存在这样的话)来检查这一点:

代码语言:javascript
复制
#import "MyApplication.h"

@interface NSObject (hack)

- (id)_gsEvent;

@end

@implementation MyApplication

- (void)sendEvent:(UIEvent *)event {
    NSLog(@"event=%@", event);
    if ([event respondsToSelector:@selector(_gsEvent)]) {
        NSLog(@"_gsEvent=%@", event._gsEvent);
    }
    [super sendEvent:event];
}

@end

以下是在多任务屏幕中删除应用程序的输出:

代码语言:javascript
复制
2014-09-15 16:14:32.894 background[19438:70b] event=<UIInternalEvent: 0x8c148e0>
2014-09-15 16:14:32.894 background[19438:70b] _gsEvent=<GSEvent 0xd9149f0>{type = 32, windowLoc = (0.000000, 0.000000)}
2014-09-15 16:14:32.895 background[19438:70b] event=<UIInternalEvent: 0x8c148e0>
2014-09-15 16:14:32.895 background[19438:70b] _gsEvent=<GSEvent 0x8f1ac20>{type = 7d7, windowLoc = (0.000000, 0.000000)}
2014-09-15 16:14:32.896 background[19438:70b] backgrounding
2014-09-15 16:14:32.943 background[19438:70b] event=<UIInternalEvent: 0x8c148e0>
2014-09-15 16:14:32.944 background[19438:70b] _gsEvent=<GSEvent 0x8b22c90>{type = 7d7, windowLoc = (0.000000, 0.000000)}
2014-09-15 16:14:32.944 background[19438:70b] backgrounding

您可以看到有两个单独的GraphicsServices事件(具有不同的地址),每个UIApplicationDidEnterBackgroundNotification都有一个事件。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25856186

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档