ios真机知识点:短信,email,加速计。课程收官之作

#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>

@interface ViewController : UIViewController<UIImagePickerControllerDelegate,UINavigationControllerDelegate,MFMessageComposeViewControllerDelegate,MFMailComposeViewControllerDelegate,UIAccelerometerDelegate>//前两个是图片和照相机共用的。
@property (retain, nonatomic) IBOutlet UIImageView *showView;

#pragma mark========photoLibrary=========
//图片
- (IBAction)photoLibrary:(id)sender {
//判断相册是否可以用。
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
//创建图片选取控制器。
UIImagePickerController *imagePicker=[[UIImagePickerController alloc]init];
imagePicker.delegate=self;//给图片选取器设置代理。
imagePicker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;//设置源类型位相册。
[self presentViewController:imagePicker animated:YES completion:^{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"您好" message:@"欢迎光临" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}];//显示该视图控制器。
[imagePicker release];
}
else{
NSLog(@"当前相册无法使用。");
}



}
//当我们选中图片的时候进入的代理//其实这里图片,照相机共用一个代理
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
UIImage *image=[info objectForKey:UIImagePickerControllerOriginalImage];
self.showView.image=image;
// [self dismissModalViewControllerAnimated:YES];
// Dismiss the current modal child. Uses a vertical sheet transition if animated. This method has been replaced by dismissViewControllerAnimated:completion:
// // It will be DEPRECATED, plan accordingly.
[picker dismissViewControllerAnimated:YES completion:^{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"您好" message:@"欢迎下次再来" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}];

}

#pragma mark========camera=========
//照相机.
- (IBAction)camera:(id)sender {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
//创建图片选取控制器。
UIImagePickerController *cameraPicker=[[UIImagePickerController alloc]init];
cameraPicker.delegate=self;//给图片选取器设置代理。
cameraPicker.sourceType=UIImagePickerControllerSourceTypeCamera;//设置源类型位相册。
[self presentViewController:cameraPicker animated:YES completion:^{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"您好" message:@"欢迎光临" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}];//显示该视图控制器。
[cameraPicker release];
}
else{
NSLog(@"当前相册无法使用。");
}


}
#pragma mark========accelerometer=========
//加速计、
- (IBAction)accelerometer:(id)sender {//不用导入新的框架,内置了一个功能简单的。
//创建加速计对象。
UIAccelerometer *acceler=[UIAccelerometer sharedAccelerometer];
//设置传感器的更新数据的时间。
[acceler setUpdateInterval:0.1];//不太精准、
//设置代理。
acceler.delegate=self;



}
-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration{
NSLog(@"acceleration.x===%f,,,acceleration.y===%f,,,acceleration.z===%f,,",acceleration.x,acceleration.y,acceleration.z );

}
#pragma mark========outSMS=========
//外部短信:就是接短信时候推出程序。
- (IBAction)outSMS:(id)sender {
NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"sms:13269991562"]];//这里是指定的电话号码,用的时候可以用%@和传来的形参代替。
if ([[UIApplication sharedApplication]canOpenURL:url]) {
[[UIApplication sharedApplication]openURL:url];
}
else{
NSLog(@"设备不支持短信。");
}


}
#pragma mark========inSMS=========
- (IBAction)inSMS:(id)sender {
if ([MFMessageComposeViewController canSendText]) {//是否可以发短信。
//创建短信发送控制器。
MFMessageComposeViewController *compose=[[MFMessageComposeViewController alloc]init];
compose.messageComposeDelegate=self;//消息写代理。
//设置消息内容。
compose.body=@"设置消息内容";
//设置电话。
compose.recipients=[NSArray arrayWithObjects:@"13269991562", nil];
//打开发送界面。
[self presentModalViewController:compose animated:YES];
[compose release];
}
else{
NSLog(@"不支持外部短信功能");
}

}
//短信发送结束的代理
-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{
if (result==MessageComposeResultSent) {
NSLog(@"消息发送成功。");
}
else if(result==MessageComposeResultFailed){
NSLog(@"消息发送失败");
}
[self dismissModalViewControllerAnimated:YES];
}

