iOS開發(fā)之發(fā)送信息 ?郵件 ?打電話

創(chuàng)建MFMessageComposeViewController對象。

設置收件人recipients、

信息正文body,

主題subject

附件attachments(可以通過canSendSubject、canSendAttachments方法判斷是否支持)

設置代理messageComposeDelegate(注意這里不是delegate屬性,因為delegate屬性已經(jīng)留給UINavigationController,MFMessageComposeViewController沒有覆蓋此屬性而是重新定義了一個代理),實現(xiàn)代理方法獲得發(fā)送狀態(tài)。

isSupportedAttachmentUTI ?判斷是否支持統(tǒng)一標識附件

根據(jù)URL路徑和添加附件,返回YES表示添加成功

- (BOOL)addAttachmentURL:(NSURL *)attachmentURL withAlternateFilename:(NSString *)alternateFilename;

根據(jù)Data數(shù)據(jù)添加附件

- (BOOL)addAttachmentData:(NSData *)attachmentData typeIdentifier:(NSString *)uti filename:(NSString *)filename;

#import"ViewController.h"

//引入框架

#import

@interfaceViewController()

@property(nonatomic,strong)UIWebView*webView;

@end

@implementationViewController

- (void)viewDidLoad {

[superviewDidLoad];

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

UIButton*phoneButton = [UIButtonbuttonWithType:UIButtonTypeCustom];

phoneButton.frame=CGRectMake(100,100,100,100);

[phoneButtonsetTitle:@"打電話"forState:UIControlStateNormal];

[phoneButtonsetTitleColor:[UIColorredColor]forState:UIControlStateNormal];

[phoneButtonaddTarget:selfaction:@selector(phoneButtonAction:)forControlEvents:UIControlEventTouchUpInside];

UIButton*messageButton = [UIButtonbuttonWithType:UIButtonTypeCustom];

messageButton.frame=CGRectMake(100,240,100,100);

[messageButtonsetTitle:@"發(fā)信息"forState:UIControlStateNormal];

[messageButtonsetTitleColor:[UIColorredColor]forState:UIControlStateNormal];

[messageButtonaddTarget:selfaction:@selector(messageButtonAction:)forControlEvents:UIControlEventTouchUpInside];

UIButton*emailButton = [UIButtonbuttonWithType:UIButtonTypeCustom];

emailButton.frame=CGRectMake(100,400,100,100);

[emailButtonsetTitle:@"發(fā)郵件"forState:UIControlStateNormal];

[emailButtonsetTitleColor:[UIColorredColor]forState:UIControlStateNormal];

[emailButtonaddTarget:selfaction:@selector(emailButtonAction:)forControlEvents:UIControlEventTouchUpInside];

[self.viewaddSubview:phoneButton];

[self.viewaddSubview:messageButton];

[self.viewaddSubview:emailButton];

}

//電話有三種方法

- (void)phoneButtonAction:(UIButton*)button

{

//法一:沒有彈窗提示直接運行不能回到原來的應用程序

//? tel://是代碼規(guī)范

//??? NSURL *url = [NSURL URLWithString:@"tel://15385548670"];

//??? [[UIApplication sharedApplication] openURL:url];

//法二:有彈窗提示可以回到原來的應用程序

//? telprompt這個是蘋果的私有方法應用程序中如果使用了這個方法審核會被駁回

//??? NSURL *url = [NSURL URLWithString:@"telprompt://15385548670"];

//??? [[UIApplication sharedApplication] openURL:url];

//法三:借助webView打電話有彈窗會回到原來的應用程序建議使用

//懶加載不要將webView添加到self.view如果添加的話會遮擋原應用程序

if(_webView==nil) {

_webView= [[UIWebViewalloc]init];

}

NSURL*url = [NSURLURLWithString:@"tel://15385548670"];

//創(chuàng)建一個請求

NSURLRequest*request = [NSURLRequestrequestWithURL:url];

//通過請求加載webView

[_webViewloadRequest:request];

}

//? message和email需要需要添加一個一個框架

- (void)messageButtonAction:(UIButton*)button

{

if([MFMessageComposeViewControllercanSendText]) {

//初始化一個控制器

MFMessageComposeViewController*messageVC = [[MFMessageComposeViewControlleralloc]init];

//設置短信內(nèi)容

//設置收件人可設置多個

messageVC.recipients=@[@"15385548670"];

//設置短信內(nèi)容

messageVC.body=@"??";

//設置代理

messageVC.messageComposeDelegate=self;

//跳轉(zhuǎn)

[selfpresentViewController:messageVCanimated:YEScompletion:nil];

}else{

UIAlertView*alert = [[UIAlertViewalloc]initWithTitle:@"提示信息"

message:@"該設備不支持短信功能"

delegate:nil

cancelButtonTitle:@"確定"

otherButtonTitles:nil,nil];

[alertshow];

}

}

- (void)emailButtonAction:(UIButton*)button

{

if([MFMailComposeViewControllercanSendMail]) {

//初始化一個控制器

MFMailComposeViewController*mailVC = [[MFMailComposeViewControlleralloc]init];

//設置郵件主題

[mailVCsetSubject:@"主題"];

//設置收件人

[mailVCsetToRecipients:@[@"2240054474@qq.com"]];

//設置正文

[mailVCsetMessageBody:@"正文"isHTML:NO];

//添加附件

UIImage*image = [UIImageimageNamed:@"1.png"];

//把UIImage轉(zhuǎn)化成NSData

NSData*data =UIImagePNGRepresentation(image);

[mailVCaddAttachmentData:datamimeType:@"image/png"fileName:@"2.png"];

//設置代理

mailVC.mailComposeDelegate=self;

//跳轉(zhuǎn)界面

[selfpresentViewController:mailVCanimated:YEScompletion:nil];

}

}

//發(fā)送信息的實現(xiàn)代理方法

- (void)messageComposeViewController:(MFMessageComposeViewController*)controller didFinishWithResult:(MessageComposeResult)result {[selfdismissViewControllerAnimated:YEScompletion:nil];}

//發(fā)送郵件代理方法的實現(xiàn)

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

{

[selfdismissViewControllerAnimated:YEScompletion:nil];

}

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

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

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