【问题标题】:How do you get an iPhone's device name如何获取 iPhone 的设备名称
【发布时间】:2010-11-09 03:57:36
【问题描述】:

如果您打开 Settings -> General -> About,屏幕顶部会显示 Bob 的 iPhone。您如何以编程方式获取该名称?

【问题讨论】:

    标签: ios uikit


    【解决方案1】:

    来自 UIDevice 类:

    Swift 版本:

    UIDevice.current.name

    Objective-C 版本:

    [[UIDevice currentDevice] name];

    UIDevice 是一个提供 有关 iPhone 或 iPod 的信息 触摸设备。

    提供的部分信息 UIDevice 是静态的,比如 device 名称或系统版本。

    来源:http://servin.com/iphone/uidevice/iPhone-UIDevice.html

    官方文档: Apple Developer Documentation > UIDevice Class Reference

    【讨论】:

    • 小心:该链接上的教程虽然非常有用,但针对的是 OS 2.2,并使用了 3.0 中已弃用的一些方法。
    • @Tim:你完全正确。我没有想到。不过,我并不是在建议该教程。我只是提供我的信息来源以及更多信息的来源。
    • @FrankV 我应该向用户请求什么权限才能让 myMusicAppName 更改他的 Iphone 名称?我如何在 Swift 中做到这一点?谢谢
    【解决方案2】:

    除了上面的答案,这是实际代码:

    [[UIDevice currentDevice] name];

    【讨论】:

      【解决方案3】:

      以编程方式获取 iPhone 的设备名称

       UIDevice *deviceInfo = [UIDevice currentDevice];
      
       NSLog(@"Device name:  %@", deviceInfo.name); 
      

      // Device name: my iPod

      【讨论】:

        【解决方案4】:

        记住:import UIKit

        斯威夫特:

        UIDevice.currentDevice().name
        

        斯威夫特 3、4、5:

        UIDevice.current.name
        

        【讨论】:

        • 必须有导入 UIKit
        • swift 5.1: UIDevice.current.name
        • 是的,UIDevice.current.name 在 Swift 5 中为我工作。同样,我可以利用 .model .batteryLevel 等谢谢!
        【解决方案5】:

        这是 UIDevice 的类结构

        + (UIDevice *)currentDevice;
        
        @property(nonatomic,readonly,strong) NSString    *name;              // e.g. "My iPhone"
        @property(nonatomic,readonly,strong) NSString    *model;             // e.g. @"iPhone", @"iPod touch"
        @property(nonatomic,readonly,strong) NSString    *localizedModel;    // localized version of model
        @property(nonatomic,readonly,strong) NSString    *systemName;        // e.g. @"iOS"
        @property(nonatomic,readonly,strong) NSString    *systemVersion;
        

        【讨论】:

          【解决方案6】:

          对于 xamarin 用户,请使用此

          UIKit.UIDevice.CurrentDevice.Name
          

          【讨论】:

          • 谢谢。对于 Swift 3:UIDevice.current.name
          【解决方案7】:

          在 Unity 中,使用 C#:

          SystemInfo.deviceName;
          

          【讨论】:

            【解决方案8】:

            对于 Swift 4+ 版本,请使用以下代码:

            UIDevice.current.name
            

            【讨论】:

              【解决方案9】:

              对于swift4.0及以上使用以下代码:

                  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 ?? "") // ABCDEF01-0123-ABCD-0123-ABCDEF012345
                  print(name)       // Name's iPhone
                  print(version)    // 14.5
                  print(modelName)  // iPhone
                  print(osName)     // iOS
                  print(localized)  // iPhone
              

              【讨论】:

              • 这很棒。很有帮助。
              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2012-06-25
              • 1970-01-01
              • 2012-02-04
              相关资源
              最近更新 更多