-1

I want to set MFMessageComposeViewControllerTextMessageAvailabilityDidChange notification to observer. I am new to swift so, i am not clear about how to do that. Please help me.

Shariif Islam
  • 259
  • 1
  • 2
  • 15

2 Answers2

1
NotificationCenter.default.addObserver(self, selector: #selector(YourClass.yourFunction(notification:_)), name: Notification.Name.MFMessageComposeViewControllerTextMessageAvailabilityDidChange, object: nil)

Function:

func yourFunction(notification: Notification) { }

I have not tested yet! But it's should work. Let me know if it's works

Discussion

Upon receiving this notification, query its userInfo dictionary with the MFMessageComposeViewControllerTextMessageAvailabilityKey key. If the availability of text message sending has changed, your app should invalidate caches and update its user interface as appropriate.

From official documentation

Mannopson
  • 2,634
  • 1
  • 16
  • 32
  • No, its not working. Not getting notification when phone is on airplane mode or when Message app failed to send SMS. – Shariif Islam Feb 05 '17 at 07:07
  • I add this observer to 'viewWillAppear' - NotificationCenter.default.addObserver(self, selector: #selector(changeByNotification(notification:)), name: Notification.Name.MFMessageComposeViewControllerTextMessageAvailabilityDidChange, object: nil) and this is method declaration - func changeByNotification(notification: Notification) { print("func called ....") } – Shariif Islam Feb 05 '17 at 07:23
  • What did you get on the console? – Mannopson Feb 05 '17 at 07:26
  • Ok! Another way: Remove the registered observer from the viewWillAppear and put it inside of MessageComposeResult's .failed case. – Mannopson Feb 05 '17 at 07:35
  • No, failed case not get called. Its call .sent status and after called .sent case Message app failed to send SMS. – Shariif Islam Feb 05 '17 at 08:28
0

Swift 3.0

 let notificationCenter = NotificationCenter.default // Note that default is now a property, not a method call
 notificationCenter.addObserver(forName: Notification.Name(rawValue: MFMessageComposeViewControllerTextMessageAvailabilityDidChange),object: nil, queue: nil,using: catchNotification)

 //handle the notification fired method
 func catchNotification(notification: Notification) -> Void {

 }
iAviator
  • 1,310
  • 13
  • 31
  • where i add MFMessageComposeViewControllerTextMessageAvailabilityDidChange notification ? – Shariif Islam Feb 05 '17 at 06:10
  • add it in viewDidLoad method – iAviator Feb 05 '17 at 06:11
  • check this http://stackoverflow.com/questions/10631375/whats-the-better-way-to-addobserver-removeobserver-with-nsnotificationcenter for better understanding of calling the addObserver. It depends on your requirement. – iAviator Feb 05 '17 at 06:28
  • @ShariifIslam does the above helps? – iAviator Feb 05 '17 at 07:16
  • Little bit, actually i add observer successfully but **MFMessageComposeViewControllerTextMessageAvailabilityDidChange** not working properly. – Shariif Islam Feb 05 '17 at 07:30
  • can you explain your issue? – iAviator Feb 05 '17 at 10:55
  • Also make sure you are sending the sms on device. – iAviator Feb 05 '17 at 10:56
  • The original question posted is resolved. Now you have a different issue. Kindly upvote the answer if it helped and ask a different question for your current issue. – iAviator Feb 05 '17 at 11:25
  • Are you doing this:- Upon receiving this notification, query its userInfo dictionary with the MFMessageComposeViewControllerTextMessageAvailabilityKey key. If the availability of text message sending has changed, your app should invalidate caches and update its user interface as appropriate. – iAviator Feb 05 '17 at 11:31
  • Yes, i am trying to get notification if SMS failed. But this notification only fire if device is not capable to sending SMS or device is not configured for send SMS. So, if SMS failed due to insufficient balance then its not called. – Shariif Islam Feb 05 '17 at 11:45