發(fā)郵件、發(fā)信息、打電話

由于不知道怎么設(shè)置簡(jiǎn)述格式,無(wú)法上傳代碼文件,所以就隨便貼上代碼

導(dǎo)入MessageUI.frameWork

#import@interface SimpleCallViewController : UIViewController

/**

*? 打電話

*

*? @param number? ? ? ? ? ? ? ? ? 電話號(hào)碼

*? @param inViewController? ? 需要打電話的控制器

*/

+(void)call:(NSString *)number inViewController:(UIViewController *)vc failBlock:(void(^)())failBlock;

/**

*? 發(fā)短信

*

*? @param message? ? ? ? ? ? ? ? ? 短信內(nèi)容

*? @param inViewController? ? 需要發(fā)短信的控制器

*/

+(void)sendMessage:(NSString *)message numb:(NSString *)number inViewController:(UIViewController *)vc failBlock:(void(^)())failBlock;

/**

*? 發(fā)郵件

*

*? @param message? ? ? ? ? ? ? ? ? 短信內(nèi)容

*? @param inViewController? ? 需要發(fā)短信的控制器

*/

+(void)sendMailInViewController:(UIViewController *)vc failBlock:(void(^)())failBlock;

@end

#import "SimpleCallViewController.h"#import@interface SimpleCallViewController (){

UIViewController *viewController;

}

@property (nonatomic,strong) UIWebView *webView;

@end

@implementation SimpleCallViewController

- (instancetype)init

{

self = [super init];

if (self) {

_webView=[[UIWebView alloc] init];

}

return self;

}

-(void)viewDidDisappear:(BOOL)animated

{

[super viewDidDisappear:animated];

[self removeFromParentViewController];

}


+(void)call:(NSString *)number inViewController:(UIViewController *)vc failBlock:(void(^)())failBlock

{

//撥打電話

NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",number]];

//? 方法一、使用openURL這個(gè)API打電話結(jié)束后,返回的是系統(tǒng)的撥打電話界面

BOOL canOpen = [[UIApplication sharedApplication] canOpenURL:url];

if(!canOpen){//不能打開(kāi)

if(failBlock!=nil) failBlock(); return;

}

//方法二? 通話結(jié)束后留在自己的應(yīng)用界面

/**

*? UIWebView*callWebview =[[UIWebView alloc] init];

*? NSURL *telURL =[NSURL URLWithString:@"tel:10010"];

*? [callWebview loadRequest:[NSURLRequest requestWithURL:telURL]];

*? [self.view addSubview:callWebview];

*/

SimpleCallViewController *mediaVC=[[SimpleCallViewController alloc] init];

mediaVC.view.frame=CGRectZero;

mediaVC.view.alpha=.0f;

[vc addChildViewController:mediaVC];

[vc.view addSubview:mediaVC.view];

NSURLRequest *request=[NSURLRequest requestWithURL:url];

[mediaVC.webView loadRequest:request];

//? 方法三 不一定通過(guò)App store

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"telprompt://10010"]];

}


+(void)sendMessage:(NSString *)message numb:(NSArray *)numberArr inViewController:(UIViewController *)vc failBlock:(void(^)())failBlock{

//? ? 方法一 發(fā)送固定短信

NSString *tel = [NSString stringWithFormat:@"sms://%@",numberArr];

[[UIApplication sharedApplication]openURL:[NSURL URLWithString:tel]];//發(fā)短信

//? ? 方法二 發(fā)送任意短信

if ([MFMessageComposeViewController canSendText]) {

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

messageVc.recipients = numberArr;? //支持群發(fā)

messageVc.body = message;? ? ? ? ? //短信內(nèi)容

messageVc.messageComposeDelegate = self;

[vc presentViewController:messageVc animated:YES completion:nil];

[[[[messageVc viewControllers] lastObject] navigationItem] setTitle:@"測(cè)試短信"];//修改短信界面標(biāo)題

}

else

NSLog(@"can't send message");

}

#pragma mark ****** MFMessageComposeViewControllerDelegate ******

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

[controller dismissViewControllerAnimated:NO completion:nil];//關(guān)鍵的一句? 不能為YES

switch ( result ) {

case MessageComposeResultCancelled:

NSLog(@"發(fā)送取消");

break;

case MessageComposeResultFailed:// send failed

NSLog(@"發(fā)送成功");

break;

case MessageComposeResultSent:

NSLog(@"發(fā)送失敗");

break;

default:

break;

}

}

