552 Stimmen

Wie bestimmt man das aktuelle iPhone/Gerätemodell?

Gibt es einen Weg, um den Gerätemodellnamen (iPhone 4S, iPhone 5, iPhone 5S usw.) in Swift zu erhalten?

Ich weiß, es gibt eine Eigenschaft namens UIDevice.currentDevice().model, aber sie gibt nur den Gerätetyp zurück (iPod touch, iPhone, iPad, iPhone Simulator, usw.).

Ich weiß auch, dass es in Objective-C leicht mit dieser Methode gemacht werden kann:

#import 

struct utsname systemInfo;
uname(&systemInfo);

NSString* deviceModel = [NSString stringWithCString:systemInfo.machine
                          encoding:NSUTF8StringEncoding];

Aber ich entwickle meine iPhone-App in Swift, kann mir also bitte jemand helfen, dies auf äquivalente Weise in Swift zu lösen?

0voto

struct utsname systemInfo;
uname(&systemInfo);

NSString* deviceModel = [NSString stringWithCString:systemInfo.machine
                          encoding:NSUTF8StringEncoding];

0voto

Kiran Patil Punkte 357

Für Swift4.0 und höher wird der folgende Code verwendet:

let udid = UIDevice.current.identifierForVendor?.uuidString
let name = UIDevice.current.name
let version = UIDevice.current.systemVersion
let modelName = UIDevice.current.model
let osName = UIDevice.current.systemName
let localized = UIDevice.current.localizedModel

print(udid ?? "")
print(name)
print(version)
print(modelName)
print(osName)
print(localized)

-2voto

CrazyPro007 Punkte 850
struct DeviceType {
        static let IST_IPHONE_4_ODER_WENIGER = UIDevice.current.userInterfaceIdiom == .phone && Constants.SCREEN_MAX_LENGTH < 568
        static let IST_IPHONE_5 = UIDevice.current.userInterfaceIdiom == .phone && Constants.SCREEN_MAX_LENGTH == 568
        static let IST_IPHONE_6 = UIDevice.current.userInterfaceIdiom == .phone && Constants.SCREEN_MAX_LENGTH == 667
        static let IST_IPHONE_6P = UIDevice.current.userInterfaceIdiom == .phone && Constants.SCREEN_MAX_LENGTH == 736
        static let IST_IPAD = UIDevice.current.userInterfaceIdiom == .pad && Constants.SCREEN_MAX_LENGTH == 1024
    }

-2voto

Amit Kalra Punkte 4085

In Swift 3 wäre es

 UIDevice.current.model

CodeJaeger.com

CodeJaeger ist eine Gemeinschaft für Programmierer, die täglich Hilfe erhalten..
Wir haben viele Inhalte, und Sie können auch Ihre eigenen Fragen stellen oder die Fragen anderer Leute lösen.

Powered by:

X