Отправить смс в iOS

Я новенький в iOS развивается и xCode, Ранее я разработал Android, и я знаю, что я должен работать с Intent, чтобы позвонить и отправить SMS в приложение для Android. Теперь я хочу разработать простое приложение, которое, когда я нажимаю Button это отправить SMS на конкретный номер. Итак, я установил Mac OS 10.11VM на моем Ubuntu, А мог подключить подключи iPhone 5 к тому и беги мой простой Hello World Приложение в реале iPhone Устройство а не simulator, Теперь я создал Button в StoryBoard и сделайте функцию, следуя этой статье: Отправить SMS-сообщение Toturial

Также посмотрите на другие ссылки и было похоже. Вот мой простой ViewController.swift,

import UIKit
import MessageUI

class ViewController: UIViewController, MFMessageComposeViewControllerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    @IBAction func sendMessage(sender: AnyObject) {

        print("Send button pressed") //display a text to make sure is calling

        if MFMessageComposeViewController.canSendText() {
            let controller = MFMessageComposeViewController()
            controller.body = "TestMessage "
            controller.recipients = ["xxxxxxxxx", "xxxxxxxxx"] // sending for two numbers
            controller.messageComposeDelegate = self
            self.presentViewController(controller, animated: true, completion: nil)
        }
    }
    func messageComposeViewController(controller: MFMessageComposeViewController, didFinishWithResult result: MessageComposeResult) {

        switch (result.rawValue) {
        case MessageComposeResultCancelled.rawValue:
            print("Message was cancelled")
            self.dismissViewControllerAnimated(true, completion: nil)

        case MessageComposeResultFailed.rawValue:
            print("Message failed")
            self.dismissViewControllerAnimated(true, completion: nil)

        case MessageComposeResultSent.rawValue:
            print("Message was sent")
            self.dismissViewControllerAnimated(true, completion: nil)

        default:
            break;
        }
    }

}

А также создал Simple Кнопка и ссылка, чтобы sendMessage выше. Когда я запускаю приложение, отображает кнопку и когда я нажимаю ее, кажется, что она что-то делает, но SMS не отправляется. Любая идея?? Я очень ценю это.

Обновление: как сказал наш друг, я добавил print("Send button pressed") в sendMessage Buttonтак я узнаю кнопку вызова sendMessage когда я нажимаю это.

2 ответа

Решение

Я наконец сделал это, я перепутал два решения, во-первых, я изменил функцию sendMessage и переименовал его в sendText, в этом случае:

    @IBAction func sendText(sender: AnyObject) {
    if (MFMessageComposeViewController.canSendText()) {
        let controller = MFMessageComposeViewController()
        controller.body = "Text Body"
        controller.recipients = ["xxxxxxxxxxx"]
        controller.messageComposeDelegate = self
        self.presentViewController(controller, animated: true, completion: nil)
    }
}

И протокол MessageComposeViewController является:

func messageComposeViewController(controller: MFMessageComposeViewController, didFinishWithResult result: MessageComposeResult) {
    switch (result.rawValue) {
    case MessageComposeResultCancelled.rawValue:
        print("Message was cancelled")
        self.dismissViewControllerAnimated(true, completion: nil)
    case MessageComposeResultFailed.rawValue:
        print("Message failed")
        self.dismissViewControllerAnimated(true, completion: nil)
    case MessageComposeResultSent.rawValue:
        print("Message was sent")
        self.dismissViewControllerAnimated(true, completion: nil)
    default:
        break;
    }
}

А затем нажмите на кнопку в раскадровке и нажмите control и перетащите кнопку в ViewController и выберите sendText функция, теперь она работает правильно.

Спасибо моему другу @Kabiroberai за помощь и дать немного времени.

Вот нижняя функция, преобразованная в swift 4:

func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
    switch (result.rawValue) {
    case MessageComposeResult.cancelled.rawValue:
        print("Message was cancelled")
        self.dismiss(animated: true, completion: nil)
    case MessageComposeResult.failed.rawValue:
        print("Message failed")
        self.dismiss(animated: true, completion: nil)
    case MessageComposeResult.sent.rawValue:
        print("Message was sent")
        self.dismiss(animated: true, completion: nil)
    default:
        break;
    }
}
Другие вопросы по тегам