2

I have seen many post here/on SO and many docs on Apple's website about reading the battery level on iOS, but what's the best way to do this on a MacBook? In the past the only way I've found to do this is with a time-consuming bash command piped to grep, which seems very inefficient. (as seen and adapted from, although I can't locate the original source I read, here and here) Is there a better way to do this programmatically? I've also seen this, but that only appears to have an Obj-C version. Is there any way to do this in Swift?

EDIT

There's actually a better way that doesn't take any significant time (ioreg -l -w0 | grep Capacity shows current charge and maximum capacity), but it is still a seemingly unneeded bash script.

Glorfindel
  • 4,057
  • Why not link to a specific instance and explain how much time it takes and what time you seek? What if we’re disappointed (but have a tool that responds) with 5 seconds and you’re stuck at 500? (Also, the battery doesn’t materially change over 15 minutes or more, so why is speed needed here?) – bmike Nov 15 '20 at 23:02
  • 1
    The speed was not really a problem, I simply thought that there must be a better way than calling a bash script. I'll put some links in the question. – Pixelated Fish Nov 15 '20 at 23:05
  • @bmike added the links. – Pixelated Fish Nov 15 '20 at 23:18
  • Nice - so it’s not at all about speed but the existence of an API now? (I was waiting to get you from 5 seconds down to 0.5 seconds if you had something other than ioreg) Since this is API/SDK related, ping be if people vote to close it - we can get it to SO in that case... – bmike Nov 15 '20 at 23:18
  • @bmike Yes, the bash script in the edit is fast enough but I still don't believe it's the best way. – Pixelated Fish Nov 15 '20 at 23:19
  • @bmike to your comment edit: does this not belong here? Since it's a programming question I would say SO, but this is very specific to mac hardware. – Pixelated Fish Nov 15 '20 at 23:21
  • 1
    I’m a mod so I wanted people to know I’m tempted to leave it - we’ll sort out any problems should it happen. If people know and answer, that’s what I care about - it’s a great question so I hope to see it stay here so people can learn how to script in swift and call out to bash (ICK) since sometimes “it works” is better than “this is the pinnacle of beauty and elegance” – bmike Nov 15 '20 at 23:33

1 Answers1

1

Yes, it can be done fully in Swift. You mention that you have found a sub-optimal solution for you in using the "ioreg" command line tool. You can find the full source code (in C) for the ioreg tool here - for inspiration:

https://github.com/opensource-apple/IOKitTools/blob/master/ioreg.tproj/ioreg.c

That tool gets the battery level for you by reading the properties of the I/O devices - the battery level being one such property. It reads them using a function that in Swift is available as IORegistryEntryCreateCFProperties() in the IOKit framework.

nohillside
  • 100,768
jksoegaard
  • 77,783