IT虾米网

iOS 设备信息获取

developer 2018年06月23日 手机开发 1030 0
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 <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]; // 获取当前网络类型 
 
 
5. 添加震动 
 
 
#import <AudioToolbox/AudioToolbox.h> 
    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]];

评论关闭
IT虾米网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!