"Learning the path of iOS: UI series" To make calls, send text messages, send email functions

Source: Internet
Author: User
Tags mail account

① Texting

1. Create a button to add a button response event

  UIButton *callbutton = [UIButton buttonwithtype:uibuttontypecustom];  Callbutton.frame = CGRectMake (on the other);  Callbutton.backgroundcolor = [Uicolor cyancolor];  [Callbutton settitle:@ "SMS" forstate:uicontrolstatenormal];  [Callbutton addtarget:self Action: @selector (callbuttonaction:)                                        Forcontrolevents:uicontroleventtouchupinside] ;  [Self.view Addsubview:callbutton];
2. Send SMS operation, set agent Mfmessagecomposeviewcontrollerdelegate

-(void) Callbuttonaction: (UIButton *) msg2{    //Determine if the user device can send SMS    if (![ Mfmessagecomposeviewcontroller Cansendtext]) {        return;    }        1. Instantiate a controller    mfmessagecomposeviewcontroller *controller =                                     [[Mfmessagecomposeviewcontroller alloc] init];    2. Set SMS Content    //1) recipient    controller.recipients = @[@ "10010", @ "10086"];        2) SMS content    controller.body = @ "Happy!" ";        3) Set proxy    controller.messagecomposedelegate = self;        3. Display SMS Controller    [self presentviewcontroller:controller animated:yes completion:nil];}
3. Implementing Proxy Methods

/** SMS Send results messagecomposeresultcancelled,     cancel Messagecomposeresultsent,          send messagecomposeresultfailed         Failure */-(void) Messagecomposeviewcontroller: (Mfmessagecomposeviewcontroller *) controller                                    Didfinishwithresult: ( Messagecomposeresult) result{    NSLog (@ "%d", result);        In object-oriented programming development, there is a principle, who applies, who releases!    //* * * This method also works because the system sends a shutdown message to self    //    [controller Dismissviewcontrolleranimated:yes Completion:nil];        You should use this!    [self Dismissviewcontrolleranimated:yes completion:nil];}
② e-mail

1. Create button (above), Set button trigger event, Emailbuttonaction: Implementation method

<span style= "color: #000000;" >-(void) Emailbuttonaction: (UIButton *) SendMail {//1. First determine if the message can be sent if (![ Mfmailcomposeviewcontroller Cansendmail]) {//Prompt user to set mailbox Uialertview *alert = [[Uialertview alloc] In                                        Itwithtitle:nil   message:@ "user does not have a mail account set up" Delegate:self Cancelbuttontitle:nil         otherbuttontitles:@ "OK", nil];        [Alert show];         [Alert release];    Return }//2.        Instantiate the mail controller, ready to send mail Mfmailcomposeviewcontroller *controller =[[mfmailcomposeviewcontroller alloc] init];    1) Topic XXX Work Report [Controller setsubject:@ "project reply"];        2) Recipient [Controller settorecipients:@[@ "[email protected]"];        5) The body [controller setmessagebody:@] is the justice "Ishtml:yes";    6) Accessories UIImage *image = [UIImage imagenamed:@ "Avatar 1.png"];     NSData *imagedata = uiimagepngrepresentation (image); 1> binary data for attachments//3> The name of the file that the recipient sees when receiving the//can add multiple attachments [controller AddattAchmentdata:imagedata mimetype:@ "Image/png" filename:@ "Avatar. png"];        7) Set agent [controller setmailcomposedelegate:self]; Display controller [self Presentviewcontroller:controller animated:yes completion:nil];} </span>
2. Implementing the Proxy Method (Mfmailcomposeviewcontrollerdelegate)

-(void) Mailcomposecontroller: (Mfmailcomposeviewcontroller *) controller                Didfinishwithresult: ( Mfmailcomposeresult) Result Error: (Nserror *) error{    //Prompt user NSLog according to different status    (@ "%d", result);        [Self Dismissviewcontrolleranimated:yes completion:nil];}
③ Call

1. Method 1

-(void) tel2{    //However: the Telprompt protocol belongs to Apple's private agreement, and once this protocol is used in the program, the program cannot be on the shelves    /system developed for jailbroken machines, you can use this protocol    nsurl *url = [Nsurl urlwithstring:@ "telprompt://10010"];        [[UIApplication sharedapplication] openurl:url];}
2. Method 2 (recommended)

@property (nonatomic, retain) UIWebView *webview;
Implementing Response Events

-(void) Phonebuttonaction: (UIButton *) tel3{    //Hint: Do not add webview to Self.view if added will obscure the original view    //Lazy Load If    (_ WebView = = nil) {        _webview = [[UIWebView alloc] init];    }    NSLog (@ "%p", _webview);    _webview = [[UIWebView alloc] initWithFrame:self.view.bounds];    [Self.view Addsubview:_webview];        Nsurl *url = [Nsurl urlwithstring:@ "tel://10010"];    Nsurlrequest *request = [Nsurlrequest requestwithurl:url];    UIWebView *view = [[UIWebView]    [_webview loadrequest:request];    }
3. This method, call the end of the phone back to the original application, will stay in the address book, and is directly dialed, do not pop-up hints.
Nsmutablestring * str=[[nsmutablestring alloc] initwithformat:@ "tel:%@",                       @ "186xxxx6979"];[ [UIApplication sharedapplication] Openurl:[nsurl Urlwithstring:str]];






"Learning the path of iOS: UI series" To make calls, send text messages, send email functions

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.