我正在构建一个 Apple Watch 应用程序,让您可以看到 iPhone
电池电量,
这是获取电池电量的代码:
UIDevice.currentDevice().batteryMonitoringEnabled = true
batteryLevel = UIDevice.currentDevice().batteryLevel
现在如何在 iPhone
电池电量为 10% 时调用通知?
谢谢
最佳答案
这是 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];
关于swift - 如何在 iPhone 电池电量为 10% 时收到通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31413715/