当我尝试通过我的应用程序发送短信时,“消息”屏幕会自动取消

时间:2017-07-24 14:07:27

标签: ios swift sms

我想让我的应用程序接受用户输入来格式化文本消息,以便他们可以通过文本以正确的格式连接到外部设备。我有初始设置文本消息,但如果用户希望删除地理围栏(初始文本消息负责设置),他们需要发送不同的文本消息,但消息屏幕在被调用时会自动取消。

@IBAction func addGeoFencePushed(_ sender: Any) {
    if(!geoFenceIsEnabled()){
        let alertController = UIAlertController(title: nil, message: "How large would you like the geofence to be?", preferredStyle: .alert)
        let confirmAction = UIAlertAction(title: "Confirm", style: .default) { (_) in
            if let field = alertController.textFields?[0] {
                // store your data
                ALGlobal.sharedInstance.globalDefaults.set(field.text! as String, forKey: "geoFenceSize")
                let rad = Double(field.text!)
                ALGlobal.sharedInstance.geoFenceRadius = rad!
                self.buttonSetting = 1
                self.toggleButton()
                self.drawGeoFence(radius: rad!)
                self.turnOnFenceText(radius: Int(rad!))
            } else {
                // user did not fill field
            }
        }
        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (_) in }

        alertController.addTextField { (textField) in
            textField.placeholder = "Feet"
            textField.keyboardType = .numberPad
        }

        alertController.addAction(confirmAction)
        alertController.addAction(cancelAction)

        self.present(alertController, animated: true, completion: nil)
    }
    else{
        // create the alert
        let alert = UIAlertController(title: "Confirm", message: "Are you sure you want to remove the geo-fence?", preferredStyle: UIAlertControllerStyle.alert)

        let confirmAction = UIAlertAction(title: "Confirm", style: .default) { (_) in
            alert.dismiss(animated: true, completion: nil)
            self.removeGeoFence()
        }
        alert.addAction(confirmAction)
        alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))

        // show the alert
        self.present(alert, animated: true, completion: nil)
    }
}

这是负责确定要显示哪条短信的内容。

func removeGeoFence(){
    self.turnOffFenceText()
    ALGlobal.sharedInstance.globalDefaults.removeObject(forKey: "geoFenceSize")
    ALGlobal.sharedInstance.geoFenceRadius = 0
    self.buttonSetting = 0

    self.toggleButton()
    self.clearMap()
}
func turnOnFenceText(radius: Int){
    let radiusConverted = Int(radius / 3)

    messageVC.body = "G1,1,0,\(radiusConverted)M"
    messageVC.recipients = [ALGlobal.sharedInstance.globalDefaults.object(forKey: "devicePhoneNumber") as! String]
    self.present(messageVC, animated: true, completion: nil)
}

func turnOffFenceText(){
    messageVC.body = "G1,0"
    messageVC.recipients = [ALGlobal.sharedInstance.globalDefaults.object(forKey: "devicePhoneNumber") as! String]
    self.present(messageVC, animated: true, completion: nil)
}

func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
    switch (result.rawValue) {
    case MessageComposeResult.cancelled.rawValue:
        print("Message was cancelled")
        messageVC.dismiss(animated: true, completion: nil)
    case MessageComposeResult.failed.rawValue:
        print("Message failed")
        messageVC.dismiss(animated: true, completion: nil)
    case MessageComposeResult.sent.rawValue:
        print("Message was sent")
        messageVC.dismiss(animated: true, completion: nil)
        //self.dismiss(animated: true, completion: nil)
    default:
        break;
    }
}

这些是消息控制器。当调用removeGeoFence()时,控制台打印出"消息被取消"并且消息应用程序永远不会打开,我无法弄清楚原因。

1 个答案:

答案 0 :(得分:0)

你需要在解雇后重新创建messageVC,否则你试图重新打开一个被解雇的视图并导致黑屏,所以在turnOnFenceText()和turnOffFenceText()中我重新初始化了messageVC:

func turnOnFenceText(radius: Int){
        messageVC = MFMessageComposeViewController()
        messageVC.messageComposeDelegate = self;
        let radiusConverted = Int(radius / 3)

        messageVC.body = "G1,1,0,\(radiusConverted)M"
        messageVC.recipients = [ALGlobal.sharedInstance.globalDefaults.object(forKey: "devicePhoneNumber") as! String]
        self.present(messageVC, animated: true, completion: nil)

    }

    func turnOffFenceText(){
        messageVC = MFMessageComposeViewController()
        messageVC.messageComposeDelegate = self;
        messageVC.body = "G1,0"
        messageVC.recipients = [ALGlobal.sharedInstance.globalDefaults.object(forKey: "devicePhoneNumber") as! String]
        self.present(messageVC, animated: true, completion: nil)
    }

解决了这个问题