Unity IOS获取电量和网络类型

由于工作需求,这几天一直在研究IOS

如何获取到的电量及网络类型

做之前我们首先要找到IOKit.tbd这个动态库及两个API头文件(IOPowerSources.h和IOPSKeys.h)

在Xcode ->显示包含内容->Contents->Developer->Platforms->iPhoneOS.platform->Developer->SDKs->iPhoneOS.sdk->System->Library->Frameworks

下找到IOKit.framework 并找到IOKit.tbd这个库及IOPowerSources.hIOPSKeys.h

Unity 代码:

	[DllImport("__Internal")]
	 static extern int IOS_GetPhoneElectricity();
	[DllImport("__Internal")]
	 static extern string IOS_GetNetworkType();

IOS代码:

 

 

#if defined(__cplusplus)
extern "C"{
#endif
 int IOS_GetPhoneElectricity()
    {
        NSLog(@"IOS_GetPhoneElectricity Start=UnityToIOS batteryLevel");
      return  (int)[UnityToIOS batteryLevel];
    }
 char* IOS_GetNetworkType ()
    {
        NSString *tmp =[UnityToIOS networkingStatesFromStatebar];
        const char* str =[tmp UTF8String];
        char* res = (char*)malloc([tmp length]+1);
        strcpy(res, str);
        
       return  res;
    }
#if defined(__cplusplus)
}
#endif

+ (NSString *)networkingStatesFromStatebar
{
    // 状态栏是由当前app控制的,首先获取当前app
    UIApplication *app = [UIApplication sharedApplication];
    
    NSArray *children = [[[app valueForKeyPath:@"statusBar"] valueForKeyPath:@"foregroundView"] subviews];
    
    int type = 0;
    for (id child in children) {
        if ([child isKindOfClass:[NSClassFromString(@"UIStatusBarDataNetworkItemView") class]]) {
            type = [[child valueForKeyPath:@"dataNetworkType"] intValue];
        }
    }
    
    NSString *stateString = @"WIFI";
    
    switch (type) {
        case 0:
            stateString = @"None";  // 没有网络
            break;
            
        case 1:
            stateString = @"2G";
            break;
            
        case 2:
            stateString = @"3G";
            break;
            
        case 3:
            stateString = @"4G";
            break;
            
        case 4:
            stateString = @"LTE";  // 比4G更快的蜂窝网
            break;
            
        case 5:
            stateString = @"WIFI";
            break;
            
    }
    
    return stateString;
}
+ (double) batteryLevel
{
    CFTypeRef blob = IOPSCopyPowerSourcesInfo();
    CFArrayRef sources = IOPSCopyPowerSourcesList(blob);
    
    CFDictionaryRef pSource = NULL;
    const void *psValue;
    
    long numOfSources = CFArrayGetCount(sources);
    if (numOfSources == 0) {
        NSLog(@"Error in CFArrayGetCount");
        return -1.0f;
    }
    
    for (int i = 0 ; i < numOfSources ; i++)
    {
        pSource = IOPSGetPowerSourceDescription(blob, CFArrayGetValueAtIndex(sources, i));
        if (!pSource) {
            NSLog(@"Error in IOPSGetPowerSourceDescription");
            return -1.0f;
        }
        psValue = (CFStringRef)CFDictionaryGetValue(pSource, CFSTR(kIOPSNameKey));
        
        int curCapacity = 0;
        int maxCapacity = 0;
        double percent;
        
        psValue = CFDictionaryGetValue(pSource, CFSTR(kIOPSCurrentCapacityKey));
        CFNumberGetValue((CFNumberRef)psValue, kCFNumberSInt32Type, &curCapacity);
        
        psValue = CFDictionaryGetValue(pSource, CFSTR(kIOPSMaxCapacityKey));
        CFNumberGetValue((CFNumberRef)psValue, kCFNumberSInt32Type, &maxCapacity);
        
        percent = ((double)curCapacity/(double)maxCapacity * 100.0f);
        
        return percent;
    }
    return -1.0f;
}


最后感谢那些为程序作出贡献的博客作者,因为有了他们才能让我们程序学习更快,更好

 

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值