打電話、發短信

   We all know that phone and message are the basic function of the cellphone , iOS SDK provide the interface, let us call . Now let's look how to call the interface in the program. 

   1. ring up

        [UIApplication sharedApplication]openURL:[NSURL URLWithString:@"tel://10010"] ; //ring up

     After using the  API of openURL ,returned is the interface of making calls. If so ,then how did we  go back to our program? There are two  methods that we share with you. 

    The first method is uploading phone by UIWebView ,the code as follows:

     UIWebView *callWebView=[UIWebView alloc]init];

     NSURL *telURL=[NSURL URLWithString:@"tel:10010"];

     [callWebView loadRequest:[NSURLRequest requestWithURL:telURL]];

     [self.view addSubView:callWebView];

   

    The second method is private ,the code as follows:

    [UIApplication sharedApplication]openURL:[NSURL URLWithString:@"tel://10100"];


  2. send message

   There are two ways to send messages in iOS program , the simplest is using openURL:

   [UIApplication sharedApplication]openURL:[NSURL URLWithString:@"sms://10010"] ; //send message

     The method above can't specify the content of the message ,such as "hello ,my daring ,Mr Hu", iOS4.0 newly add MFMessageComposeViewController and MFMessageComposeViewControllerDelegate ,which provide the API of send Messages  and can send messages like send e-mails ,rather than  stand back from a program . The introduction in details ,about which you can know from Message UiFramework Reference .

                            Determining if Message Composition Is Available

                                  + canSendText

                             Accessing the Delegate

                                   messageComposeDelegate property

                              Setting the initial Message Information

                                      recipients    property (the receivers)

                                      body property      (the content)      

         MFMessageComposeViewController provides operation interface ,which before using you must check the method of CanSendText , if return NO ,you mustn't expro the controller ,but giving a tint telling the user the device can't support the function of sending message.

           messageComposeDelegate : agency , dealing with the result of sending messages

           recipients: receivers <list ,supporting  mass texting>

           body : the message content

      Importing MessageUI.framework in Framework.

      #import<MessageUI/MessageUI.h>

     adding protocol : <MFMessageComposeViewControllerDelegate>

- (IBAction)sendMessageAutoBack:(id)sender {

    if ([MFMessageComposeViewController canSendText]) {

        messageVC=[[MFMessageComposeViewController alloc]init];

        messageVC.recipients=[NSArray arrayWithObject:@"sms://15806138106"];

        messageVC.body=@"阿邱你好帥";

        messageVC.messageComposeDelegate=self;

        [self presentViewController:messageVC animated:YES completion:nil];

        

        [[[[messageVC viewControllers]lastObject]navigationItem]setTitle:@"測試短信"];

     }

    else {

         [self alertWithTitle:@"提示信息" msg:@"設備沒有短信功能"];

        

    }

}

-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{

    [messageVC dismissViewControllerAnimated:NO completion:nil];

    switch (result) {

        case MessageComposeResultCancelled:

            [self alertWithTitle:@"提示信息" msg:@"發送取消"];

                       break;

        case MessageComposeResultSent:

             [self alertWithTitle:@"提示信息" msg:@"發送成功"];

            break;

        case MessageComposeResultFailed:

             [self alertWithTitle:@"提示信息" msg:@"發送失敗"];

            break;

            

        default:

            break;

    }

}

-(void)alertWithTitle:(NSString *)title msg:(NSString *)msg{

    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:title message:msg delegate:self cancelButtonTitle:@"確定" otherButtonTitles:nil];

    [alert show];


}






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章