Ben Cavenagh Ben Cavenagh - 1 year ago 48
Swift Question

When I try to send a text message through my app the Messages screen is automatically cancelled

I am tying to have my app take a users input to format a text message so that they can connect to an external device in the proper format through text. I have the initial setup text message working but if the user wishes to delete the geo-fence (the initial text message is responsible for setting it) they need to send a different text message however the Messages screen is automatically cancelled when called.

@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)
}
}


This is what is responsible for determining which text message to display.

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


And these are the message controllers. When removeGeoFence() is called the console prints out "Message was cancelled" and the Messages app never opens and I can't figure out why.

Answer Source

You need to recreate the messageVC after you dismiss it otherwise your trying to reopen a dismissed view and that results in a black screen so in the turnOnFenceText() and turnOffFenceText() I reinitialized the 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)
    }

That fixed the problem

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download