2016-07-24 6 views
1

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

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

import UIKit  
    import MessageUI 

    class ViewController: UIViewController, MFMessageComposeViewControllerDelegate { 

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

     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. 

    } 

} 

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

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

+0

Вы можете поместить 'печати ("Отправить нажатой кнопку")' в 'функцию sendMessage'? Если вы не видите это сообщение в консоли, функция, вероятно, не будет вызвана. – kabiroberai

+0

Я добавил эту строку до и после, если в SendMessage funtion, где должен видеть, что печать? Я не могу видеть в области отладки или на экране iphone, Теперь что мне делать? –

+0

Вы подключили свою кнопку от раскадровки до кода? – kabiroberai

ответ

1

я, наконец, сделал это, я перепутал два решения, Прежде всего, я изменил функцию 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, теперь он работает правильно.

Thx моему другу @ Kabiroberai за помощью и дать некоторое время.

0

Heres нижняя функция преобразуется в стремительной 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; 
    } 
} 
Смежные вопросы