gpt4 book ai didi

ios - 在 Darwin 通知中心回调上调用 UIAlert

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:00:16 27 4
gpt4 key购买 nike

我在我的 ViewDidLoad 上创建了一个 Darwin 通知,我想在调用回调时调用一个 UIAlert。在这种情况下,我想在屏幕解锁时调用警报,为此我将创建一个变量,该变量将在第二次调用此回调时设置为 TRUE/YES(考虑到第一次将一次是在用户锁定屏幕时,第二次是在用户解锁屏幕时)。当此变量为 TRUE/YES 时,将调用警报。

我该怎么做?

我的代码:

- (void)viewDidLoad {
[super viewDidLoad];
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(),
NULL,
hasBlankedScreen,
CFSTR("com.apple.springboard.hasBlankedScreen"),
NULL,
CFNotificationSuspensionBehaviorDeliverImmediately);
}

static void hasBlankedScreen(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo)
{
/*
if(isUnlocked){
[self myAlert];
} else{
isUnlocked = true;
}
*/
}

- (void) myAlert{
UIAlertController * alert= [UIAlertController
alertControllerWithTitle:@"HEY"
message:@"Screen is unlocked"
preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
[self pop];
}];

[alert addAction:defaultAction];

[self presentViewController:alert animated:YES completion:nil];
}

最佳答案

这很简单:

1) 将 (const void*)self 作为观察者传递给 CFNotificationCenterAddObserver 而不是 NULL

2) 在 hasBlankedScreen 中使用 (__bridge ViewController*)observer 而不是 self

完整代码:

- (void)viewDidLoad {
[super viewDidLoad];
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(),
(const void*)self,
hasBlankedScreen,
CFSTR("com.apple.springboard.hasBlankedScreen"),
NULL,
CFNotificationSuspensionBehaviorDeliverImmediately);
}

static void hasBlankedScreen(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) {
if (self.locked) {
[(__bridge ViewController*)observer myAlert];
self.locked = NO;
} else {
self.locked = YES;
}
}

关于ios - 在 Darwin 通知中心回调上调用 UIAlert,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37282038/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com