如何获取有关手机版本和手机型号的信息? SwiftUI

Posted

技术标签:

【中文标题】如何获取有关手机版本和手机型号的信息? SwiftUI【英文标题】:How do I get information about the phone version and phone model? SwiftUI 【发布时间】:2021-06-20 15:24:35 【问题描述】:

我有一个代码,允许我从应用程序发送电子邮件消息。如何获取手机型号和ios版本数据..我是SwiftUi的新用户,不胜感激。

请参阅屏幕截图中的示例 Example in the picture

这是我的代码

import Foundation
import SwiftUI
import MessageUI

struct MailView: View 
@State private var showingMail = false

var body: some View 
    VStack 
        Button("Open Mail") 
            self.showingMail.toggle()
        
    
    .sheet(isPresented: $showingMail) 
        MailComposeViewController(toRecipients: [""], mailBody: "Here is mail body") 
            // Did finish action
        
    





struct MailComposeViewController: UIViewControllerRepresentable 

var toRecipients: [String]
var mailBody: String

var didFinish: ()->()

func makeCoordinator() -> Coordinator 
    return Coordinator(self)


func makeUIViewController(context: UIViewControllerRepresentableContext<MailComposeViewController>) -> MFMailComposeViewController 
    
    let mail = MFMailComposeViewController()
    mail.mailComposeDelegate = context.coordinator
    mail.setToRecipients(self.toRecipients)
    mail.setMessageBody(self.mailBody, ishtml: true)
    
    return mail


final class Coordinator: NSObject, MFMailComposeViewControllerDelegate 
    
    var parent: MailComposeViewController
    
    init(_ mailController: MailComposeViewController) 
        self.parent = mailController
    
    
    func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) 
        parent.didFinish()
        controller.dismiss(animated: true)
    


func updateUIViewController(_ uiViewController: MFMailComposeViewController, context: UIViewControllerRepresentableContext<MailComposeViewController>) 
    


【问题讨论】:

【参考方案1】:

你可以这样做

struct ContentView: View 
    
    var systemVersion = UIDevice.current.systemVersion
    var device = UIDevice.current.name
    
      var body: some View 
        VStack 
            Text("iOS Version: \(systemVersion)")
            Text("Device: \(device)")
        
      



https://developer.apple.com/documentation/uikit/uidevice

【讨论】:

你知道如何在我的示例代码中换行写文本吗? .sheet(isPresented: $showingMail) MailComposeViewController(toRecipients: ["mail@mail.ru"], mailBody:"iOS Version: \(systemVersion) Device: \(device) " ) @DenisMel 你可以像这样在"iOS Version: \(systemVersion) \nDevice: \(device)"之间添加\n

以上是关于如何获取有关手机版本和手机型号的信息? SwiftUI的主要内容,如果未能解决你的问题,请参考以下文章

mobile-detect.js获取手机型号和系统

js获取移动端设备信息(IMEM,IMIS,手机型号,系统版本,浏览器信息等)

如何通过adb命令获取手机型号与设备信息

如何查找华为手机的手机名称?

Android 获取手机的厂商型号Android系统版本号IMEI当前系统语言等工具类

如何获取华为手机上EMUI版本号