iOS 的 APP 應用開發的過程當中,有時爲了 bug 跟蹤或者獲取用反饋的須要自動收集用戶設備、系統信息、應用信息等等,這些信息方便開發者診斷問題,固然這些信息是用戶的非隱私信息,是經過開發 api 能夠獲取到的。那麼經過那些 api 能夠獲取這些信息呢,iOS 的 SDK 中提供了 UIDevice,NSBundle,NSLocale。ios
UIDevice 提供了多種屬性、類函數及狀態通知,幫助咱們全方位瞭解設備情況。從檢測電池電量到定位設備與臨近感應,UIDevice 所作的工做就是爲應用程序提供用戶及設備的一些信息。UIDevice 類還可以收集關於設備的各類具體細節,例如機型及 iOS 版本等。其中大部分屬性都對開發工做具備積極的輔助做用。objective-c
bundle 是一個目錄,其中包含了程序會使用到的資源。這些資源包含了如圖像、聲音、編譯好的代碼、nib 文件(用戶也會把 bundle 稱爲 plug-in),對應 bundle,cocoa 提供了類 NSBundle。一個應用程序看上去和其餘文件沒有什麼區別. 可是實際上它是一個包含了 nib 文件,編譯代碼,以及其餘資源的目錄。咱們把這個目錄叫作程序的 main bundle。經過這個路徑能夠獲取到應用的信息,例如應用名、版本號等。api
NSLocale 能夠獲取用戶的本地化信息設置,例如貨幣類型,國家,語言,數字,日期格式的格式化,提供正確的地理位置顯示等等。下面的代碼獲取機器當前語言和國家代碼。安全
// 獲取當前設備 UIDevice *device = [UIDevice currentDevice]; // 獲取設備名稱 /* e.g. "My iPhone" */ NSString *name = device.name; // 獲取設備類型 /* e.g. @"iPhone", @"iPod touch" */ NSString *model = device.model; // 獲取本地化設備類型 /* localized version of model */ NSString *localizedModel = device.localizedModel; // 獲取設備系統名稱 /* e.g. @"iOS" */ NSString *systemName = device.systemName; // 獲取設備系統版本 /* e.g. @"4.0" */ NSString *systemVersion = device.systemVersion; // 獲取設備 UUID /* 可用於惟一標識該設備,同一供應商不一樣應用具備相同的 UUID */ NSUUID *identifierForVendor = device.identifierForVendor; NSString *UUID = identifierForVendor.UUIDString;
// 判斷設備是否生成設備轉向通知 BOOL generatesDeviceOrientationNotifications = device.isGeneratingDeviceOrientationNotifications; // 開啓設備轉向通知 /* 經過調用該方法通知設備:若是用戶改變了設備的朝向,咱們想獲悉這一點 在註冊設備方向通知時,須要先調用該方法 */ [device beginGeneratingDeviceOrientationNotifications]; // 中止設備轉向通知 /* 在移除設備方向通知後,須要調用該方法 */ [device endGeneratingDeviceOrientationNotifications]; // 註冊屏幕方向變化通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationDidChange) name:@"UIDeviceOrientationDidChangeNotification" object:nil]; // 獲取設備方向 /* UIDeviceOrientationUnknown, UIDeviceOrientationPortrait, // home button on the bottom 豎向,頭向上 UIDeviceOrientationPortraitUpsideDown, // home button on the top 豎向,頭向下 UIDeviceOrientationLandscapeLeft, // home button on the right 橫向,頭向左 UIDeviceOrientationLandscapeRight, // home button on the left 橫向,頭向右 UIDeviceOrientationFaceUp, // face up 平放,屏幕朝上 UIDeviceOrientationFaceDown // face down 平放,屏幕朝下 除非正在生成設備方向的通知,不然返回 UIDeviceOrientationUnknown */ UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
// 判斷設備是否開啓電池監控 /* default is NO */ BOOL batteryMonitoringEnabled = device.isBatteryMonitoringEnabled; // 開啓電池監控 /* default is NO */ device.batteryMonitoringEnabled = YES; // 獲取設備電池狀態 /* UIDeviceBatteryStateUnknown, UIDeviceBatteryStateUnplugged, // on battery, discharging 未充電 UIDeviceBatteryStateCharging, // plugged in, less than 100% 正在充電 UIDeviceBatteryStateFull, // plugged in, at 100% 滿電 若是禁用電池監控,則電池狀態爲 UIDeviceBatteryStateUnknown */ UIDeviceBatteryState batteryState = device.batteryState; // 註冊電池狀態變化通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceBatteryStateDidChange) name:@"UIDeviceBatteryStateDidChangeNotification" object:nil]; // 獲取電池電量 /* 0 .. 1.0. 若是電池狀態爲 UIDeviceBatteryStateUnknown,百分比爲 -1.0 */ float batteryLevel = device.batteryLevel; // 註冊電池電量變化通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceBatteryLevelDidChange) name:@"UIDeviceBatteryLevelDidChangeNotification" object:nil];
// 判斷設備是否開啓接近狀態監控 /* default is NO */ BOOL proximityMonitoringEnabled = device.isProximityMonitoringEnabled; // 開啓接近狀態監控 /* default is NO */ device.proximityMonitoringEnabled = YES; // 獲取接近狀態 /* 若是設備不具有接近感應器,則老是返回 NO */ BOOL proximityState = device.proximityState; // 註冊接近狀態變化通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceProximityStateDidChange) name:@"UIDeviceProximityStateDidChangeNotification" object:nil];
// 判斷設備是否支持多任務 BOOL multitaskingSupported = device.multitaskingSupported;
// 獲取用戶界面模式 /* UIUserInterfaceIdiomUnspecified = -1, UIUserInterfaceIdiomPhone // iPhone and iPod touch style UI iPhone 和 iPod touch 風格 UIUserInterfaceIdiomPad // iPad style UI iPad 風格 UIUserInterfaceIdiomTV // Apple TV style UI Apple TV 風格 UIUserInterfaceIdiomCarPlay // CarPlay style UI CarPlay 風格 */ UIUserInterfaceIdiom userInterfaceIdiom = device.userInterfaceIdiom; // 播放一個輸入的聲音 [device playInputClick]; // UIInputViewAudioFeedback 協議方法 /* 實現該方法,返回 YES 則自定義的視圖可以播放輸入的聲音 */ - (BOOL)enableInputClicksWhenVisible { return YES; }
NSDictionary *dicInfo = [[NSBundle mainBundle] infoDictionary]; CFShow((__bridge CFTypeRef)(dicInfo)); // App 應用名稱 NSString *appName = [dicInfo objectForKey:@"CFBundleName"]; // App 應用版本 NSString *appVersion = [dicInfo objectForKey:@"CFBundleShortVersionString"]; // App 應用 Build 版本 NSString *appBuild = [dicInfo objectForKey:@"CFBundleVersion"];
// 語言 en NSString *language = [[NSLocale preferredLanguages] objectAtIndex:0]; // 國家 en_US NSString *country = [[NSLocale currentLocale] localeIdentifier];
#import <AdSupport/AdSupport.h> NSString *adId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
從 17 年 2 月初開始,Apple 開始拒絕採集 IDFA(identifier for advertising)而未集成任何廣告服務的應用進入 AppStore。網絡
NSString *idfv = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
NSString *uuid = [[NSUUID UUID] UUIDString];
NSString *idfv = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
// 查詢 OSStatus SecItemCopyMatching(CFDictionaryRef query, CFTypeRef *result); // 添加 OSStatus SecItemAdd(CFDictionaryRef attributes, CFTypeRef *result); // 更新 KeyChain 中的 ItemOSStatus SecItemUpdate(CFDictionaryRef query, CFDictionaryRef attributesToUpdate); // 刪除 KeyChain 中的 ItemOSStatus SecItemDelete(CFDictionaryRef query)
// 保存一個 UUID 字符串到鑰匙串: CFUUIDRef uuid = CFUUIDCreate(NULL); assert(uuid != NULL); CFStringRef uuidStr = CFUUIDCreateString(NULL, uuid); [SSKeychain setPassword:[NSString stringWithFormat:@"%@", uuidStr] forService:@"com.yourapp.yourcompany"account:@"user"]; // 從鑰匙串讀取 UUID: NSString *retrieveuuid = [SSKeychainpasswordForService:@"com.yourapp.yourcompany"account:@"user"]; // 注意: setPassword 和 passwordForSevice 方法中的 services 和 accounts 參數應該是一致的。
+ (NSString *)getDeviceId { NSString *currentDeviceUUIDStr = [SSKeychain passwordForService:[NSBundle mainBundle].bundleIdentifier account:@"uuid"]; if (currentDeviceUUIDStr == nil || [currentDeviceUUIDStr isEqualToString:@""]) { NSUUID *currentDeviceUUID = [UIDevice currentDevice].identifierForVendor; currentDeviceUUIDStr = currentDeviceUUID.UUIDString; currentDeviceUUIDStr = [currentDeviceUUIDStr stringByReplacingOccurrencesOfString:@"-" withString:@""]; currentDeviceUUIDStr = [currentDeviceUUIDStr lowercaseString]; [SSKeychain setPassword: currentDeviceUUIDStr forService:[NSBundle mainBundle].bundleIdentifier account:@"uuid"]; } return currentDeviceUUIDStr; }