zoukankan      html  css  js  c++  java
  • iOS判断iPhone型号

    链接: http://stackoverflow.com/questions/11197509/ios-how-to-get-device-make-and-model
    http://stackoverflow.com/questions/26028918/ios-how-to-determine-iphone-model-in-swift

    这种是在较高层次获取设备类型,返回的是 iPhone , iPod , iPad 。适合要求不高的。

    UIDevice.currentDevice().model

    以下可以获取具体型号信息:
    Swift Code:

    //
    //  UIDeviceExtension.swift
    //  StopSmokingPrograms
    //
    //  Created by Fran on 15/12/29.
    //  Copyright © 2015年 kimree. All rights reserved.
    //
    
    import Foundation
    
    public enum DeviceModel : String {
        case Simulator = "Simulator/Sandbox",
        iPod1          = "iPod 1",
        iPod2          = "iPod 2",
        iPod3          = "iPod 3",
        iPod4          = "iPod 4",
        iPod5          = "iPod 5",
        iPad2          = "iPad 2",
        iPad3          = "iPad 3",
        iPad4          = "iPad 4",
        iPhone4        = "iPhone 4",
        iPhone4S       = "iPhone 4S",
        iPhone5        = "iPhone 5",
        iPhone5S       = "iPhone 5S",
        iPhone5C       = "iPhone 5C",
        iPadMini1      = "iPad Mini 1",
        iPadMini2      = "iPad Mini 2",
        iPadMini3      = "iPad Mini 3",
        iPadAir1       = "iPad Air 1",
        iPadAir2       = "iPad Air 2",
        iPhone6        = "iPhone 6",
        iPhone6plus    = "iPhone 6 Plus",
        iPhone6S       = "iPhone 6S",
        iPhone6Splus   = "iPhone 6S Plus",
        Unrecognized   = "?unrecognized?"
    }
    
    extension UIDevice{
    
        public var deviceModel: DeviceModel{
            var systemInfo = utsname()
            uname(&systemInfo)
            let modelCode = withUnsafeMutablePointer(&systemInfo.machine) {
                ptr in String.fromCString(UnsafePointer<CChar>(ptr))
            }
            var modelMap : [ String : DeviceModel ] = [
                "i386"      : .Simulator,
                "x86_64"    : .Simulator,
                "iPod1,1"   : .iPod1,
                "iPod2,1"   : .iPod2,
                "iPod3,1"   : .iPod3,
                "iPod4,1"   : .iPod4,
                "iPod5,1"   : .iPod5,
                "iPad2,1"   : .iPad2,
                "iPad2,2"   : .iPad2,
                "iPad2,3"   : .iPad2,
                "iPad2,4"   : .iPad2,
                "iPad2,5"   : .iPadMini1,
                "iPad2,6"   : .iPadMini1,
                "iPad2,7"   : .iPadMini1,
                "iPhone3,1" : .iPhone4,
                "iPhone3,2" : .iPhone4,
                "iPhone3,3" : .iPhone4,
                "iPhone4,1" : .iPhone4S,
                "iPhone5,1" : .iPhone5,
                "iPhone5,2" : .iPhone5,
                "iPhone5,3" : .iPhone5C,
                "iPhone5,4" : .iPhone5C,
                "iPad3,1"   : .iPad3,
                "iPad3,2"   : .iPad3,
                "iPad3,3"   : .iPad3,
                "iPad3,4"   : .iPad4,
                "iPad3,5"   : .iPad4,
                "iPad3,6"   : .iPad4,
                "iPhone6,1" : .iPhone5S,
                "iPhone6,2" : .iPhone5S,
                "iPad4,1"   : .iPadAir1,
                "iPad4,2"   : .iPadAir2,
                "iPad4,4"   : .iPadMini2,
                "iPad4,5"   : .iPadMini2,
                "iPad4,6"   : .iPadMini2,
                "iPad4,7"   : .iPadMini3,
                "iPad4,8"   : .iPadMini3,
                "iPad4,9"   : .iPadMini3,
                "iPhone7,1" : .iPhone6plus,
                "iPhone7,2" : .iPhone6,
                "iPhone8,1" : .iPhone6S,
                "iPhone8,2" : .iPhone6Splus
            ]
    
            if let model = modelMap[String.fromCString(modelCode!)!] {
                return model
            }
    
            return DeviceModel.Unrecognized
        }
    }

    Objective-C Code:
    UIDevice+DeviceModel.h

    //
    //  UIDevice+DeviceModel.h
    //  TestDemo
    //
    //  Created by Fran on 15/12/29.
    //  Copyright © 2015年 Fran. All rights reserved.
    //
    
    #import <UIKit/UIKit.h>
    
    extern NSString *const Device_Simulator;
    extern NSString *const Device_iPod1;
    extern NSString *const Device_iPod2;
    extern NSString *const Device_iPod3;
    extern NSString *const Device_iPod4;
    extern NSString *const Device_iPod5;
    extern NSString *const Device_iPad2;
    extern NSString *const Device_iPad3;
    extern NSString *const Device_iPad4;
    extern NSString *const Device_iPhone4;
    extern NSString *const Device_iPhone4S;
    extern NSString *const Device_iPhone5;
    extern NSString *const Device_iPhone5S;
    extern NSString *const Device_iPhone5C;
    extern NSString *const Device_iPadMini1;
    extern NSString *const Device_iPadMini2;
    extern NSString *const Device_iPadMini3;
    extern NSString *const Device_iPadAir1;
    extern NSString *const Device_iPadAir2;
    extern NSString *const Device_iPhone6;
    extern NSString *const Device_iPhone6plus;
    extern NSString *const Device_iPhone6S;
    extern NSString *const Device_iPhone6Splus;
    extern NSString *const Device_Unrecognized;
    
    @interface UIDevice (DeviceModel)
    
    -(NSString *) deviceModel;
    
    @end

    UIDevice+DeviceModel.m

    //
    //  UIDevice+DeviceModel.m
    //  TestDemo
    //
    //  Created by Fran on 15/12/29.
    //  Copyright © 2015年 Fran. All rights reserved.
    //
    
    #import "UIDevice+DeviceModel.h"
    #import <sys/utsname.h>
    
    NSString *const Device_Simulator = @"Simulator";
    NSString *const Device_iPod1 = @"iPod1";
    NSString *const Device_iPod2 = @"iPod2";
    NSString *const Device_iPod3 = @"iPod3";
    NSString *const Device_iPod4 = @"iPod4";
    NSString *const Device_iPod5 = @"iPod5";
    NSString *const Device_iPad2 = @"iPad2";
    NSString *const Device_iPad3 = @"iPad3";
    NSString *const Device_iPad4 = @"iPad4";
    NSString *const Device_iPhone4 = @"iPhone 4";
    NSString *const Device_iPhone4S = @"iPhone 4S";
    NSString *const Device_iPhone5 = @"iPhone 5";
    NSString *const Device_iPhone5S = @"iPhone 5S";
    NSString *const Device_iPhone5C = @"iPhone 5C";
    NSString *const Device_iPadMini1 = @"iPad Mini 1";
    NSString *const Device_iPadMini2 = @"iPad Mini 2";
    NSString *const Device_iPadMini3 = @"iPad Mini 3";
    NSString *const Device_iPadAir1 = @"iPad Air 1";
    NSString *const Device_iPadAir2 = @"iPad Mini 3";
    NSString *const Device_iPhone6 = @"iPhone 6";
    NSString *const Device_iPhone6plus = @"iPhone 6 Plus";
    NSString *const Device_iPhone6S = @"iPhone 6S";
    NSString *const Device_iPhone6Splus = @"iPhone 6S Plus";
    NSString *const Device_Unrecognized = @"?unrecognized?";
    
    @implementation UIDevice (DeviceModel)
    
    - (NSString *)deviceModel{
    
        struct utsname systemInfo;
    
        uname(&systemInfo);
    
        NSString* code = [NSString stringWithCString:systemInfo.machine
                                            encoding:NSUTF8StringEncoding];
    
        static NSDictionary* deviceNamesByCode = nil;
    
        if (!deviceNamesByCode) {
    
            deviceNamesByCode = @{
                                  @"i386"      : Device_Simulator,
                                  @"x86_64"    : Device_Simulator,
                                  @"iPod1,1"   : Device_iPod1,
                                  @"iPod2,1"   : Device_iPod2,
                                  @"iPod3,1"   : Device_iPod3,
                                  @"iPod4,1"   : Device_iPod4,
                                  @"iPod5,1"   : Device_iPod5,
                                  @"iPad2,1"   : Device_iPad2,
                                  @"iPad2,2"   : Device_iPad2,
                                  @"iPad2,3"   : Device_iPad2,
                                  @"iPad2,4"   : Device_iPad2,
                                  @"iPad2,5"   : Device_iPadMini1,
                                  @"iPad2,6"   : Device_iPadMini1,
                                  @"iPad2,7"   : Device_iPadMini1,
                                  @"iPhone3,1" : Device_iPhone4,
                                  @"iPhone3,2" : Device_iPhone4,
                                  @"iPhone3,3" : Device_iPhone4,
                                  @"iPhone4,1" : Device_iPhone4S,
                                  @"iPhone5,1" : Device_iPhone5,
                                  @"iPhone5,2" : Device_iPhone5,
                                  @"iPhone5,3" : Device_iPhone5C,
                                  @"iPhone5,4" : Device_iPhone5C,
                                  @"iPad3,1"   : Device_iPad3,
                                  @"iPad3,2"   : Device_iPad3,
                                  @"iPad3,3"   : Device_iPad3,
                                  @"iPad3,4"   : Device_iPad4,
                                  @"iPad3,5"   : Device_iPad4,
                                  @"iPad3,6"   : Device_iPad4,
                                  @"iPhone6,1" : Device_iPhone5S,
                                  @"iPhone6,2" : Device_iPhone5S,
                                  @"iPad4,1"   : Device_iPadAir1,
                                  @"iPad4,2"   : Device_iPadAir2,
                                  @"iPad4,4"   : Device_iPadMini2,
                                  @"iPad4,5"   : Device_iPadMini2,
                                  @"iPad4,6"   : Device_iPadMini2,
                                  @"iPad4,7"   : Device_iPadMini3,
                                  @"iPad4,8"   : Device_iPadMini3,
                                  @"iPad4,9"   : Device_iPadMini3,
                                  @"iPhone7,1" : Device_iPhone6plus,
                                  @"iPhone7,2" : Device_iPhone6,
                                  @"iPhone8,1" : Device_iPhone6S,
                                  @"iPhone8,2" : Device_iPhone6Splus
                                  };
        }
    
        NSString* deviceName = [deviceNamesByCode objectForKey:code];
        if(deviceName){
            return deviceName;
        }
    
        return Device_Unrecognized;
    }
    
    @end
  • 相关阅读:
    解决:RuntimeError: invalid argument 0: Sizes of tensors must match except in dimension 0. Got 544 and 1935 in dimension 2 at ../aten/src/TH/generic/THTensor.cpp:711
    ubuntu16.04 notebook中(在anaconda的虚拟环境) 无法使用 anaconda的库
    多版本pip 指定下载
    Jmeter 使用-JDBC请求
    java基础函数
    Linux下安装bugfree
    linux下安装xampp
    Linux 常用命令
    Linux scp命令
    性能测试工具-Ngrinder使用之IDEA脚本开发环境配置
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/5786457.html
Copyright © 2011-2022 走看看