How to get device make and model on iOS?

后端 未结 19 959
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-22 04:58

I was wondering if it\'s possible to determine what kind of iPhone (for example) the currentdevice is? I know it\'s possible to get the model through
NSString

19条回答
  •  终归单人心
    2020-11-22 05:07

    I've optimized NicolasMiari's implementation, and added Simulator, here:

    + (NSString*)deviceName {
    
      static NSDictionary* deviceNamesByCode = nil;
      static NSString* deviceName = nil;
    
      if (deviceName) {
        return deviceName;
      }
    
      deviceNamesByCode = @{
        @"i386"      :@"Simulator",
        @"iPod1,1"   :@"iPod Touch",      // (Original)
        @"iPod2,1"   :@"iPod Touch",      // (Second Generation)
        @"iPod3,1"   :@"iPod Touch",      // (Third Generation)
        @"iPod4,1"   :@"iPod Touch",      // (Fourth Generation)
        @"iPhone1,1" :@"iPhone",          // (Original)
        @"iPhone1,2" :@"iPhone",          // (3G)
        @"iPhone2,1" :@"iPhone",          // (3GS)
        @"iPad1,1"   :@"iPad",            // (Original)
        @"iPad2,1"   :@"iPad 2",          //
        @"iPad3,1"   :@"iPad",            // (3rd Generation)
        @"iPhone3,1" :@"iPhone 4",        //
        @"iPhone4,1" :@"iPhone 4S",       //
        @"iPhone5,1" :@"iPhone 5",        // (model A1428, AT&T/Canada)
        @"iPhone5,2" :@"iPhone 5",        // (model A1429, everything else)
        @"iPad3,4"   :@"iPad",            // (4th Generation)
        @"iPad2,5"   :@"iPad Mini",       // (Original)
        @"iPhone5,3" :@"iPhone 5c",       // (model A1456, A1532 | GSM)
        @"iPhone5,4" :@"iPhone 5c",       // (model A1507, A1516, A1526 (China), A1529 | Global)
        @"iPhone6,1" :@"iPhone 5s",       // (model A1433, A1533 | GSM)
        @"iPhone6,2" :@"iPhone 5s",       // (model A1457, A1518, A1528 (China), A1530 | Global)
        @"iPad4,1"   :@"iPad Air",        // 5th Generation iPad (iPad Air) - Wifi
        @"iPad4,2"   :@"iPad Air",        // 5th Generation iPad (iPad Air) - Cellular
        @"iPad4,4"   :@"iPad Mini",       // (2nd Generation iPad Mini - Wifi)
        @"iPad4,5"   :@"iPad Mini"        // (2nd Generation iPad Mini - Cellular)
      };
    
      struct utsname systemInfo;
      uname(&systemInfo);
      NSString* code = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
    
      deviceName = [deviceNamesByCode objectForKey:code];
    
      if (!deviceName) {
        // Not found in database. At least guess main device type from string contents:
    
        if ([code rangeOfString:@"iPod"].location != NSNotFound) {
          deviceName = @"iPod Touch";
        } else if([code rangeOfString:@"iPad"].location != NSNotFound) {
          deviceName = @"iPad";
        } else if([code rangeOfString:@"iPhone"].location != NSNotFound){
          deviceName = @"iPhone";
        } else {
          deviceName = @"Simulator";
        }
      }
    
      return deviceName;
    }
    

    ```

提交回复
热议问题