多种方法实现打电话、打开网址、发邮件、发短信、应用评分

    xiaoxiao2025-10-23  1

    OS 中的很多小功能都是非常简单的,几行代码就搞定了,比如打电话、打开网址、发邮件、发短信等。 一、打电话     方法一: 最简单最直接的方式 直接跳到拨号界面

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

    [[UIApplication sharedApplication] openURL:url];

    缺点:电话打完后,不会自动回到原应用,直接停留在通话记录界面    方法二: 拨号之前会弹框询问用户是否拨号,拨完后能自动回到 应用

    NSURL *url= [NSURL URLWithString:@"telprompt://10010"];

    [[UIApplication sharedApplication] openURL:url];

      缺点: 因为是私有 API ,所以可能不会被审核通过    方法三: 创建一个 UIWebView 来加载 URL ,拨完后能自动回到 应用

    if (_webView == nil) {

       _webView = [[UIWebView alloc] initWithFrame:CGRectZero];

    }

    [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"tel://10010"]]];

    需要注意的是:这个webView千万不要添加到界面上来,不然会挡住其他界面

    二、发短信     方法一: n直接跳到发短信界面,但是不能指定短信内容,而且不能自动回到原应用

    NSURL *url= [NSURL URLWithString:@"sms://10010"];

    [[UIApplication sharedApplication] openURL:url];

                  

    方法二:如果想指定短信内容,那就得使用MessageUI框架,MessageUI.framework

    包含主头文件

    #import<MessageUI/MessageUI.h>

    显示发短信的控制器

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

    // 设置短信内容

    vc.body = @"吃饭了没?";

    // 设置收件人列表

    vc.recipients = @[@"10010", @"02010010"];

    // 设置代理

    vc.messageComposeDelegate = self;

    // 显示控制器

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

    代理方法,当短信界面关闭的时候调用,发完后会自动回到原应用

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

    {

       //关闭短信界面

       [controller dismissViewControllerAnimated:YES completion:nil];

       

       if(result == MessageComposeResultCancelled) {

            NSLog(@"取消发送");

       } else if(result == MessageComposeResultSent) {

            NSLog(@"已经发出");

       } else {

            NSLog(@"发送失败");

       }

    }

    三、发邮件

        方法一:用自带的邮件客户端,发完邮件后不会自动回到原应用

    NSURL *url= [NSURL URLWithString:@"mailto://10010@qq.com"];

    [[UIApplication sharedApplication] openURL:url];

      方法二:跟发短信的第2种方法差不多,只不过控制器类名叫做:MFMailComposeViewController

    假设发送的邮件内容如下图所示,代码实现看备注

    // 不能发邮件

    if(![MFMailComposeViewController canSendMail]) return;

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

    // 设置邮件主题

    [vcsetSubject:@"会议"];

    // 设置邮件内容

    [vcsetMessageBody:@"今天下午开会吧"isHTML:NO];

    // 设置收件人列表

    [vcsetToRecipients:@[@"643055866@qq.com"]];

    // 设置抄送人列表

    [vcsetCcRecipients:@[@"1234@qq.com"]];

    // 设置密送人列表

    [vcsetBccRecipients:@[@"56789@qq.com"]];

    // 添加附件(一张图片)

    UIImage*image = [UIImage imageNamed:@"lufy.jpeg"];

    NSData*data = UIImageJPEGRepresentation(image, 0.5);

    [vcaddAttachmentData:data mimeType:@"image/jepg"fileName:@"lufy.jpeg"];

    // 设置代理

    vc.mailComposeDelegate = self;

    // 显示控制器

    [selfpresentViewController:vc animated:YES completion:nil];

    邮件发送后的代理方法回调 发完后会自动回到原应用

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

    {

       //关闭邮件界面

       [controller dismissViewControllerAnimated:YES completion:nil];

       

       if(result == MFMailComposeResultCancelled) {

            NSLog(@"取消发送");

       } else if(result == MFMailComposeResultSent) {

            NSLog(@"已经发出");

       } else {

            NSLog(@"发送失败");

       }

    }

    四、应用评分

    为了提高应用的用户体验,经常需要邀请用户对应用进行评分 应用评分无非就是跳转到 AppStore 展示自己的应用,然后由用户自己撰写评论 如何跳转到 AppStore ,并且展示自己的应用 Ø 方法 1

    NSString *appid= @"444934666";

    NSString *str= [NSString stringWithFormat:

                     @"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=%@",appid];

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];

    Ø 方法 2

    NSString *str= [NSString stringWithFormat:

                     @"itms-apps://itunes.apple.com/cn/app/id%@?mt=8",appid];

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];

    转载请注明原文地址: https://ju.6miu.com/read-1303432.html
    最新回复(0)