IOS程序内发短信
2019獨角獸企業重金招聘Python工程師標準>>>
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 .解決方案:
代碼:
#pragma mark -#pragma mark SMS-(IBAction)showSMSPicker:(id)sender {// The MFMessageComposeViewController class is only available in iPhone OS 4.0 or later.// So, we must verify the existence of the above class and log an error message for devices// running earlier versions of the iPhone OS. Set feedbackMsg if device doesn't support// MFMessageComposeViewController API. Class messageClass = (NSClassFromString(@"MFMessageComposeViewController"));if (messageClass != nil) {// Check whether the current device is configured for sending SMS messages if ([messageClass canSendText]) {[self displaySMSComposerSheet];}else {[UIAlertView quickAlertWithTitle:@"設備沒有短信功能" messageTitle:nil dismissTitle:@"關閉"];}}else {[UIAlertView quickAlertWithTitle:@"iOS版本過低,iOS4.0以上才支持程序內發送短信" messageTitle:nil dismissTitle:@"關閉"];} }-(void)displaySMSComposerSheet {MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];picker.messageComposeDelegate = self;NSMutableString* absUrl = [[NSMutableString alloc] initWithString:web.request.URL.absoluteString];[absUrl replaceOccurrencesOfString:@"http://i.aizheke.com" withString:@"http://m.aizheke.com" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [absUrl length])];picker.body=[NSString stringWithFormat:@"我在愛折客上看到:%@ 可能對你有用,推薦給你!link:%@" ,[web stringByEvaluatingJavaScriptFromString:@"document.title"],absUrl];[absUrl release];[self presentModalViewController:picker animated:YES];[picker release]; }- (void)messageComposeViewController:(MFMessageComposeViewController *)controllerdidFinishWithResult:(MessageComposeResult)result {switch (result){case MessageComposeResultCancelled:LOG_EXPR(@"Result: SMS sending canceled");break;case MessageComposeResultSent:LOG_EXPR(@"Result: SMS sent");break;case MessageComposeResultFailed:[UIAlertView quickAlertWithTitle:@"短信發送失敗" messageTitle:nil dismissTitle:@"關閉"];break;default:LOG_EXPR(@"Result: SMS not sent");break;}[self dismissModalViewControllerAnimated:YES]; }轉載于:https://my.oschina.net/orange1/blog/77615
總結
- 上一篇: ffmpeg h264+ts +udp传
- 下一篇: 两个精彩的比喻:吞吐量和延迟、信号量和互