0

So, I want to use IOKit to show the battery level as a percentage for iOS 3 SDK using theos, and it compiles correctly, but when it is trying to link some symbols, it says:

 Linking Application Battery...
Undefined symbols:
 "IOPSCopyPowerSourcesList(void const*)", referenced from:
   -[RootViewController IOKitBatteryLevel] in RootViewController.mm.8c9b32f3.o
 "IOPSCopyPowerSourcesInfo()", referenced from:
   -[RootViewController IOKitBatteryLevel] in RootViewController.mm.8c9b32f3.o
 "IOPSGetPowerSourceDescription(void const*, void const*)", referenced from:
   -[RootViewController IOKitBatteryLevel] in RootViewController.mm.8c9b32f3.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
make[2]: *** [obj/Battery.app/Battery.64873355.unsigned] Error 1
make[1]: *** [internal-application-all_] Error 2
make: *** [Battery.all.application.variables] Error 2

My .h for RootViewController is:

@interface RootViewController: UIViewController {
// some irrelevant declarations
float level;
float percent;
}

@end

And my .mm for RootViewController is:

#import "RootViewController.h"
#include "IOPowerSources.h"
#include "IOPSKeys.h"
#import <IOKit/IOKit.h>
#import <IOKit/IOKitLib.h>
#include <CoreFoundation/CoreFoundation.h>
#include <CoreFoundation/CFDictionary.h>

@implementation RootViewController

- (double)IOKitBatteryLevel {
CFTypeRef blob = IOPSCopyPowerSourcesInfo();
CFArrayRef sources = IOPSCopyPowerSourcesList(blob);

CFDictionaryRef pSource = NULL;
const void *psValue;

int numOfSources = CFArrayGetCount(sources);
if (numOfSources == 0) {
    NSLog(@"-- No power source found");
    return -1.0f;
}

for (int i = 0 ; i < numOfSources ; i++)
{
    pSource = IOPSGetPowerSourceDescription(blob, CFArrayGetValueAtIndex(sources, i));
    if (!pSource) {
        NSLog(@"-- Can't get power source description");
        return -1.0f;
    }
    psValue = (CFStringRef)CFDictionaryGetValue(pSource, CFSTR(kIOPSNameKey));

    int curCapacity = 0;
    int maxCapacity = 0;

    psValue = CFDictionaryGetValue(pSource, CFSTR(kIOPSCurrentCapacityKey));
    CFNumberGetValue((CFNumberRef)psValue, kCFNumberSInt32Type, &curCapacity);

    psValue = CFDictionaryGetValue(pSource, CFSTR(kIOPSMaxCapacityKey));
    CFNumberGetValue((CFNumberRef)psValue, kCFNumberSInt32Type, &maxCapacity);
    percent = (float)((double)curCapacity/(double)maxCapacity * 100.0f);
    return ((double)curCapacity/(double)maxCapacity * 100.0f);
}
return percent;
}

- (void)loadView {
// some code I will not share

[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(batteryStatusDidChange:) name:UIDeviceBatteryLevelDidChangeNotification object:nil];
// some more code I will not share
level = [self IOKitBatteryLevel];
// even more code I will not share
progress.progress = level;
// some code after that
}

- (void)batteryStatusDidChange:(NSNotification *)notification {
level = [self IOKitBatteryLevel];
}

@end

Keep in mind, this compiles correctly with the iOS 3 SDK on my iPod Touch 2G (iOS 4.2.1), it just won't link the app.

Dave1482
  • 3
  • 3
  • iOS 3? Oh man that's really old. I don't know if anyone remembers that sdk. – Adrian P Nov 25 '13 at 23:54
  • i just need info to link the .o files. I know it is, will move to ios 4 soon. – Dave1482 Nov 26 '13 at 00:06
  • i wish someone had iOS 4.2 SDK work with theos. – Dave1482 Nov 26 '13 at 00:07
  • i guess it won't make much difference, because they have to link and not compile because it is already. – Dave1482 Nov 26 '13 at 04:36
  • for purpose of learning you can look it up, but much has changed in iOS sdk's since the 2.0 there are tons of depreciated methods and Xcode itself has changed tremendously. plus if you even find the stuff you are looking for you can't upload the app to app store since the current iOS is 7. better start learning the new stuff my friend. – Adrian P Nov 26 '13 at 17:24
  • i do not have a Mac to do so, nor a compatible device. – Dave1482 Nov 26 '13 at 17:50
  • I am using iOS 3 SDK, as you can probably do barely anything at all with iOS 2. Also, I make sure I don't use depreciated parts in my apps. – Dave1482 Nov 26 '13 at 20:02
  • K, what would be the solution for a higher iOS? – Dave1482 Nov 29 '13 at 01:17
  • Well, here's to coming back to this page a year and a half from the first post. I will see if this will work just for gags, as now theos supports multiple sdks, and iOS 4.2+ can show battery % in the status bar. – Dave1482 Apr 25 '15 at 20:35
  • good to see you are back – Adrian P Apr 26 '15 at 01:54

0 Answers0