码迷,mamicode.com
首页 > 移动开发 > 详细

iOS 打电话 发短信(转载)

时间:2014-05-08 20:26:04      阅读:500      评论:0      收藏:0      [点我收藏+]

标签:class   ext   color   int   c   string   

官方代码

发短息和邮件添加MessageUI.framework 库

 

发送信息

- (IBAction)showSMSPicker:(id)sender

{

    // You must check that the current device can send SMS messages before you

    // attempt to create an instance of MFMessageComposeViewController.  If the

    // device can not send SMS messages,

    // [[MFMessageComposeViewController alloc] init] will return nil.  Your app

    // will crash when it calls -presentViewController:animated:completion: with

    // a nil view controller.

    if ([MFMessageComposeViewControllercanSendText])

    // The device can send email.

    {

        [selfdisplaySMSComposerSheet];

    }

    else

    // The device can not send email.

    {

        self.feedbackMsg.hidden =NO;

      self.feedbackMsg.text = @"Device not configured to send SMS.";

    }

 

 

- (void)displaySMSComposerSheet 

{

 

   MFMessageComposeViewController *picker = [[MFMessageComposeViewControllerallocinit];

    picker.messageComposeDelegate =self;

    picker.navigationBar.tintColor = [UIColorblackColor];

    picker.recipients = [NSArrayarrayWithObject:@"186888888"];

    picker.body =@"Hello from California!";

    

    [selfpresentViewController:picker animated:YEScompletion:NULL];

}

 

// -------------------------------------------------------------------------------

// messageComposeViewController:didFinishWithResult:

//  Dismisses the message composition interface when users tap Cancel or Send.

//  Proceeds to update the feedback message field with the result of the

//  operation.

// -------------------------------------------------------------------------------

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller 

  didFinishWithResult:(MessageComposeResult)result

{

self.feedbackMsg.hidden =NO;

// Notifies users about errors associated with the interface

switch (result)

{

caseMessageComposeResultCancelled:

self.feedbackMsg.text = @"Result: SMS sending canceled";

break;

caseMessageComposeResultSent:

self.feedbackMsg.text = @"Result: SMS sent";

break;

caseMessageComposeResultFailed:

self.feedbackMsg.text = @"Result: SMS sending failed";

break;

default:

self.feedbackMsg.text = @"Result: SMS not sent";

break;

}

    

[selfdismissViewControllerAnimated:YEScompletion:NULL];

}

 

发送邮件

 

- (IBAction)showMailPicker:(id)sender

{

    // You must check that the current device can send email messages before you

    // attempt to create an instance of MFMailComposeViewController.  If the

    // device can not send email messages,

    // [[MFMailComposeViewController alloc] init] will return nil.  Your app

    // will crash when it calls -presentViewController:animated:completion: with

    // a nil view controller.

    if ([MFMailComposeViewControllercanSendMail])

    // The device can send email.

    {

        [selfdisplayMailComposerSheet];

    }

    else

    // The device can not send email.

    {

        self.feedbackMsg.hidden =NO;

self.feedbackMsg.text =@"Device not configured to send mail.";

    }

}

 

#pragma mark - Compose Mail/SMS

 

// -------------------------------------------------------------------------------

// displayMailComposerSheet

//  Displays an email composition interface inside the application.

//  Populates all the Mail fields.

// -------------------------------------------------------------------------------

- (void)displayMailComposerSheet 

{

 

MFMailComposeViewController *picker = [[MFMailComposeViewControllerallocinit];

picker.mailComposeDelegate =self;

 

[picker setSubject:@"Hello from California!"];

 

// Set up recipients

 

NSArray *toRecipients = [NSArrayarrayWithObject:@"first@example.com"]; 

NSArray *ccRecipients = [NSArrayarrayWithObjects:@"second@example.com",@"third@example.com"nil]; 

NSArray *bccRecipients = [NSArrayarrayWithObject:@"fourth@example.com"]; 

 

[picker setToRecipients:toRecipients];

[picker setCcRecipients:ccRecipients];

[picker setBccRecipients:bccRecipients];

 

// Attach an image to the email

 

NSString *path = [[NSBundlemainBundlepathForResource:@"rainy"ofType:@"jpg"];

NSData *myData = [NSDatadataWithContentsOfFile:path];

[picker addAttachmentData:myData mimeType:@"image/jpeg"fileName:@"rainy"];

 

 

// Fill out the email body text

 

 

NSString *emailBody =@"It is raining in sunny California!";

[picker setMessageBody:emailBody isHTML:NO];

 

[selfpresentViewController:picker animated:YEScompletion:NULL];

 

 

}

 

 

#pragma mark - Delegate Methods

 

// -------------------------------------------------------------------------------

// mailComposeController:didFinishWithResult:

//  Dismisses the email composition interface when users tap Cancel or Send.

//  Proceeds to update the message field with the result of the operation.

// -------------------------------------------------------------------------------

- (void)mailComposeController:(MFMailComposeViewController*)controller 

didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error

{

self.feedbackMsg.hidden =NO;

// Notifies users about errors associated with the interface

switch (result)

{

caseMFMailComposeResultCancelled:

self.feedbackMsg.text = @"Result: Mail sending canceled";

break;

caseMFMailComposeResultSaved:

self.feedbackMsg.text = @"Result: Mail saved";

break;

caseMFMailComposeResultSent:

self.feedbackMsg.text = @"Result: Mail sent";

break;

caseMFMailComposeResultFailed:

self.feedbackMsg.text = @"Result: Mail sending failed";

break;

default:

self.feedbackMsg.text = @"Result: Mail not sent";

break;

}

    

[selfdismissViewControllerAnimated:YEScompletion:NULL];

}

 

打电话 我只写了一种简单的方式 还有其他的

添加  AddressBookUI.framework 库

 

#import <AddressBook/AddressBook.h>

#import <AddressBook/ABMultiValue.h>

#import <AddressBook/ABRecord.h>

 

 

 

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath

{

    NSString *key = [keyArrayobjectAtIndex:indexPath.row];

    NSArray *array = [tableDataDictionaryobjectForKey:key];

    

    NSString *phoneNum = [arrayobjectAtIndex:1];//电话号码

    

    NSURL *phoneURL = [NSURLURLWithString:[NSString stringWithFormat:@"tel:%@",phoneNum]];

    

    if ( !_phoneCallWebView ) {

        

        _phoneCallWebView = [[UIWebViewallocinitWithFrame:CGRectZero];

        

    }

    

    [_phoneCallWebViewloadRequest:[NSURLRequest requestWithURL:phoneURL]];

   

}

 

iOS 打电话 发短信(转载),布布扣,bubuko.com

iOS 打电话 发短信(转载)

标签:class   ext   color   int   c   string   

原文地址:http://www.cnblogs.com/alihaiseyao/p/3709530.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!