0

I'm trying to send sms and email from an iOS app, but I encounter some problems.

I took the code from a tutorial, for example the sms code :

func sendMessage() {
    let prefs = NSUserDefaults.standardUserDefaults()
    var message = MFMessageComposeViewController()

    // Contacts
    var phoneNumbers = [String]()
    var phoneNumberNb: Int = prefs.objectForKey("phonenumberssaved")!.count
    for(var i = 0; i < phoneNumberNb; i++) {
        phoneNumbers.append(prefs.objectForKey("phonenumberssaved")!.objectAtIndex(i) as! String)
    }

    println(phoneNumbers)

    message.recipients = phoneNumbers
    message.body = self.messageText
    message.messageComposeDelegate = self

    self.presentViewController(message, animated: false, completion: nil)
} 

and I printed my datas (they seems to be ok), but when I try to send it, the app crash with a

'fatal error: unexpectedly found nil while unwrapping an Optional value' and it show me a strange file with a line highlighted : '0x4b5164 <+64>: trap'...

9
  • What is the output for. println(phoneNumbers) this sentance
    – V.J.
    Commented Aug 5, 2015 at 10:16
  • the output is : [01 47 89 65 83]
    – Karz
    Commented Aug 5, 2015 at 10:18
  • So, the number is in right format.? I think the number should be like "+1999999999" or "999999999" etc..
    – V.J.
    Commented Aug 5, 2015 at 10:20
  • Thanks, I am going to change it but there is the same error with emails, so I think that's not the only mistake ?
    – Karz
    Commented Aug 5, 2015 at 10:22
  • What is the self.messageText? it is textbox or string variable.? [@"12345678", @"72345524"] your number array must be like this.
    – V.J.
    Commented Aug 5, 2015 at 10:23

2 Answers 2

0

Use conditional unwrapping. Also, you don't need to process each array element:

func sendMessage() {
    let prefs = NSUserDefaults.standardUserDefaults()
    let message = MFMessageComposeViewController()

    // Contacts
    if let phoneNumbers = prefs.objectForKey("phonenumberssaved") as? [String] {
        println(phoneNumbers)

        message.recipients = phoneNumbers
        message.body = "some body"
        message.messageComposeDelegate = self

        self.presentViewController(message, animated: false, completion: nil)
    }
}
1
  • Thanks for your help, I tried your code but not working, same error :S
    – Karz
    Commented Aug 5, 2015 at 10:29
0

You can try this before init your "message", some device don't have option to send message:

if MFMessageComposeViewController.canSendText() {
    ...
}

Verify if phoneNumbers is not nil before setting message.recipients, this can be append if there is no phone number saved in user preferences.

And self.messageText is not nil too by the way.

Hope this can help you.

2
  • I just tested it, and it's crossing right in the statement, but always that error...
    – Karz
    Commented Aug 5, 2015 at 10:35
  • I'm testing the app on an iPod Touch, but it can send message with iMessage with the same api than sms, and it's passing the statement so I guess this isn't the problem
    – Karz
    Commented Aug 5, 2015 at 10:47

Not the answer you're looking for? Browse other questions tagged or ask your own question.