Zeist Zeist - 3 months ago 25
Swift Question

Check Mac battery percentage in swift

I have been trying to check the mac battery level programmatically.it can be done on ios but i want to do it in mac.i found some resources on stackoverflow but those links were deprecated. Any Ideas?

Answer

First create a "Umbrella-Bridging-Header.h" with the content:

#import <IOKit/ps/IOPowerSources.h>

then in main.swift

import Foundation

println("Hello, World!")
let timeRemaining = IOPSGetTimeRemainingEstimate ()
println("timeRemaining: \(timeRemaining)")
Comments