0

所以,我想使用 IOKit 将电池电量显示为使用 theos 的 iOS 3 SDK 的百分比,它编译正确,但是当它试图链接一些符号时,它说:

 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

我的 .h 为 RootViewController 是:

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

@end

我的 .mm 为 RootViewController 是:

#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

请记住,这可以与我的 iPod Touch 2G (iOS 4.2.1) 上的 iOS 3 SDK 正确编译,只是不会链接应用程序。

4

0 回答 0