您的位置:首页 > 移动开发 > IOS开发

ios硬件特性(通讯录,邮件,短信,重力感应)

2015-08-22 08:43 423 查看
1:相机&从相册中获取图片

.h记得导入<UIImagePickerControllerDelegate,UINavigationControllerDelegate,

MFMailComposeViewControllerDelegate

>

.m:

//

// ViewController.m

// LessonHardWare

//

// Created by cyy on 13-1-29.

// Copyright (c) 2013年 LanOuKeJi. All rights reserved.

//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad

{

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

}

- (void)didReceiveMemoryWarning

{

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

- (IBAction)camera:(id)sender

{

//判断是否可以打开相机,模拟器此功能无法使用

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

UIImagePickerController *picker = [[UIImagePickerController alloc] init];

picker.delegate = self;//需要接受2个协议

//picker.allowsEditing = YES; //是否可编辑

//摄像头

picker.sourceType = UIImagePickerControllerSourceTypeCamera;

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

[picker release];

}else {

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"你没有摄像头" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];

[alert show];

[alert release];

}

}

- (IBAction)library:(id)sender

{

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])

{

UIImagePickerController *picker = [[UIImagePickerController alloc] init];

picker.delegate = self;

//picker.allowsEditing = YES; //是否可编辑

//打开相册选择照片

picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

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

[picker release];

}

}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

{

//得到原始图片,如果可编辑打开,可以获得编辑图片,但是key不是这个。

UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];

imageView.image = image;

[self dismissViewControllerAnimated:YES completion:nil];

}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker

{

[self dismissViewControllerAnimated:YES completion:nil];

}

- (void)dealloc {

[imageView release];

[super dealloc];

}

@end

===============================================================

2:信息&邮件

.h导入

<MFMessageComposeViewControllerDelegate,MFMailComposeViewControllerDelegate>

.m:

//

// MessageViewController.m

// LessonHardWare

//

// Created by cyy on 13-1-29.

// Copyright (c) 2013年 LanOuKeJi. All rights reserved.

//

#import "MessageViewController.h"

@interface MessageViewController ()

@end

@implementation MessageViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

if (self) {

// Custom initialization

}

return self;

}

- (void)viewDidLoad

{

[super viewDidLoad];

// Do any additional setup after loading the view.

}

- (void)didReceiveMemoryWarning

{

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

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

{

switch (result) {

case MessageComposeResultCancelled:

//取消发短信功能

NSLog(@"取消发送");

break;

case MessageComposeResultSent:

//发送短信

NSLog(@"发送成功");

break;

case MessageComposeResultFailed:

//发送失败

NSLog(@"发送失败");

break;

default:

break;

}

[self dismissViewControllerAnimated:YES completion:nil];

}

- (IBAction)messageInApp:(id)sender

{

BOOL canSendSMS = [MFMessageComposeViewController canSendText];

if (canSendSMS)

{

//创建短信视图控制器

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

//设置代理,代理方法实现对短信发送状态的监控(成功,失败,取消)

message.messageComposeDelegate = self;

//设置短信内容

message.body = @"周日下午老地方见";

//设置发送的电话,

message.recipients = [NSArray arrayWithObject:@"15101185711"];

//打开短信功能

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

[message release];

}

}

- (IBAction)messageOutApp:(id)sender

{

NSURL *numberURL = [NSURL URLWithString:[NSString stringWithFormat:@"sms:15101185711"]];

//判断程序是否可以打开短信功能

if ([[UIApplication sharedApplication] canOpenURL:numberURL])

{

[[UIApplication sharedApplication] openURL:numberURL];

} else

{

NSLog(@"无法打开短信功能");

}

}

- (IBAction)mailInApp:(id)sender

{

BOOL canSendMail = [MFMailComposeViewController canSendMail];

if (canSendMail)

{

//创建邮件视图控制器

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

//设置主题

[mail setSubject:@"蓝鸥科技"];

//设置发送对象

[mail setToRecipients:[NSArray arrayWithObject:@"cuiyayun@lanou3g.com"]];

//设置抄送对象

[mail setCcRecipients:[NSArray arrayWithObject:@"ganjuefengzou@gmail.com"]];

//设置内容

[mail setMessageBody:@"火车票帮你买好了,记得提前去取票啊" isHTML:YES];

//设置代理

[mail setMailComposeDelegate:self];

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

[mail release];

}

}

- (IBAction)mailOutApp:(id)sender

{

NSURL *mailURL = [NSURL URLWithString:[NSString stringWithFormat:@"mailto:%@",@"ganjuesuifengzou@gmail.com"]];

//cc:抄送对象 subject:主题 body:内容

if ([[UIApplication sharedApplication] canOpenURL:mailURL])

{

[[UIApplication sharedApplication] openURL:mailURL];

} else {

NSLog(@"无法打开邮件功能");

}

}

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

{

switch (result) {

case MFMailComposeResultCancelled:

//取消发送

NSLog(@"Result: canceled");

break;

case MFMailComposeResultSent:

//发送

NSLog(@"Result: Sent");

break;

case MFMailComposeResultFailed:

//发送失败

NSLog(@"Result: Failed");

break;

case MFMailComposeResultSaved:

//保存

NSLog(@"Result: Saved");

break;

default:

break;

}

[self dismissViewControllerAnimated:YES completion:nil];

}

@end

====================================================================

3:加速计

.h导入<UIAccelerometerDelegate>

.m:

//

// AccelerometerViewController.m

// LessonHardWare

//

// Created by cyy on 13-1-29.

// Copyright (c) 2013年 LanOuKeJi. All rights reserved.

//

#import "AccelerometerViewController.h"

@interface AccelerometerViewController ()

@end

@implementation AccelerometerViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

if (self) {

// Custom initialization

}

return self;

}

