iOS------打電話 ,發信息,發郵件方法

轉自:http://blog.csdn.net/cym_bj/article/details/18910771  感謝分享!

•如果想打開一些常見文件,比如html、txt、PDF、PPT等,都可以使用UIWebView打開
•只需要告訴UIWebView文件的URL即可
•至於打開一個遠程的共享資源,比如http協議的,也可以調用系統自帶的Safari瀏覽器:

NSURL *url= [NSURL URLWithString:@”http://www.baidu.com"];

[[UIApplication sharedApplicationopenURL:url];



#import "ViewController.h"

#import <MessageUI/MessageUI.h>

@interface ViewController ()<MFMessageComposeViewControllerDelegate,MFMailComposeViewControllerDelegate>


@end


@implementation ViewController


- (void)viewDidLoad

{

    [super viewDidLoad];

    

//----------------------------打電話的三種方式

    

    //方式1 通話結束時回到通訊錄裏。不會回到應用程序

    NSURL *url=[NSURL URLWithString:@"tel://10086"];

    [[UIApplication sharedApplication]openURL:url];

    

    //方式2,(彈出提示框)通話結束自動回到原來的應用程序,用的是私有api,不能上傳上蘋果商店,適合做越獄的。

    NSURL *url1=[NSURL URLWithString:@"telprompt://10086"];

    [[UIApplication sharedApplication]openURL:url1];

    

    //方式3,(彈出提示框)通話結束看自動回到原來的應用程序 這個比較合理

    

    UIWebView *web=[[UIWebView alloc]init];

    [web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"tel://10086"]]];

    

    //不需要添加到視圖上, 不然會擋住其它界面

    //[self.view addSubview:web];

    

   

    

    //----------------------------發短信的二種方式

    //方式1  直接跳到短信界面,不能指定短信內容,不能回到原應用程序

    NSURL *url2=[NSURL URLWithString:@"sms://10086"];

    [[UIApplication sharedApplication]openURL:url2];

    

    //方式2

    /* 1,需要添加 MessageUI.framework 框架

     */

    //創建短信控制器

    MFMessageComposeViewController *vc=[[MFMessageComposeViewController alloc]init];

    //設置短信內容

    vc.body=@"短信內容";

    //設置收件人

    vc.recipients=[NSArray arrayWithObjects:@"10086",@"10010"nil];

    

    //設置代理

    vc.messageComposeDelegate=self;

    

    //顯示控制器

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

    

    //----------------------------發郵件的二種方式

    //方法一  系統自帶的郵件客戶端, 不能加到原應用程序

    NSURL *url3=[NSURL URLWithString:@"mailto://[email protected]"];

    [[UIApplication sharedApplication]openURL:url3];

    

    //方法二  和發短信一樣,用的的框架

    //創建發郵件的控制器

    MFMailComposeViewController *mailvc=[[MFMailComposeViewController alloc]init];

    

    mailvc.mailComposeDelegate=self;

    //設置主題

    [mailvc setSubject:@"大家好"];

    //設置郵件內容

    [mailvc setMessageBody:@"<font color=\"red\">哈哈</fon>" isHTML:YES];

    //設置發送人

    [mailvc setToRecipients:[NSArray arrayWithObjects:@"551730360",@"12306.qq.com"nil]];

    //設置抄送

    [mailvc setCcRecipients:[NSArray arrayWithObjects:@".--.qcom"nil]];

    //設置密送

    [mailvc setBccRecipients:[NSArray arrayWithObjects:@"ddff.com"nil]];

    

    //添加附件

    NSURL *url4=[[NSBundle mainBundle]URLForResource:@"圖片名" withExtension:@"png"];

    NSData *data=[NSData dataWithContentsOfURL:url4];

    [mailvc addAttachmentData:data mimeType:@"image/png" fileName:@"圖片名.png"];

    

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

    

    

}

#pragma mark -代理方法,短信界面關閉結束的時候調用。

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

{

    /* 

      result  的三個狀態

    MessageComposeResultCancelled, 取消的   0

    MessageComposeResultSent, // 成功     1

    MessageComposeResultFailed// 失敗    2

     */

    

    //關閉短信界面

    [controller dismissViewControllerAnimated:YES completion:nil];

    

    if (result==MessageComposeResultCancelled)

    {

        NSLog(@"取消發送");

    }

    else if (result==MessageComposeResultSent)

    {

        NSLog(@"發送成功");

    }

    else if (result==MessageComposeResultFailed)

    {

        NSLog(@"發送失敗");

    }


    

}


#pragma mark - 發郵件結束時調用

-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error

{

    

    /*

     result  的四個狀態

     MFMailComposeResultCancelled,取消的   0

     MFMailComposeResultSaved,     是否存草搞  1

     MFMailComposeResultSent,      成功     2

     MFMailComposeResultFailed    失敗    3

  

           */

    

    

    if (result==MFMailComposeResultCancelled)

    {

        NSLog(@"取消發送");

    }

    else if (result==MFMailComposeResultSaved)

    {

        NSLog(@"是否存草搞");

    }

    else if (result==MFMailComposeResultSent)

    {

        NSLog(@"發送成功");

    }

    else if (result==MessageComposeResultFailed)

    {

        NSLog(@"發送失敗");

    }


    //關閉郵箱界面

    [controller dismissViewControllerAnimated:YES completion:nil];


    

}


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