#pragma mark========outEmail=========
- (IBAction)outEmail:(id)sender {
NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"mailto:%@?cc=%@&subject=%@&body=%@",@"fujiekai@gmail.com",@"1049055935@qq.com",@"test",@"ceshi"]];//mailto协议,cc后面是抄送。他们都是收件人
if ([[UIApplication sharedApplication]canOpenURL:url]) {
[[UIApplication sharedApplication]openURL:url];
}
else{
NSLog(@"设备不支持短信。");
}


}
#pragma mark========inEmail=========
- (IBAction)inEmail:(id)sender {
if ([MFMailComposeViewController canSendMail]) {//是否可以发邮件。
//创建邮件发送控制器。
MFMailComposeViewController *compose=[[MFMailComposeViewController alloc]init];
compose.mailComposeDelegate=self;//消息写代理。
//设置邮件标题。
[compose setSubject:@"test"];
//设置邮件内容。
[compose setMessageBody:@"hello" isHTML:YES];
//设置邮件发送的对象。
[compose setToRecipients:[NSArray arrayWithObjects:@"1049055935@qq.com", nil]];
//设置邮件抄送对象。
[compose setCcRecipients:[NSArray arrayWithObjects:@"2698580804@qq.com", nil]];
//显示邮件视图控制器。
[self presentModalViewController:compose animated:YES];
[compose release];
}
else{
NSLog(@"不支持外部短信功能");
}
}
//邮件结束时的代理。
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
if (result==MFMailComposeResultSent) {
NSLog(@"成功了");
}
else if(result==MFMailComposeResultFailed){
NSLog(@"失败了");
}
//收起视图控制器。
[controller dismissModalViewControllerAnimated:YES];
}


#if 0
http://www.cocoachina.com/iphonedev/sdk/2010/0811/1996.html
Core Motion的大体介绍就是这些。下面说说Core Motion具体负责的采集,计算和处理。Core Motion的使用就是一三部曲:初始化,获取数据,处理后事。

在初始化阶段,不管你要获取的是什么数据,首先需要做的就是

motionManager = [[CMMotionManager alloc] init];

所有的操作都会由这个manager接管。后面的初始化操作相当直观,以加速度的pull方式为例

if (!motionManager.accelerometerAvailable) {
// fail code // 检查传感器到底在设备上是否可用
}
motionManager.accelerometerUpdateInterval = 0.01; // 告诉manager,更新频率是100Hz
[motionManager startAccelerometerUpdates]; // 开始更新,后台线程开始运行。这是pull方式。

如果是push方式,更新的代码可以写成这样

[motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:^(CMAccelerometerData *latestAcc, NSError *error)
{
// Your code here
}];

接下来就是获取数据了。Again,很简单的代码

CMAccelerometerData *newestAccel = motionManager.accelerometerData;
filteredAcceleration[0] = newestAccel.acceleration.x;
filteredAcceleration[1] = newestAccel.acceleration.y;
filteredAcceleration[2] = newestAccel.acceleration.z;

通过定义的CMAccelerometerData变量,获取CMAcceleration信息。和以前的UIAccelerometer类的使用方式一样,CMAcceleration在Core Motion中是以结构体形式定义的

typedef struct {
double x;
double y;
double z;
}

对应的motion信息,比如加速度或者旋转速度,就可以直接从这三个成员变量中得到。

最后是处理后事,就是在你不需要Core Motion进行处理的时候,释放资源

[motionManager stopAccelerometerUpdates];
//[motionManager stopGyroUpdates];
//[motionManager stopDeviceMotionUpdates];
[motionManager release];

你看,就是这么简单。当然,如果这么Core Motion这么简单,就太无趣了。实际上,Core Motion最好玩的地方,既不是加速度,也不是角速度,而是经过sensor fusing算法处理的Device Motion信息的提供。Core Motion里面提供了一个叫做CMDeviceMotion的类,用来把下图所示的这些数据封装成Device Motion信息:


#endif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值