【问题标题】:How to get notified when iPhone battery is at 10%如何在 iPhone 电池电量为 10% 时收到通知
【发布时间】:2015-07-14 17:44:22
【问题描述】:

我正在构建一个 Apple Watch 应用,让你可以看到iPhone 的电池电量, 这是获取电池电量的代码:

 UIDevice.currentDevice().batteryMonitoringEnabled = true
 batteryLevel = UIDevice.currentDevice().batteryLevel

现在我如何在iPhone 电池电量为 10% 时发出通知? 谢谢

【问题讨论】:

    标签: swift notifications watchkit apple-watch battery


    【解决方案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];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多