在电子邮件正文中显示应用和设备信息 - swift

时间:2017-11-13 14:51:48

标签: ios swift email

这如何转换为swift?我需要的是电子邮件附加的应用名称,应用版本和区域设置或国家/地区。

NSString *appName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"];
UIDevice *currentDevice = [UIDevice currentDevice];
NSString *model = [currentDevice model];
NSString *systemVersion = [currentDevice systemVersion];
NSArray *languageArray = [NSLocale preferredLanguages];
NSString *language = [languageArray objectAtIndex:0];
NSLocale *locale = [NSLocale currentLocale];
NSString *country = [locale localeIdentifier];
NSString *appVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:(NSString *)kCFBundleVersionKey];
NSString *emailBody = [NSString stringWithFormat:@"\n\n\n\n\n\n\n\n\nApp Name: %@ \nModel: %@ \nSystem Version: %@ \nLanguage: %@ \nCountry: %@ \nApp Version: %@", appName, model, systemVersion, language, country, appVersion];

到目前为止我找到了

let appName = Bundle.main.object(forInfoDictionaryKey: "CFBundleName") as! String

虽然没有运气将其添加到邮件正文中。

这是我到目前为止所做的。

@IBAction func sendMail(_ sender: AnyObject) {

    let recipients = ["support@enquinn.rocks"]
    let title = "App feedback"
    let appName = Bundle.main.object(forInfoDictionaryKey: "CFBundleName") as! String


    let message = ""

    let mc: MFMailComposeViewController = MFMailComposeViewController()

    mc.mailComposeDelegate = self

    mc.setToRecipients(recipients)
    mc.setSubject(title)
    mc.setMessageBody(message, isHTML: false)

    self.present(mc, animated: true, completion: nil)

1 个答案:

答案 0 :(得分:1)

你正走在正确的道路上。如果您使用的是Swift 4,则可以使用多字符串文字并将信息格式化为:

let appName = Bundle.main.object(forInfoDictionaryKey: "CFBundleName") as! String
let appVersion = "5"


let message = """
App name: \(appName)
Version: \(appVersion)
"""

Swift 3版本:

let appName = Bundle.main.object(forInfoDictionaryKey: "CFBundleName") as! String
let appVersion = "5"

var message = "App name: \(appName)\n"
message += "Version: \(appVersion)"

我刚刚在上面的示例中对您的appVersion进行了硬编码,因为您具体询问了如何将变量插入到邮件中。希望有所帮助。