Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check Mac battery percentage in swift

Tags:

macos

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?

like image 895
Zeist Avatar asked Mar 26 '15 12:03

Zeist


People also ask

How do I show battery percentage in Swift?

Xcode 11 • Swift 5.1 Then you can create a computed property to return the battery level: Battery level ranges from 0.0 (fully discharged) to 1.0 (100% charged). Before accessing this property, ensure that battery monitoring is enabled. If battery monitoring is not enabled, battery state is UIDevice.

How do I see my battery percentage on Mac?

Show the percentage of battery charge in the menu bar On your Mac, choose Apple menu > System Preferences, then click Dock & Menu Bar . Click Battery in the sidebar (you may have to scroll to see it), then select “Show Percentage.”

How can I check my battery percentage?

Check battery life & useOpen your phone's Settings app. Under "Battery," see how much charge you have left, and about how long it will last. For details, tap Battery.

Can you add Mac to battery widget?

Answer: A: You would like the Battery Widget on your iPhone to show the battery charge levels for your iPhone, you Mac and your iPad. That is NOT possible. Those are individual widgets on individual devices.


1 Answers

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)")
like image 178
Isidoro Ghezzi Avatar answered Sep 20 '22 02:09

Isidoro Ghezzi