如何在IOS中以编程方式获取电池的温度。 IOS 中是否有任何 API 可以为我们提供有关电池制造商等的任何信息。目前我可以获得电池电量和充电状态。想看看 IOS 中是否还有其他与电池相关的信息可以获取
正如 @Fogh 正确指出的那样,使用公共 API 是不可能的。数据可在
IOKit
中获取,但 IOKit
被视为 iOS 上的私有框架。如果您正在开发内部分发,只需使用 IOKit
(这里有一个示例程序,展示如何获取数据:https://github.com/eldoogy/EEIOKitListener)
如果你更具冒险精神,并且想以某种方式在 App Store 应用程序中获取这些数据,你可以使用我编写的一个名为
UIDeviceListener
的类,它基本上可以从系统中窃取数据,而不依赖于任何私有 API,但我不能向你保证苹果会批准它... https://github.com/eldoogy/UIDeviceListener
无论您使用哪种方法,您最终都会得到一个字典,其中包含一个名为
Temperature
的键,它是一个整数,表示电池温度(以摄氏度为单位)(除以 100 为 float
以获得准确值)。
这在公共 API 中是不可能的。
制造商信息可以托管在您的服务器上,您只需检测运行您的应用程序的设备,获取电池统计信息,解析并显示。
对于电池温度,如果没有API,我猜它是使用当前CPU使用率和GPU使用率或其他什么的近似值?
您可以使用 IOKit 中的以下私有 API:
typedef struct __IOHIDEventSystemClient* IOHIDEventSystemClientRef;
typedef struct __IOHIDServiceClient *IOHIDServiceClientRef;
typedef struct __IOHIDEvent *IOHIDEventRef;
typedef double IOHIDFloat;
@interface HIDServiceClient : NSObject
{
struct {
struct __IOHIDEventSystemClient *system;
void *serviceID;
struct __CFDictionary *cachedProperties;
struct IOHIDServiceFastPathInterface **fastPathInterface;
struct IOCFPlugInInterfaceStruct **plugInInterface;
void *removalHandler;
unsigned int primaryUsagePage;
unsigned int primaryUsage;
struct _IOHIDServiceClientUsagePair *usagePairs;
unsigned int usagePairsCount;
} _client;
}
- (id)description;
- (void)dealloc;
- (unsigned long long)_cfTypeID;
@end
IOHIDEventSystemClientRef IOHIDEventSystemClientCreate(CFAllocatorRef allocator);
int IOHIDEventSystemClientSetMatching(IOHIDEventSystemClientRef client, CFDictionaryRef match);
CFArrayRef IOHIDEventSystemClientCopyServices(IOHIDEventSystemClientRef x);
CFStringRef IOHIDServiceClientCopyProperty(HIDServiceClient *service, CFStringRef property);
IOHIDEventRef IOHIDServiceClientCopyEvent(HIDServiceClient *event, int64_t , int32_t, int64_t);
IOHIDFloat IOHIDEventGetFloatValue(IOHIDEventRef event, int32_t field);
它适用于内部测试,但如果您想将其上传到商店,可能会被拒绝。
有关如何使用它的示例。您可以查看此示例应用程序https://github.com/Dev1an/ThermalSensorMonitor
我在运行 iOS 12 的 iPhone 7 和运行 iOS 16 的 iPhone XS 上测试了 IOKit 代码,两者都运行良好。显然,带有 Swift Charts 的代码仅适用于 iOS 16。