Ios 在Swift 2.0中使用MFMailComposeViewController
在我的应用程序中,我需要实现(发送邮件)功能。 我使用MFMailComposeViewController来实现这个功能。 但是我遇到了一个奇怪的问题,MFMailComposeViewController无法发送邮件时,会出现一个奇怪的弹出窗口,如下所示: 我的代码是:Ios 在Swift 2.0中使用MFMailComposeViewController,ios,swift,mfmailcomposeviewcontroller,Ios,Swift,Mfmailcomposeviewcontroller,在我的应用程序中,我需要实现(发送邮件)功能。 我使用MFMailComposeViewController来实现这个功能。 但是我遇到了一个奇怪的问题,MFMailComposeViewController无法发送邮件时,会出现一个奇怪的弹出窗口,如下所示: 我的代码是: let mailComposerVC = MFMailComposeViewController() mailComposerVC.mailComposeDelegate = self
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()
}
有人面临这个问题吗?
请帮忙
谢谢通过模拟器使用
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
通过模拟器使用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
谷歌搜索显示:,这和你的项目有什么关系吗?我认为本地化有问题。如果在模拟器上进行测试,有时会发生忽略:)@DeVladinci nooo我在设备上测试它,而不是在模拟器上:(@MartinR wooow!这正是发生在我身上的事情。但我不知道是否有可能编辑它。你知道这个链接是什么吗?谷歌搜索显示:,这与你的项目有关吗?我认为本地化有问题。如果你在模拟器上测试,忽略它有时会发生:)@DeVladinci nooo我在设备上测试它,而不是在模拟器上:(@MartinR wooow!这正是发生在我身上的事情。但我不知道是否可以编辑它。你知道这个链接是什么吗?不,我在设备上试过:(都是关于Localazations文件的。)我不知道如何解决它:(@Rawan我很抱歉,Rawan。这是我所能帮助的,我以前从未使用过本地字符串。尝试在没有“第一本地优先”的情况下发送电子邮件,可能这不是问题所在。我在设备上尝试过:(这都是关于Localazations文件的。我不知道如何解决它:(@Rawan我很抱歉,Rawan。这是我所能帮助的,我以前从未使用过本地字符串。请尝试在没有“本地优先”的情况下发送电子邮件,也许这不是问题所在