打电话、发短信

来源:互联网 发布:linux 多tomcat配置 编辑:程序博客网 时间:2024/04/30 07:58

   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

                                   messageComposeDelegateproperty

                              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 ([MFMessageComposeViewControllercanSendText]) {

        messageVC=[[MFMessageComposeViewControlleralloc]init];

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

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

        messageVC.messageComposeDelegate=self;

        [selfpresentViewController:messageVCanimated:YEScompletion:nil];

        

        [[[[messageVCviewControllers]lastObject]navigationItem]setTitle:@"测试短信"];

     }

   else {

         [selfalertWithTitle:@"提示信息"msg:@"设备没有短信功能"];

        

    }

}

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

    [messageVCdismissViewControllerAnimated:NOcompletion:nil];

   switch (result) {

        caseMessageComposeResultCancelled:

            [selfalertWithTitle:@"提示信息"msg:@"发送取消"];

                      break;

        caseMessageComposeResultSent:

             [selfalertWithTitle:@"提示信息"msg:@"发送成功"];

           break;

        caseMessageComposeResultFailed:

             [selfalertWithTitle:@"提示信息"msg:@"发送失败"];

           break;

            

       default:

           break;

    }

}

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

    UIAlertView *alert=[[UIAlertViewalloc]initWithTitle:titlemessage:msg delegate:selfcancelButtonTitle:@"确定"otherButtonTitles:nil];

    [alertshow];


}






0 0
原创粉丝点击