iOS 裝置資訊擷取
1. 擷取裝置的資訊
UIDevice *device = [[UIDevice alloc] init];
NSString *name = device.name;
NSString *model = device.model; // 裝置類型,比如是蘋果還是itouch
NSString *type = device.localizedModel; // 擷取語言版本
NSString *systemName = device.systemName; // 當前運行系統的名稱
NSString *systemVersion = device.systemVersion; //擷取當前系統的版本
NSLog(@%@-%@-%@-%@-%@,name,model,type,systemName,systemVersion);
//iPhone Simulator-iPhone Simulator-iPhone Simulator-iPhone OS-8.1
2. 擷取裝置的唯一識別碼(UDID)
NSString *identifier = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
3.擷取當前螢幕解析度的資訊
CGRect rect = [UIScreen mainScreen].bounds;
CGFloat scale = [UIScreen mainScreen].scale;
CGFloat width = rect.size.width * scale;
CGFloat height = rect.size.height * scale;
4. 擷取電訊廠商的資訊
#import
#import
CTTelephonyNetworkInfo *info = [[CTTelephonyNetworkInfo alloc] init];
CTCarrier *carrier = [info subscriberCellularProvider];
NSString *mCarrier = [NSString stringWithFormat:@%@,[carrier carrierName]]; // 擷取電訊廠商的名稱
NSString *mConnectType = [NSString stringWithFormat:@%@,info.currentRadioAccessTechnology]; // 擷取當前網路類型
5. 添加震動
#import
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate); // 添加震動
但是貌似這個不支援傳入震動時間和模式。
6. 擷取電池的相關資訊
@implementation BatterMonitor
//擷取電池當前的狀態,共有4種狀態
-(NSString*) getBatteryState {
UIDevice *device = [UIDevice currentDevice];
if (device.batteryState == UIDeviceBatteryStateUnknown) {
return @UnKnow;
}else if (device.batteryState == UIDeviceBatteryStateUnplugged){
return @Unplugged;
}else if (device.batteryState == UIDeviceBatteryStateCharging){
return @Charging;
}else if (device.batteryState == UIDeviceBatteryStateFull){
return @Full;
}
return nil;
}
//擷取電量的等級,0.00~1.00
-(float) getBatteryLevel {
return [UIDevice currentDevice].batteryLevel;
}
-(void) getBatteryInfo
{
NSString *state = getBatteryState();
float level = getBatteryLevel()*100.0;
//yourControlFunc(state, level); //寫自己要實現的擷取電量資訊後怎麼處理
}
//開啟對電量和電池狀態的監控,類似定時器的功能
-(void) didLoad
{
[[UIDevice currentDevice] setBatteryMonitoringEnable:YES];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getBatteryInfo:) name:UIDeviceBatteryStateDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getBatteryInfo:) name:UIDeviceBatteryLevelDidChangeNotification object:nil];
[NSTimer scheduledTimerWithTimeInterval:0.5f target:self selector:@selector(getBatteryInfo:) userInfo:nil repeats:YES];
}
@end
7. app中開啟一個網頁
NSString *url = @www.apple.com
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
8. app中開啟另一個app
開啟另一個app還是可以通過openURL來實現。但是要分兩種情況。
第一種是啟動內建的應用,一般的電話,瀏覽器,簡訊和郵件可以直接調用並添加參數,譬如:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@tel://10086]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@mailto://devprograms@apple.com]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@sms://10086]];
第二種情況是要開啟自己開發的app,這種情況則要為將要開啟的app註冊一個URL協議。這個可以在項目的檔案info.plist中註冊。主要操作為:
Step1. 右鍵,選擇“Add Row”
Step2. Key值選擇“URL types”
Step3. 開啟“Item 0″,然後為該key增加一個URL identifier。可以是任何值,但建議用“反網域名稱”(例如 “com.fcplayer.testHello”)。
Step4. 在“Item 0”下再加一行。
Step5. 選擇“URL Schemes” 作為Key。
Step6. 輸入你的URL協議名 (例如“testHello://” 應寫做“testHello”)。如果有必要,你可以在這裡加入多個協議。
其實在開啟的時候只需要URL Schemes即可,URL identifier是可選項。如果需要傳送參數,可以在URL Schemes://添加你的參數,格式和網頁開發的傳遞參數差不多。(又或者URL Schemes://URL identifier@添加的參數)關鍵是要和接收參數方定義好處理的方式。然後在需要開啟的地方添加代碼:
NSString *url = @URL Schemes的路徑
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];