Implementation of SMS sending code for IOS apps

Source: Internet
Author: User

IOS ProgramOfSend SMSCode implementation is the content to be introduced in this article. iOS4.0 has added MFMessageComposeViewController and MFMessageComposeViewControllerDelegate to provideSend SMSYou can send emails without jumping out of the program.Send SMSFor more information, see Message UI Framework Reference.

Notes:

MFMessageComposeViewController

1) provides an operation interface

2) Check the canSendText method before use. If NO is returned, the controller should not be displayed, but the system should prompt that the user does not support sending text messages.

3) the interface cannot be customized.

4) the body and the recipient (recipients) of the text message to be sent must be initialized before the controller is displayed. After the display, the text message content cannot be modified through a program. however, you can manually modify the text message content and select recipients.

5) when the user clicks "send" or "cancel", or fails to send the message, the MFMessageComposeViewControllerDelegate

 
 
  1. – messageComposeViewController:didFinishWithResult: 

Methods can be notified, and corresponding processing is performed here

If it runs on iOS3.0, a prompt is displayed.

 
 
  1. dyld: Symbol not found: _OBJC_CLASS_$_MFMessageComposeViewController . 

Solution:

1) Select weak for the introduced Type of MessageUI. framework (modify it in target-> Get Info-> General-> Linked Libraries-> MessageUI. framework-> Type)

2) do not directly import MessageUI/MFMessageComposeViewController. h In the. h file, and change it to import <MessageUI/MessageUI. h>

Code:

 
 
  1. # Pragma mark-
  2. # Pragma mark SMS
  3. -(IBAction) showSMSPicker :( id) sender {
  4. // The MFMessageComposeViewController class is only available in iPhone OS 4.0 or later.
  5. // So, we must verify the existence of the above class and log an error message for devices
  6. // Running earlier versions of the iPhone OS. Set feedbackMsg if device doesn' t support
  7. // MFMessageComposeViewController API.
  8. Class messageClass = (NSClassFromString (@ "MFMessageComposeViewController "));
  9. If (messageClass! = Nil ){
  10. // Check whether the current device is configured for sending SMS messages
  11. If ([messageClass canSendText]) {
  12. [Self displaySMSComposerSheet];
  13. }
  14. Else {
  15. [UIAlertView quickAlertWithTitle: @ "the device does not have the SMS function" messageTitle: nil dismissTitle: @ "close"];
  16. }
  17. }
  18. Else {
  19. [UIAlertView quickAlertWithTitle: @ "iOS version is too low. If iOS4.0 or later, you can send a text message" messageTitle: nil dismissTitle: @ "close"];
  20. }
  21. }
  22. -(Void) displaySMSComposerSheet
  23. {
  24. MFMessageComposeViewController * picker = [[MFMessageComposeViewController alloc] init];
  25. Picker. messageComposeDelegate = self;
  26. NSMutableString * absUrl = [[NSMutableString alloc] initWithString: web. request. URL. absoluteString];
  27. [AbsUrl replaceOccurrencesOfString: @ "http:// I .aizheke.com" withString: @ "http://m.aizheke.com"
  28. Options: NSCaseInsensitiveSearch range: NSMakeRange (0, [absUrl length])];
  29. Picker. body = [NSString stringWithFormat: @ "I have seen on love Guest: % @ may be useful to you. We recommend it to you! Link: % @"
  30. , [Web stringByEvaluatingJavaScriptFromString: @ "document. title"]
  31. , AbsUrl];
  32. [AbsUrl release];
  33. [Self presentModalViewController: picker animated: YES];
  34. [Picker release];
  35. }
  36. -(Void) messageComposeViewController :( MFMessageComposeViewController *) controller
  37. DidFinishWithResult :( MessageComposeResult) result {
  38. Switch (result)
  39. {
  40. Case MessageComposeResultCancelled:
  41. LOG_EXPR (@ "Result: SMS sending canceled ");
  42. Break;
  43. Case MessageComposeResultSent:
  44. LOG_EXPR (@ "Result: SMS sent ");
  45. Break;
  46. Case MessageComposeResultFailed:
  47. [UIAlertView quickAlertWithTitle: @ "failed to send SMS" messageTitle: nil dismissTitle: @ "disabled"];
  48. Break;
  49. Default:
  50. LOG_EXPR (@ "Result: SMS not sent ");
  51. Break;
  52. }
  53. [Self dismissModalViewControllerAnimated: YES];
  54. }

Summary:IOS ProgramOfSend SMSThe content of code implementation is complete. I hope this article will help you!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.