1

Xcode 6.4

Using parse on my app, retrieving users via geopoint. Now, how can I send text message via Parse? or, if it's no possible, could I send a message via MFMessageComposeViewControllerDelegate but using only an email or the Parse objetId of the user? I don't want to use/request phone numbers.

import UIKit
import MessageUI


class ViewController: UIViewController, MFMessageComposeViewControllerDelegate  {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }





    //MARK: let's add a method required to conform to MFMessageComposeViewControllerDelegate
    func messageComposeViewController(controller: MFMessageComposeViewController!, didFinishWithResult result: MessageComposeResult) {
        switch (result.value) {
        case MessageComposeResultCancelled.value:
            println("Message was cancelled")
            self.dismissViewControllerAnimated(true, completion: nil)
        case MessageComposeResultFailed.value:
            println("Message failed")
            self.dismissViewControllerAnimated(true, completion: nil)
        case MessageComposeResultSent.value:
            println("Message was sent")
            self.dismissViewControllerAnimated(true, completion: nil)
        default:
            break;
        }
    }






    @IBAction func sendMessage(sender: UIButton) {

//        MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
        var messageVC = MFMessageComposeViewController()

        messageVC.body = "Enter a message"
        messageVC.recipients = ["Enter tel-nr"]
        messageVC.messageComposeDelegate = self



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

}
biggreentree
  • 1,633
  • 3
  • 20
  • 35

2 Answers2

0

You can't override MFMessageComposeViewController for anything other than sending messages between iMessage users. You'll have to build your own messaging UI (or use an existing open-source library) to send 'texts' between Parse users.

This tutorial explains in-depth how to create a messaging app and send and show messages between Parse users. This Parse tutorial explains how to use and setup push notifications.

The basic flow:

  1. User1 creates a "Message" object linked to User2
  2. User2 receives push notification with "Message" content or polls Parse for new messages
  3. User2 opens message history with User1 and views message
thattyson
  • 718
  • 8
  • 17
  • the tutorial could be helpful and I'll try it, but I'm not that good at objective-c. ;( – biggreentree Jul 12 '15 at 15:23
  • what do you think if after the login, I present a second "would you like to use message service?" view where you put number, then when you want to use the service, I put an array of parse filtered phone numbers in "messageVC.recipients"? do you think this could work? may I use only iMessage users? (I mean, sending via email instead of numbers?) – biggreentree Jul 12 '15 at 17:33
0

According to this thread from Parse's question archive, this is possible.

You can use Mailgun or Twilio to generate these emails and text messages from Cloud Code.

This Parse blog post gives an example using Twilio, but it seems like you'll need a phone number to identify the receiving user.

Parse.Cloud.httpRequest({
    method: "POST",
    url: "https://<account_sid>:<auth_token>@api.twilio.com/2010-04-01/Accounts/<account_sid>/SMS/Messages.json",
    body: {
        From:"+14085550693",
        To: "+14085551212",
        Body:"Hi, Parse can send SMS via Twilio!"
    }
});

Since you'd rather not use phone numbers, I've included an example from Mailgun's documentation that shows how to send a message with their API.

curl -s --user 'api:YOUR_API_KEY' \
https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/messages \
-F from='Excited User <mailgun@YOUR_DOMAIN_NAME>' \
-F to=YOU@YOUR_DOMAIN_NAME \
-F to=bar@example.com \
-F subject='Hello' \
-F text='Testing some Mailgun awesomness!'
ndmeiri
  • 4,979
  • 12
  • 37
  • 45
  • in that case, it could be a problem, I should ask users to give me their number during registration on parse wichever they choose between Facebook or email login – biggreentree Jul 12 '15 at 15:21
  • @biggreentree Yes. Unfortunately, according to this Stack Overflow post, programmatically obtaining the user's phone number is not possible: http://stackoverflow.com/questions/193182/programmatically-get-own-phone-number-in-ios – ndmeiri Jul 12 '15 at 15:24
  • @biggreentree You'll also need a way for the user to input the recipients' phone numbers. – ndmeiri Jul 12 '15 at 15:25
  • I see, what do you think if after the login, I present a second "would you like to use message service?" view where you put number, then when you want to use the service, I put an array of parse filtered phone numbers in "messageVC.recipients"? do you think this could work? may I use only iMessage users? (I mean, sending via email instead of numbers?) – biggreentree Jul 12 '15 at 17:33
  • Yes, that could work. You could also use the Address Book framework and the Address Book UI framework to present contacts to the user, so they can select multiple contacts as recipients. You should present the address book UI, at some point during the message composition flow. – ndmeiri Jul 12 '15 at 17:42
  • good idea! even if the point is socialize with new people around you more than your address book, but now I'm scared about privacy, is there a func to hide the sender number? – biggreentree Jul 12 '15 at 17:56
  • I don't believe there is a way to hide the sender's number when you send SMS. If you want to socialize with people around you, you could use Multipeer Connectivity. See PeerKit, an open-source library that could help you build a Multipeer Connectivity app, at https://github.com/jpsim/PeerKit. (At the time of writing, I have not contributed to PeerKit.) – ndmeiri Jul 12 '15 at 18:08