/** *? 發(fā)郵件 */+ (void)sendMailInViewController:(UIViewController *)vc failBlock:(void(^)())failBlock;{? ??

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

if (mailClass != nil)?

?? {

? ? ? ? if ([mailClass canSendMail])

? ? ? ? {?

?? ? ? ? ? MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];?

?? ? ? ? ? //? ? 字體顏色?

?? ? ? ? ? //? ? mc.navigationBar.tintColor = [UIColor colorWithRed:209.0/255 green:183.0/255 blue:126.0/255 alpha:1.0];

? ? ? ? ? ? //? ? 代理?

?? ? ? ? ? mc.mailComposeDelegate = self;

? ? ? ? ? ?//? ? 主題

?? ? ? ? ? ?[mc setSubject:@"Hello, World!"];

? ? ? ? ? ? //? ? 主送

? ? ? ? ? ? [mc setToRecipients:[NSArray arrayWithObjects:@"502748959@qq.com", nil]];

? ? ? ? ? ? //? ? 抄送

? ? ? ? ? ? [mc setCcRecipients:[NSArray arrayWithObject:@"wenfuhoo@163.com"]];?

?? ? ? ? ? //? ? 密送

? ? ? ? ? ? [mc setBccRecipients:[NSArray arrayWithObject:@"1945028197@qq.com"]];

? ? ? ? ? ? //? ? 內(nèi)容 1

? ? ? ? ? ? [mc setMessageBody:@"Watson!!!\n\nCome here, I need you!" isHTML:NO];

? ? ? ? ? ? //? ? 內(nèi)容 2

? ? ? ? ? ? // [mc setMessageBody:@"Hello, Joe!?What do you know?" ?isHTML:YES];?

?? ? ? ? ? //? ? 內(nèi)容 3?

?? ? ? ? ? //? ? ? ? ? ? [mailCompose setMessageBody:[self formalEmailBody] isHTML:YES];?

?? ? ? ? ? // 添加圖片

? ? ? ? ? ? UIImage *addPic = [UIImage imageNamed:@""];

? ? ? ? ? ? NSData *imageData = UIImagePNGRepresentation(addPic);? ? ? ? ? ? // png? ? ? ? ? ??

? ? ? ? // NSData *imageData = UIImageJPEGRepresentation(addPic, 1);? ? // jpeg?

? ? ? ? ? ? ?[mc addAttachmentData: imageData mimeType: @"" fileName: @""];? ??

? ? ? ? ? ? ? [vc presentViewController:mc animated:YES completion:nil]; ??

? ? }??

? ? ? else

? ? ? ? {

?? ? ? ? ? NSLog(@"無(wú)法發(fā)送郵件,請(qǐng)?jiān)凇班]箱”應(yīng)用中設(shè)置您的郵箱帳戶!");

? ? ? ? }?

?? }

}

#pragma mark ****** MFMailComposeViewControllerDelegate ******

- (void)mailComposeController:(MFMailComposeViewController*)controller

didFinishWithResult:(MFMailComposeResult)result

error:(NSError*)error {

switch (result)

{

case MFMailComposeResultCancelled:

NSLog(@"Mail send canceled...");

break;

case MFMailComposeResultSaved:

NSLog(@"Mail saved...");

break;

case MFMailComposeResultSent:

NSLog(@"Mail sent...");

break;

case MFMailComposeResultFailed:

NSLog(@"Mail send errored: %@...", [error localizedDescription]);

break;

default:

break;

}

[controller dismissViewControllerAnimated:YES completion:nil];

}

- (NSString *)formalEmailBody

{

NSString *strBody;

/// 讀取feedback.html文件

NSString *fdFile = [[NSBundle mainBundle] pathForResource:@"feedback"

ofType:@"html"];

NSString *strFeedBack = [NSString stringWithContentsOfFile:fdFile

encoding:NSUTF8StringEncoding

error:nil];

if (strFeedBack.length > 0){

/// 獲取APP名稱

NSDictionary *appInfo = [[NSBundle mainBundle] localizedInfoDictionary];

NSString *appName = [appInfo objectForKey:@"CFBundleDisplayName"];

/// 獲取APP版本號(hào)

NSString *appVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"];

/// 獲取設(shè)備類型及OS版本

NSString *devName = [UIDevice currentDevice].model;

NSString *sysVersion = [UIDevice currentDevice].systemVersion;

strBody = [NSString stringWithFormat:strFeedBack,devName,sysVersion,appName,appVersion];

}

else

strBody = @"";

return strBody;

}

-(void)dealloc

{

self.webView=nil;

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容