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

标签 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;
    }
}

这些是消息 Controller 。当调用removeGeoFence()时,控制台打印出“消息已取消”,并且消息应用程序从未打开,我不明白为什么。

最佳答案

您需要在关闭 messageVC 后重新创建它,否则您尝试重新打开已关闭的 View 并导致黑屏,因此在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)
    }

问题解决了

关于ios - 当我尝试通过我的应用程序发送短信时,消息屏幕会自动取消,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45282723/

相关文章:

ios - 数据使用和 Firebase 查询

C++和Swift : How are structs handled in C++ stack frames? struct继承的复杂性为什么Swift不支持struct继承?

python - 在 Python 中检测串行端口上的设备名称

android - 我可以发送 "SMS received intent"吗?

javascript - 如何使用 Javascript 读取位于 iPhone PhoneGap 项目文件夹中的 json 文件

ios - 批量/多个 iOS 推送通知代码 - 适用于 2 台设备,但不适用于 100 台设备

swift - 通用完成作为非通用通过

api - 发送短信进行号码验证最便宜的方式?

ios - 核心数据 - 具有目标规范的共享模型?

objective-c - iOS 5 开发者过渡指南?