如何在 iPhone 电池电量为 10% 时收到通知
Posted
技术标签:
【中文标题】如何在 iPhone 电池电量为 10% 时收到通知【英文标题】:How to get notified when iPhone battery is at 10% 【发布时间】:2015-07-14 17:44:22 【问题描述】:我正在构建一个 Apple Watch 应用,让你可以看到iPhone
的电池电量,
这是获取电池电量的代码:
UIDevice.currentDevice().batteryMonitoringEnabled = true
batteryLevel = UIDevice.currentDevice().batteryLevel
现在我如何在iPhone
电池电量为 10% 时发出通知?
谢谢
【问题讨论】:
【参考方案1】:这是 Objective-C,但很容易翻译成 Swift:
[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
float battery = [[UIDevice currentDevice] batteryLevel];
if (battery < alertLevel)
//Send notification
如何发送 UILocalNotification
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:0];
notification.alertBody = @"WARNING!";
notification.timeZone = [NSTimeZone defaultTimeZone];
notification.soundName = UILocalNotificationDefaultSoundName;
notification.applicationIconBadgeNumber = 0;
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"Low battery!" forKey:@"Notification"];
notification.userInfo = infoDict;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
【讨论】:
以上是关于如何在 iPhone 电池电量为 10% 时收到通知的主要内容,如果未能解决你的问题,请参考以下文章