IOS怎樣使用MessageUI Framework 發(fā)送短信息。使用MessageUI Framework 中的 MFMessageComposeViewController 發(fā)送短信息,另外其中的MFMailComposeViewController用于發(fā)送郵件。使用非常簡單:
目前創(chuàng)新互聯(lián)公司已為上1000家的企業(yè)提供了網(wǎng)站建設、域名、網(wǎng)頁空間、網(wǎng)站運營、企業(yè)網(wǎng)站設計、曲水網(wǎng)站維護等服務,公司將堅持客戶導向、應用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。
導入框架:MessageUI.framework
2. 添加協(xié)議:MFMessageComposeViewControllerDelegate
3. #import MessageUI/MessageUI.h
1 //判斷當前設備是否可以發(fā)送短信息
2 if([MFMessageComposeViewController canSendText])
3 {
4
5 MFMessageComposeViewController *mc = [[MFMessageComposeViewController alloc] init];
6 //設置委托
7 mc.messageComposeDelegate = self;
8 //短信內容
9 mc.body = @"nihao";
10 //短信接收者,可設置多個
11 mc.recipients = [NSArray arrayWithObject:@"10086",nil];
12
13 [self presentModalViewController:mc animated:YES];
14 [mc release];
15 }
16 else
17 {
18 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error",
19 message:@"The current device can not send SMS"
20 delegate:nil
21 cancelButtonTitle:"OK",
22 otherButtonTitles:nil];
23 [alert show];
24 [alert release];
25 }
26
27
28 #pragma mark -
29 #pragma mark MFMessageComposeViewControllerDelegate
30 -(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
31 {
32 switch(result)
33 {
34 case MessageComposeResultCancelled:
35 ...
36 case MessageComposeResultFailed:
37 ...
38 case MessageComposeResultSend:
39 ...
40 default:
41 ...
42 }
43 }
這個方法,并不是很高級,因為只能寫好聯(lián)系人,下面提供一個方法,可以直接編輯好內容還有收信人的!
不過本人求一個方法,可以直接把短信給發(fā)送出去了的,為這個方法只能編輯好,最后還是要用戶自己點擊一下發(fā)送按鈕,有不有什么方法可以直接發(fā)送出去了呢!希望各位賜教留言!!
pre[[UIApplication sharedApplication]openURL:[NSURL URLWithString:
@sms://13888888888]];
-(void)messageComposeViewController(MFMessageComposeViewController*)controller didFinishWithResult:(MessageComposeResult)result
{
[self dismissViewControllerAnimated:YES completion:nil];
switch
(result) {
case
MessageComposeResultSent:
信息傳送成功
break;
case
MessageComposeResultFailed:
信息傳送失敗
break;
case
MessageComposeResultCancelled:
信息被用戶取消傳送
break;
default:
break;
}
}/pre
程序內調用系統(tǒng)發(fā)短信
pre
實現(xiàn)代理方法MFMessageComposeViewControllerDelegate
-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
[self dismissViewControllerAnimated:YES completion:nil];
switch (result) {
case MessageComposeResultSent:
//信息傳送成功
}
/pre
發(fā)送短信
pre
-(void)showMessageView:(NSArray *)phones title:(NSString *)title body:(NSString *)body
{
if( [MFMessageComposeViewController canSendText] )
{
MFMessageComposeViewController * controller = [[MFMessageComposeViewController alloc] init];
controller.recipients = phones;
controller.navigationBar.tintColor = [UIColor redColor];
controller.body = body;
controller.messageComposeDelegate = self;
[self presentViewController:controller animated:YES completion:nil];
[[[[controller viewControllers] lastObject] navigationItem] setTitle:title];//修改短信界面標題
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@提示信息
message:@該設備不支持短信功能
delegate:nil
cancelButtonTitle:@確定
otherButtonTitles:nil, nil];
[alert show];
}
}
參數(shù)phones:發(fā)短信的手機號碼的數(shù)組,數(shù)組中是一個即單發(fā),多個即群發(fā)。
/pre
調用發(fā)短信的方法
pre
[self showMessageView:[NSArray arrayWithObjects:@13888888888,@13999999999, nil] title:@test body:@你是土豪么,么么噠];
/pre