【问题标题】:Using MFMailComposeViewController in Swift 2.0在 Swift 2.0 中使用 MFMailComposeViewController
【发布时间】:2016-04-04 12:35:09
【问题描述】:

在我的应用程序中,我需要实现(发送邮件)功能。 我使用 MFMailComposeViewController 来实现这个功能。 但是我遇到了一个奇怪的问题,当 MFMailComposeViewController 无法发送邮件时,会出现一个奇怪的弹出窗口,如下所示:

我的代码是:

let mailComposerVC = MFMailComposeViewController()

        mailComposerVC.mailComposeDelegate = self

        mailComposerVC.setToRecipients(["in**@******.***"])

        mailComposerVC.setSubject(getEmailSubject())

        let message = "Name: \(self.nameTextField.text!) \n Email: \(self.emailTextField.text!) \n Phone: \(self.phoneTextField.text!) \n \(self.messageTextView.text!)"

        print(message)

        mailComposerVC.setMessageBody(message, isHTML: false)

        if MFMailComposeViewController.canSendMail() {

            self.presentViewController(mailComposerVC, animated: true, completion: nil)

        } else {

            self.showSendMailErrorAlert()

        }

有人遇到过这个问题吗? 请帮忙。

谢谢

【问题讨论】:

  • Google 搜索显示:github.com/Legoless/iOS-Localization/blob/master/7.0.4/iPhone/…,这与您的项目有什么关系吗?
  • 我认为本地化有问题。如果你在模拟器上测试忽略它有时会发生:)
  • @DeVladinci nooo 我在设备上而不是在模拟器上测试它:(
  • @MartinR 哇!这正是发生在我身上的事。但我不知道是否可以编辑它.. 你知道这个链接是什么吗?

标签: ios swift mfmailcomposeviewcontroller


【解决方案1】:

通过模拟器使用MFMailComposeViewController 可能会失败。 最佳做法是在真实设备上进行检查。

我正在附加我的一个项目的工作代码。

 func shareMail()
    {
        let mailClass:AnyClass?=NSClassFromString("MFMailComposeViewController")
        if(mailClass != nil)
        {
            if((mailClass?.canSendMail()) != nil)
            {
                displayComposerSheet()
            }
            else
            {
                launchMailAppOnDevice()
            }
        }
        else
        {
            launchMailAppOnDevice()
        }
    }
    func launchMailAppOnDevice()
    {
        let recipients:NSString="";
        let body:NSString = "hey"
        var email:NSString="\(recipients) \(body)"
        email=email.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!
        UIApplication.sharedApplication().openURL(NSURL(string: email as String)!)
    }
    func displayComposerSheet()
    {
        let picker: MFMailComposeViewController=MFMailComposeViewController()
        picker.mailComposeDelegate=self;
        picker .setSubject("Check out this photo I've edited with PICCELL! ")
        picker.setMessageBody(bodyFor, isHTML: true)
        let data: NSData = UIImagePNGRepresentation(sendImage)!

        picker.addAttachmentData(data, mimeType: "image/png", fileName: "images.png")

        self.presentViewController(picker, animated: true, completion: nil)
    }
    func mailComposeController(controller:MFMailComposeViewController, didFinishWithResult result:MFMailComposeResult, error:NSError?)
    {
        if result == MFMailComposeResultSent
        {
           animateFinished(btn_email)
        }

        self.dismissViewControllerAnimated(true, completion: nil)
    }

别忘了添加代表:

UINavigationControllerDelegate,MFMessageComposeViewControllerDelegate, MFMailComposeViewControllerDelegate

【讨论】:

  • 不,我已经在设备上尝试过 :( 都是关于 Localazations 文件的。我不知道如何解决它 :(
  • @Rawan 对不起,Rawan。这就是我所能提供的帮助,我以前从未使用过本地字符串。尝试不先本地先发送电子邮件,也许这不是问题
猜你喜欢
  • 2014-08-10
  • 2015-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-12
  • 1970-01-01
  • 2015-08-29
相关资源
最近更新 更多