- (void)viewDidLoad

{

[super viewDidLoad];

// Do any additional setup after loading the view.

}

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration NS_DEPRECATED_IOS(2_0, 5_0);

{

//左右挪动

NSLog(@"%f %f %f",acceleration.x,acceleration.y,acceleration.z);

if (acceleration.x < -0.2 && acceleration.x > -1) {

imageView.center = CGPointMake(imageView.center.x-0.5, imageView.center.y);

}

if (acceleration.x > 0.2 && acceleration.x < 1) {

imageView.center = CGPointMake(imageView.center.x+0.5, imageView.center.y);

}

//上下挪动

if (acceleration.y < -0.2 && acceleration.y > -1) {

imageView.center = CGPointMake(imageView.center.x, imageView.center.y+0.5);

}

if (acceleration.y > 0.2 && acceleration.y < 1) {

imageView.center = CGPointMake(imageView.center.x, imageView.center.y-0.5);

}

}

- (void)didReceiveMemoryWarning

{

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

- (void)dealloc {

[imageView release];

[super dealloc];

}

- (IBAction)stop:(id)sender

{

// 设置委托(注意对象销毁时要将委托设置为空)

[[UIAccelerometer sharedAccelerometer] setDelegate:nil];

}

- (IBAction)start:(id)sender

{

[[UIAccelerometer sharedAccelerometer] setUpdateInterval:1.0 / 60.0];

// 设置委托(注意对象销毁时要将委托设置为空)

[[UIAccelerometer sharedAccelerometer] setDelegate:self];

}

@end

==================================================================

4:通讯薄

.h导入<ABPeoplePickerNavigationControllerDelegate,ABNewPersonViewControllerDelegate>

.m:

//

// AddressViewController.m

// LessonHardWare

//

// Created by cyy on 13-1-29.

// Copyright (c) 2013年 LanOuKeJi. All rights reserved.

//

#import "AddressViewController.h"

@interface AddressViewController ()

@end

@implementation AddressViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

if (self) {

// Custom initialization

}

return self;

}

- (void)viewDidLoad

{

[super viewDidLoad];

// Do any additional setup after loading the view.

}

- (void)didReceiveMemoryWarning

{

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

- (IBAction)new:(id)sender

{

ABNewPersonViewController *new = [[ABNewPersonViewController alloc] init];

new.newPersonViewDelegate = self;

[self.navigationController pushViewController:new animated:YES];

[new release];

}

- (void)newPersonViewController:(ABNewPersonViewController *)newPersonView didCompleteWithNewPerson:(ABRecordRef)person

{

[self.navigationController popViewControllerAnimated:YES];

}

- (IBAction)address:(id)sender

{

ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];

picker.peoplePickerDelegate = self;

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

[picker release];

}

- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker

{

NSLog(@"cancel");

[self dismissViewControllerAnimated:YES completion:nil];

}

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person

{

//[self dismissViewControllerAnimated:YES completion:nil];

return YES;

}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: