代码中:
func sendMessage(sender:UIButton){ if self.customerItem.phone.isNotEmpty { //首先要判断设备具不具备发送短信功能 if MFMessageComposeViewController.canSendText(){ let controller = MFMessageComposeViewController() //设置短信内容 controller.body = self.messageContentCell.textview.text //设置收件人列表 controller.recipients = [“\(self.customerItem.phone)”] //设置代理 controller.messageComposeDelegate = self //打开界面 self.presentViewController(controller, animated: true, completion: nil) } else { self.errorNotice(“本设备不能发送短信”) } } else { self.errorNotice(“确定客户手机号码”) } } |
和:
if let currentVc = UIApplication.topViewController() { gLog.verbose(“currentVc=\(currentVc), currentVc.presentedViewController=\(currentVc.presentedViewController)”) //currentVc=<Sales_App.CustomerViewController: 0x7bbb3000> //currentVc=<Sales_App.HomeViewController: 0x7d0d3590>, currentVc.presentedViewController=nil let verifyGesturePwdVC = GesturePasswordViewController(curMode: .VerifyGesture, drawStatusNaviView: true, callerVc: currentVc) currentVc.modelPresentViewController(verifyGesturePwdVC, animated: false) } else { gLog.error(“can not find top view controller, can not show authority verify”) } |
调试期间,想要判断currentVc是不是发送短信的界面viewcontroller
发现此时的currentVc是CKSMSComposeController类型的,但是去判断,结果却又出错:
(lldb) po currentVc <CKSMSComposeController: 0x1268697e0> (lldb) po currentVc is CKSMSComposeController error: <EXPR>:2:14: error: use of undeclared type ‘CKSMSComposeController’ currentVc is CKSMSComposeController ^~~~~~~~~~~~~~~~~~~~~~ (lldb) po currentVc is MFMessageComposeViewController error: <EXPR>:2:14: error: use of undeclared type ‘MFMessageComposeViewController’ currentVc is MFMessageComposeViewController ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
所以:想要搞清楚,如何判断当前的视图是发送短信的视图?
搜:
CKSMSComposeController
objective c – iOS – Check if MFMessageComposeViewController is currently visible – Stack Overflow
whether is MFMessageComposeViewController
check MFMessageComposeViewController
后来实在没办法,只能去:
在使用到MFMessageComposeViewController的vc:
SendMessageViewController.swift
中,创建对应的变量:
class SendMessageViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextViewDelegate, UIGestureRecognizerDelegate, MFMessageComposeViewControllerDelegate { var messageComposeVc:MFMessageComposeViewController init(customerItem:CustomerItem){ self.messageComposeVc = MFMessageComposeViewController() } self.messageComposeVc = MFMessageComposeViewController() //设置短信内容 messageComposeVc.body = self.messageContentCell.textview.text //设置收件人列表 messageComposeVc.recipients = [“\(self.customerItem.phone)”] //设置代理 messageComposeVc.messageComposeDelegate = self //打开界面 self.presentViewController(messageComposeVc, animated: true, completion: nil) |
然后在别处调用的时候,判断是非是对应的vc,如果是,主动调用该变量的dimiss去消失掉:
func needDismissSpecialViewController(completion:((Void) -> Void)? = nil) -> Bool { var need = false if let topVcInStack = UIApplication.topViewControllerInStack { if topVcInStack is SendMessageViewController { let sendMsgVc = topVcInStack as! SendMessageViewController gLog.verbose(“before dismiss special view controller \(sendMsgVc)”) need = true //dismiss special view controller sendMsgVc.messageComposeVc.dismissViewControllerAnimated(false, completion: { gLog.verbose(“after dismiss special view controller \(sendMsgVc)”) if completion != nil { completion!() } }) } } return need } if needDismissSpecialViewController(showVerifyGestureVC) { gLog.verbose(“will dismiss special view controller”) } else { showVerifyGestureVC() } |
[总结]
最终还是没办法直接判断,当前正在显示的页面是否是MFMessageComposeViewController
转载请注明:在路上 » [间接解决]swift中想要判断当前是否在发送短信的视图中