How to integrate the text message function of iPhone in applications

Source: Internet
Author: User

Ios4.0 is added with mfmessagecomposeviewcontroller and mfmessagecomposeviewcontrollerdelegate. It provides an interface for sending text messages, so you don't need to jump out of the program to send text messages like sending emails. Cocoachina member "Tianya Haijiao" summarizes the features of mfmessagecomposeviewcontroller's integrated short message function:

  • Provides an operation interface
  • You must check the cansendtext method before using it. If no is returned, the Controller should not be displayed, but the system should prompt that the user does not support sending text messages.
  • The interface cannot be customized by yourself.
  • The content (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.
  • When the user clicks send or cancel, or fails to send, the-messagecomposeviewcontrollerdelegate-messagecomposeviewcontroller: didfinishwithresult: method can be notified, and corresponding processing is performed here
  • If running on ios3.0, the system prompts dyld: Symbol not found: _ objc_class _ $ _ mfmessagecomposeviewcontroller.
    Solution:
    Select weak for the introduced type of messageui. Framework (modify it in target-> get info-> General-> linked libraries-> messageui. Framework-> type)
    Do not directly import messageui/mfmessagecomposeviewcontroller. h In the. h file, and change it to import <messageui/messageui. h>

Main Code:

Import framework: messageui. Framework
# Import <messageui/messageui. h>
Add Protocol: <mfmessagecomposeviewcontrollerdelegate>

# Pragma mark-
# Pragma mark mfmessagecomposeviewcontroller
-(Void) alertwithtitle :( nsstring *) Title MSG :( nsstring *) MSG {
Uialertview * Alert = [[uialertviewalloc] initwithtitle: Title
Message: MSG delegate: Nil cancelbuttontitle: @ "OK" otherbuttontitles: Nil];
[Alert show];
[Alert release];
}

-(Void) displaysms :( nsstring *) Message {

Mfmessagecomposeviewcontroller * picker = [[mfmessagecomposeviewcontrolleralloc] init];
Picker. messagecomposedelegate = self;
Picker. navigationbar. tintcolor = [uicolorblackcolor];
Picker. Body = message; // default information
// Default recipient (multiple recipients allowed)
// Picker. Recipients = [nsarray arraywithobject: @ "12345678901", nil];
[Selfpresentmodalviewcontroller: picker animated: Yes];
[Picker release];
}

-(Void) sendsms :( nsstring *) Message {

Classmessageclass = (nsclassfromstring (@ "mfmessagecomposeviewcontroller "));
Nslog (@ "can send SMS [% d]", [messageclass cansendtext]);

If (messageclass! = Nil ){
If ([messageclass cansendtext]) {
[Self displaysms: Message];
} Else {
[Selfalertwithtitle: nilmsg: @ "the device does not have the SMS function"];
}
} Else {
[Selfalertwithtitle: nilmsg: @ "iOS version is too low. Only iOS or later supports sending SMS messages in the program"];
}

}

-(Void) messagecomposeviewcontroller :( mfmessagecomposeviewcontroller *) Controller
Didfinishwithresult :( messagecomposeresult) Result {
Nsstring * MSG;

Switch (result ){
Case messagecomposeresultcancelled:
MSG = @ "cancel sending ";
Break;
Case messagecomposeresultsent:
MSG = @ "sent successfully ";
[Self alertwithtitle: Nil MSG: MSG];
Break;
Case messagecomposeresultfailed:
MSG = @ "failed to send ";
[Self alertwithtitle: Nil MSG: MSG];
Break;
Default:
Break;
}

Nslog (@ "sending result: % @", MSG );

[Selfdismissmodalviewcontrolleranimated: Yes];
}

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.