【发布时间】:2014-02-03 15:49:27
【问题描述】:
当核心位置框架委托方法触发时,我想在我的 IOS 设备中触发警报。当应用程序处于前台时,警报会正确响起。但是当设备被锁定时,警报功能不起作用。这是我使用的代码。
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate date];
localNotification.alertBody = @"body";
localNotification.soundName = @"RING.WAV";
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
是因为我使用了 UILocalNotification 类吗?如果有,是否有替代方案。
锁屏时显示本地通知提示体,但不响闹。
编辑:
在 appdelegate 中,我让代理接收本地通知
- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notification{
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/RING.WAV", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = -1;
if (audioPlayer == nil)
NSLog(@"error :%@",[error description]);
else
[audioPlayer play];
}
我发现当设备被锁定时这个方法不会被触发。当设备被锁定时,有没有办法根据 UILocalNotification 触发警报?
【问题讨论】:
-
您的应用是 VoIP 还是音频?因为当设备被锁定时,应用程序会停止,所以它永远不会执行任何代码。您可以在睡觉前执行代码并安排将来的通知。
-
但是当应用程序在前台时这工作正常。只有当它锁定时,才会出现此问题。
-
您的代码何时何地被调用?
-
以上代码是从 CLLocationManager 委托方法中调用的。
-
您是否在 app-info.plist 中指定了后台模式?你能在你的代码前放一条 NSLog 消息,看看代码是否真的执行了吗?
标签: ios iphone ipad uilocalnotification