按钮“取消”不工作。在控制台中几秒钟后出现:
vIEwServiceDIDTerminateWithError: Error Domain=_UIVIEwServiceInterfaceErrorDomain Code=3 "The operation Couldn’t be completed. (_UIVIEwServiceInterfaceErrorDomain error 3.)" UserInfo=0x7f8409f29b50 {Message=Service Connection Interrupted}<MFMailComposeRemoteVIEwController: 0x7f8409c89470> timed out waiting for fence barrIEr from com.apple.MailCompositionService
有我的代码:
func actionSheet(actionSheet: UIActionSheet!,clickedbuttonAtIndex buttonIndex: Int) { if buttonIndex == 0 { println("Export!") var csvString = NSMutableString() csvString.appendString("Date;Time;Systolic;Diastolic;pulse") for tempValue in results { //result define outsIDe this function var tempDateTime = NSDate() tempDateTime = tempValue.datePress var dateFormatter = NSDateFormatter() dateFormatter.dateFormat = "dd-MM-yyyy" var tempDate = dateFormatter.stringFromDate(tempDateTime) dateFormatter.dateFormat = "HH:mm:ss" var tempTime = dateFormatter.stringFromDate(tempDateTime) csvString.appendString("\n\(tempDate);\(tempTime);\(tempValue.sisPress);\(tempValue.diaPress);\(tempValue.hbPress)") } let fileManager = (NSfileManager.defaultManager()) let directorys : [String]? = NSSearchPathForDirectorIEsInDomains(NSSearchPathDirectory.documentDirectory,NSSearchPathDomainMask.AllDomainsMask,true) as? [String] if ((directorys) != nil) { let directorIEs:[String] = directorys!; let dictionary = directorIEs[0]; let pListfile = "bpmonitor.csv" let pListpath = dictionary.stringByAppendingPathComponent(pListfile); println("\(pListpath)") csvString.writetofile(pListpath,atomically: true,enCoding: NSUTF8StringEnCoding,error: nil) var testData: NSData = NSData(contentsOffile: pListpath) var myMail: MFMailComposeVIEwController = MFMailComposeVIEwController() if(MFMailComposeVIEwController.canSendMail()){ myMail = MFMailComposeVIEwController() myMail.mailComposeDelegate = self // set the subject myMail.setSubject("My report") //Add some text to the message body var sentfrom = "Mail sent from BPMonitor" myMail.setMessageBody(sentfrom,isHTML: true) myMail.addAttachmentData(testData,mimeType: "text/csv",filename: "bpmonitor.csv") //display the vIEw controller self.presentVIEwController(myMail,animated: true,completion: nil) } else { var alert = UIAlertController(Title: "Alert",message: "Your device cannot send emails",preferredStyle: UIAlertControllerStyle.Alert) alert.addAction(UIAlertAction(Title: "OK",style: UIAlertActionStyle.Default,handler: nil)) self.presentVIEwController(alert,completion: nil) } } else { println("file system error!") } }}
尝试改为使用UIActivityVIEwController发送邮件:
let fileURL: NSURL = NSURL(fileURLWithPath: pListpath)let actVIEwController = UIActivityVIEwController(activityItems: [fileURL],applicationActivitIEs: nil)self.presentVIEwController(actVIEwController,completion: nil)
看到大致相同的屏幕发送电子邮件,一会儿后返回到上一个屏幕。在控制台中,现在又出现了另一个错误:
vIEwServiceDIDTerminateWithError: Error Domain=_UIVIEwServiceInterfaceErrorDomain Code=3 "The operation Couldn’t be completed. (_UIVIEwServiceInterfaceErrorDomain error 3.)" UserInfo=0x7faab3296ad0 {Message=Service Connection Interrupted}Errors encountered while discovering extensions: Error Domain=PlugInKit Code=13 "query cancelled" UserInfo=0x7faab3005890 {NSLocalizedDescription=query cancelled}<MFMailComposeRemoteVIEwController: 0x7faab3147dc0> timed out waiting for fence barrIEr from com.apple.MailCompositionService
有关于PlugInKit的东西。
使用UIdocumentInteractionController改为UIActivityVIEwController:
let docController = UIdocumentInteractionController(URL: fileURL)docController.delegate = selfdocController.presentPrevIEwAnimated(true)...func documentInteractionControllerVIEwControllerForPrevIEw(controller: UIdocumentInteractionController!) -> UIVIEwController! { return self}
我看到这个屏幕与内容的CSV文件:http://prntscr.com/4ilgax我按右上角的按钮导出,看到这个屏幕http://prntscr.com/4ilguk在那里我选择MAIL和几秒钟我看到http://prntscr.com/4ilh2h然后返回显示文件的内容!在控制台中使用与使用UIActivityVIEwController时相同的消息。
* *重要 – 请勿使用模拟器。 * *即使在2016年,模拟器非常简单地不支持从应用程序发送邮件。
事实上,模拟器根本没有邮件客户端。
但!看到底部的消息!
亨利给了完全答案。你必须
– 在较早阶段分配和启动MFMailComposeVIEwController,和
– 把它保存在一个静态变量中,然后,
– 每当需要时,获取静态MFMailComposeVIEwController实例并使用它。
你几乎肯定必须在每次使用后循环全局MFMailComposeVIEwController。重复使用同一个是不可靠的。
有一个全局例程,释放然后重新初始化单例MFMailComposeVIEwController。每次完成邮件编写器后,调用该全局例程。
在任何singleton。不要忘记,你的应用程序委托是,当然是一个单例,所以做那里…
@property (nonatomic,strong) MFMailComposeVIEwController *globalMailComposer;-(BOol)application:(UIApplication *)application dIDFinishLaunchingWithOptions:(NSDictionary *)launchOptions { ........ // part 3,our own setup [self cycleTheGlobalMailComposer]; // needed due to the worst programming in the history of Apple ......... }
和…
-(voID)cycleTheGlobalMailComposer { // cycling GlobalMailComposer due to idiotic iOS issue self.globalMailComposer = nil; self.globalMailComposer = [[MFMailComposeVIEwController alloc] init]; }
然后使用邮件,这样的东西…
-(voID)helpEmail { // APP.globalMailComposer IS READY TO USE from app launch. // recycle it AFTER OUR USE. if ( [MFMailComposeVIEwController canSendMail] ) { [APP.globalMailComposer setToRecipIEnts: [NSArray arrayWithObjects: emailAddressNsstring,nil] ]; [APP.globalMailComposer setSubject:subject]; [APP.globalMailComposer setMessageBody:msg isHTML:NO]; APP.globalMailComposer.mailComposeDelegate = self; [self presentVIEwController:APP.globalMailComposer animated:YES completion:nil]; } else { [UIAlertVIEw ok:@"Unable to mail. No email on this device?"]; [APP cycleTheGlobalMailComposer]; } }-(voID)mailComposeController:(MFMailComposeVIEwController *)controller dIDFinishWithResult:(MFMailComposeResult)result error:(NSError *)error { [controller dismissVIEwControllerAnimated:YES completion:^ { [APP cycleTheGlobalMailComposer]; } ]; }
{nb,fixed typo per Michael Salamone below。}
为了方便起见,在您的前缀文件中有以下宏
#define APP ((AppDelegate *)[[UIApplication sharedApplication] delegate])
还有一个“小”的问题,可能会花费你的天:http://stackoverflow.com/a/17120065/294884
只是为2016年FTR这里的基本swift代码发送电子邮件IN APP,
class YourClass:UIVIEwController,MFMailComposeVIEwControllerDelegate { func clickedMetrIEArrow() { print("click arrow! v1") let e = MFMailComposeVIEwController() e.mailComposeDelegate = self e.setToRecipIEnts( ["help@smhk.com"] ) e.setSubject("Blah subject") e.setMessageBody("Blah text",isHTML: false) presentVIEwController(e,completion: nil) } func mailComposeController(controller: MFMailComposeVIEwController,dIDFinishWithResult result: MFMailComposeResult,error: NSError?) { dismissVIEwControllerAnimated(true,completion: nil) }
然而!注意!
这些天,它是糟糕的发送电子邮件“在应用程序”。
今天好多了,只是切断电子邮件客户端。
添加到pList …
<key>LSApplicationQuerIEsSchemes</key> <array> <string>instagram</string> </array>
然后代码就好了
func pointlessMarketingEmailForClIEnt() { let subject = "Some subject" let body = "Plenty of <i>email</i> body." let coded = "mailto:blah@blah.com?subject=\(subject)&body=\(body)".stringByAddingPercentEnCodingWithAllowedCharacters(.URLqueryAllowedCharacterSet()) if let emailURL:NSURL = NSURL(string: coded!) { if UIApplication.sharedApplication().canopenURL(emailURL) { UIApplication.sharedApplication().openURL(emailURL) } else { print("fail A") } } else { print("fail B") } }
这些天,这比尝试从“内部”的应用程序电子邮件更好。
再次记住,iOS模拟器根本没有电子邮件客户端(也不能使用应用程序中的作曲家发送电子邮件)。您必须在设备上测试。
总结以上是内存溢出为你收集整理的我有真正的误解与MFMailComposeViewController在Swift(iOS8)在模拟器全部内容,希望文章能够帮你解决我有真正的误解与MFMailComposeViewController在Swift(iOS8)在模拟器所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)