IOS程式內發簡訊

來源:互聯網
上載者:User
文章目錄
  • MFMessageComposeViewController

iOS4.0新加入了MFMessageComposeViewController和MFMessageComposeViewControllerDelegate,提供了傳送簡訊的介面,可以像發送郵件那樣不用跳出程式來傳送簡訊. 介紹可參閱Message UI
Framework Reference

一些筆記:

MFMessageComposeViewController
  • 提供了操作介面
  • 使用前必須檢查canSendText方法,若返回NO則不應將這個controller展現出來,而應該提示使用者不支援傳送簡訊功能.
  • 介面不能自行定製
  • 要發送的簡訊的內容(body)和收件者(recipients)在展現這個controller前需初始化好,展現了之後簡訊內容不能通過程式來進行修改.不過使用者仍然可以手工修改簡訊內容和選擇收件者
  • 使用者點了發送或者取消,或者發送失敗時,MFMessageComposeViewControllerDelegate 的– messageComposeViewController:didFinishWithResult:方法都能得到通知,在這裡進行相應的處理

若在iOS3.0上啟動並執行話,會提示dyld: Symbol not found: _OBJC_CLASS_$_MFMessageComposeViewController .解決方案:

  1. MessageUI.framework的引入類型應選擇weak(在target -> Get Info -> General -> Linked Libraries -> MessageUI.framework -> Type 裡修改)
  2. 不要在.h檔案裡直接import MessageUI/MFMessageComposeViewController.h,改為import <MessageUI/MessageUI.h>

代碼:

01 #pragma mark -
02 #pragma mark SMS
03  
04 -(IBAction)showSMSPicker:(id)sender {
05     //  The MFMessageComposeViewController class is only available in iPhone OS 4.0 or later.
06     //  So, we must verify the existence of the above class and log an error message for devices
07     //      running earlier versions of the iPhone OS. Set feedbackMsg if device doesn't support
08     //      MFMessageComposeViewController API.
09     Class messageClass = (NSClassFromString(@"MFMessageComposeViewController"));
10  
11     if (messageClass != nil) {
12         // Check whether the current device is configured for sending SMS messages
13         if ([messageClass canSendText]) {
14             [self displaySMSComposerSheet];
15         }
16         else {
17             [UIAlertView quickAlertWithTitle:@"裝置沒有簡訊功能" messageTitle:nil dismissTitle:@"關閉"];
18         }
19     }
20     else {
21         [UIAlertView quickAlertWithTitle:@"iOS版本過低,iOS4.0以上才支援程式內傳送簡訊" messageTitle:nil dismissTitle:@"關閉"];
22     }
23 }
24  
25 -(void)displaySMSComposerSheet
26 {
27     MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
28     picker.messageComposeDelegate = self;
29  
30     NSMutableString* absUrl = [[NSMutableString alloc] initWithString:web.request.URL.absoluteString];
31     [absUrl replaceOccurrencesOfString:@"http://i.aizheke.com" withString:@"http://m.aizheke.com" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [absUrl length])];
32  
33     picker.body=[NSString stringWithFormat:@"我在愛折客上看到:%@ 可能對你有用,推薦給你!link:%@"
34                                         ,[web stringByEvaluatingJavaScriptFromString:@"document.title"]
35                                         ,absUrl];
36     [absUrl release];
37     [self presentModalViewController:picker animated:YES];
38     [picker release];
39 }
40  
41 - (void)messageComposeViewController:(MFMessageComposeViewController *)controller
42                  didFinishWithResult:(MessageComposeResult)result {
43  
44     switch (result)
45     {
46         case MessageComposeResultCancelled:
47             LOG_EXPR(@"Result: SMS sending canceled");
48             break;
49         case MessageComposeResultSent:
50             LOG_EXPR(@"Result: SMS sent");
51             break;
52         case MessageComposeResultFailed:
53             [UIAlertView quickAlertWithTitle:@"簡訊發送失敗" messageTitle:nil dismissTitle:@"關閉"];
54             break;
55         default:
56             LOG_EXPR(@"Result: SMS not sent");
57             break;
58     }
59     [self dismissModalViewControllerAnimated:YES];
60 }
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.