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
- 獲取設備的唯一標識符(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;
- 獲取運營商的信息
#import <CoreTelephony/CTCarrier.h>
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
CTTelephonyNetworkInfo *info = [[CTTelephonyNetworkInfo alloc] init];
CTCarrier *carrier = [info subscriberCellularProvider];
NSString *mCarrier = [NSString stringWithFormat:@"%@",[carrier carrierName]]; // 獲取運營商的名稱
NSString *mConnectType = [NSString stringWithFormat:@"%@",info.currentRadioAccessTechnology]; // 獲取當前網絡類型
- 添加震動
import <AudioToolbox/AudioToolbox.h>
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate); // 添加震動
但是貌似這個不支持傳入震動時間和模式。
- 獲取電池的相關信息
@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
- app中打開一個網頁
NSString *url = @"www.apple.com"
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
- 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]];</pre>