iOS中摇一摇 发短信 打电话 发邮件

本文介绍如何在iOS应用中实现摇一摇功能,并演示了如何使用系统自带的方法发送短信、拨打电话及电子邮件。包括在viewDidLoad中启用摇一摇、定义摇动事件响应方法及实现邮件发送界面。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 ios中的摇一摇功能是系统自己 有的方法 只需要调用就行

首先在view  didload 中写入

//应用支持晃动

  [[UIApplicationsharedApplication] setApplicationSupportsShakeToEdit:YES];

//成为第一响应者

    [selfbecomeFirstResponder];

然后下面执行方法:

#pragma mark   - 系统实现摇一摇功能 -

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{

    //监测到摇动

    NSLog(@" i can shake");

}

- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event{

    // 摇动被取消

    NSLog(@" i am  canncelled");

}

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{

    //摇动结束

    if(event.subtype == UIEventSubtypeMotionShake){

        NSLog(@"end  of  shake");


    }

   }

//实现简单的发短信打电话的功能

发短信功能:

在view didload 中写入

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms://10086"]];//发短信

会自动跳到发短信页面

打电话功能:

在view didload中写入  [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://10086"]];//打电话  

//发邮件的功能

首先引入框架MessageUI.framework

然后再工程中引入 

#import <MessageUI/MessageUI.h>

然后在.m文件中写入代理

@interface ViewController ()<MFMailComposeViewControllerDelegate>

然后再viewDidLoad执行

[self createMessageViewController];


- (void)createMessageViewController{

    //初始化一个视图类

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

    //设置代理

    mail.mailComposeDelegate = self;

    //设置邮件的主题

    [mail setSubject:@"这是一个邮件"];

    //设置邮件的内容一种是纯文本一种是html

    [mail setMessageBody:@"This is an E-mail" isHTML:NO];

    if([MFMailComposeViewController canSendMail]){

        [self presentViewController:mail animated:YES completion:^{

        }];

    }

    

}

#pragma mark   - 代理方法 -

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

    switch (result)

    {

        case MFMailComposeResultCancelled:

            NSLog(@"取消发送mail");

            break;

        case MFMailComposeResultSaved:

            NSLog(@"保存邮件");

            break;

        case MFMailComposeResultSent:

            NSLog(@"发送邮件");

            break;

        case MFMailComposeResultFailed:

            NSLog(@"邮件发送失败: %@...", [error localizedDescription]);

            break;

        default:

            break;

    }

    [self dismissViewControllerAnimated:YES completion:nil];

}








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值