简体   繁体   English

通过OS X或XCode切换iPhone充电

[英]Toggle iPhone Charging through OS X or XCode

I'm currently writing an iOS application and am receiving battery status change notifications. 我目前正在编写iOS应用程序,并且正在接收电池状态更改通知。 Of course, when I'm debugging my app it would be nice if I could toggle the battery status without unplugging my phone. 当然,当我调试我的应用程序时,如果我可以切换电池状态而不拔下我的手机,那将是很好的。 Is there any way to do this via OS X or XCode, that is, while having the phone plugged in tell it to stop charging so I can test my notifications and actions? 有没有办法通过OS X或XCode这样做,也就是说,在插入手机时告诉它停止充电,以便我可以测试我的通知和操作?

Due to Apple's sandboxing regulations and firmware, you can not tell the phone to stop charging. 由于Apple的沙盒法规和固件,您无法告诉手机停止充电。 This is a service extremely close to the core of the phone, and it can not be programmatically accessed. 这是一种非常接近手机核心的服务,无法以编程方式访问。

Of course, when I'm debugging my app it would be nice if I could toggle the battery status without unplugging my phone. 当然,当我调试我的应用程序时,如果我可以切换电池状态而不拔下我的手机,那将是很好的。

Since you can't do that (per the accepted answer), an alternative that will help you debug is to create and send your own battery status notifications. 由于您不能这样做(根据已接受的答案),可帮助您调试的替代方法是创建并发送您自己的电池状态通知。 The notifications are regular old NSNotification objects, and you can create and send your own with the name set to UIDeviceBatteryLevelDidChangeNotification or UIDeviceBatteryStateDidChangeNotification . 通知是常规的旧NSNotification对象,您可以使用设置为UIDeviceBatteryLevelDidChangeNotificationUIDeviceBatteryStateDidChangeNotificationname创建和发送自己的通知。 That should let you trace through any code that would normally run when your app receives those notifications. 这应该可以让您跟踪应用程序收到这些通知时通常会运行的任何代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM