Get iPhone battery percentage from WatchKit Extension

How do I do that?


I found these things on the interwebs:

UIDevice.currentDevice().batteryMonitoringEnabled = true

UIDevice.currentDevice().batteryLevel


But when I use those I get an error with Use of unresolved identifier 'UIDevice'. I have imported UIKit...


Another question, how do I get it the other way around? The battery percentage of the Apple Watch from the iPhone itself?

UIDevice only runs on the iphone so, if you're trying to use it in a WatchKit 2

app, you need to make sure it remains in the extension not in the watch app itself.


As for getting the battery level of the watch, there is no public API for this so the

answer is you can't.

Try use WatchConnectivity to get the counterpart battery level , the public API has alot of limits , so we have to do a little trick to break it.

I'm adding the code in the watchkit extension InterfaceController.swift. That would be on the extension then right or am I doing something wrong?


The whole InterfaceController.swift:


import UIKit
import WatchKit
import Foundation
class InterfaceController: WKInterfaceController {
    override func awakeWithContext(context: AnyObject?) {
        super.awakeWithContext(context)
       
        UIDevice.currentDevice().batteryMonitoringEnabled = true
    }
    override func willActivate() {
        super.willActivate()
    }
    override func didDeactivate() {
        super.didDeactivate()
    }
   
    func refresh() {
        var percentage = UIDevice.currentDevice().batteryLevel
    }
}

How would I go about doing that?

Get iPhone battery percentage from WatchKit Extension
 
 
Q