Unity IOS获取电量和网络类型
来源:互联网 发布:mac ariana grande 编辑:程序博客网 时间:2024/04/30 11:59
由于工作需求,这几天一直在研究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.h和IOPSKeys.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 0
- Unity IOS获取电量和网络类型
- Unity获取手机电量 网络和时间
- Unity显示时间、电量和网络
- Unity获取安卓电量和时间
- 获取 ios 系统网络状况、电量
- 获取 ios 系统网络状况、电量
- unity 获取电量;
- Unity&Android之二:获取手机电量信息、网络状况
- 判断IOS的网络和电量状况
- iOS获取网络类型和运营商信息
- 获取ios设备电量
- 获取ios设备电量
- iOS 获取剩余电量
- unity获取手机电量信息
- iOS 获取网络类型
- ios 获取网络类型
- ios 获取网络类型
- unity如何获取安卓手机的时间电量网络信息
- 操作系统机制学习总结
- SuperMap iMobile 8C(2017) for Android 离线数据使用说明
- C语言——内存的思考
- 第二本书unit8 ldap网络账号
- hdu2571命运 DP
- Unity IOS获取电量和网络类型
- Muli3D <8> 计算Shader中顶点属性相对于屏幕坐标的偏导数
- 基于CLR框架SuperMap Objects .NET快速入门
- [C++](编程珠玑)10^7个数据量的磁盘文件排序
- Unity 和 Android 交互实践与总结(Unity5.6 和 Eclipse 及 AndroidStudio2.3.1)
- QT跨线程连接信号和槽
- NVIC中断优先级管理
- JSP中数据库操作事务处理
- <Effective Mordern C++>笔记:Item 4:Knowing how to view deduced types .