調用系統的短信和發送郵件功能,實現短信分享郵件分享


導入MessageUI.framework

.h文件#import <MessageUI/MessageUI.h>

#import<MessageUI/MFMailComposeViewController.h>

 

實現 MFMailComposeViewControllerDelegate

MFMessageComposeViewControllerDelegate

 

.m 文件

//郵件

-(void)showMailPicker {

   Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));

    

if (mailClass !=nil) {

        if ([mailClass canSendMail]) {

[selfdisplayMailComposerSheet];

}else{

            UIAlertView *alert=[[UIAlertView allocinitWithTitle:@""message:@"設備不支持郵件功能" delegate:selfcancelButtonTitle:@"確定"otherButtonTitles:nil];

            [alert show];

            [alert release];

        }

    }else{

        

    }

    

}

-(void)displayMailComposerSheet 

{

MFMailComposeViewController *picker = [[MFMailComposeViewControllerallocinit];

   

picker.mailComposeDelegate =self;

 

[pickersetSubject:@"文件分享"];

 

 

// Set up recipients

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

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

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

    

 

[pickersetToRecipients:toRecipients];

[pickersetCcRecipients:ccRecipients];

[pickersetBccRecipients:bccRecipients];

//發送圖片附件

//NSString *path = [[NSBundle mainBundle] pathForResource:@"rainy" ofType:@"jpg"];

//NSData *myData = [NSData dataWithContentsOfFile:path];

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

 

//發送txt文本附件

//NSString *path = [[NSBundle mainBundle] pathForResource:@"MyText" ofType:@"txt"];

//NSData *myData = [NSData dataWithContentsOfFile:path];

//[picker addAttachmentData:myData mimeType:@"text/txt" fileName:@"MyText.txt"];

 

//發送doc文本附件 

//NSString *path = [[NSBundle mainBundle] pathForResource:@"MyText" ofType:@"doc"];

//NSData *myData = [NSData dataWithContentsOfFile:path];

//[picker addAttachmentData:myData mimeType:@"text/doc" fileName:@"MyText.doc"];

 

//發送pdf文檔附件

/*

NSString *path = [[NSBundlemainBundlepathForResource:@"CodeSigningGuide"ofType:@"pdf"];

NSData *myData = [NSDatadataWithContentsOfFile:path];

[pickeraddAttachmentData:myData mimeType:@"file/pdf"fileName:@"rainy.pdf"];

*/

 

 

// Fill out the email body text

NSString *emailBody =[NSStringstringWithFormat:@"我分享了文件給您,地址是%@",address] ;

[pickersetMessageBody:emailBody isHTML:NO];

 

[selfpresentModalViewController:picker animated:YES];

[pickerrelease];

}

- (void)mailComposeController:(MFMailComposeViewController*)controller 

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

 

 

// Notifies users about errors associated with the interface

switch (result)

{

caseMFMailComposeResultCancelled:

NSLog(@"Result: Mail sending canceled");

break;

caseMFMailComposeResultSaved:

           NSLog(@"Result: Mail saved");

break;

caseMFMailComposeResultSent:

NSLog(@"Result: Mail sent");

break;

caseMFMailComposeResultFailed:

NSLog(@"Result: Mail sending failed");

break;

default:

NSLog(@"Result: Mail not sent");

break;

}

[selfdismissModalViewControllerAnimated:YES];

}

//短信

-(void)showSMSPicker{

   Class messageClass = (NSClassFromString(@"MFMessageComposeViewController"));

 

    if (messageClass != nil) { 

   // Check whether the current device is configured for sending SMS messages

        if ([messageClass canSendText]) {

        [selfdisplaySMSComposerSheet];

        }

        else {

            UIAlertView *alert=[[UIAlertView allocinitWithTitle:@""message:@"設備不支持短信功能" delegate:selfcancelButtonTitle:@"確定"otherButtonTitles:nil];

            [alert show];

            [alert release];

            

        }

    }

    else {

        }

 }

-(void)displaySMSComposerSheet

{

   MFMessageComposeViewController *picker = [[MFMessageComposeViewControllerallocinit];

picker.messageComposeDelegate =self;

NSString *smsBody =[NSStringstringWithFormat:@"我分享了文件給您,地址是%@",address] ;

    picker.body=smsBody;

[selfpresentModalViewController:picker animated:YES];

[pickerrelease];

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM