iPhone 下应用中发送短信
来源:互联网 发布:sql怎么删除置顶列 编辑:程序博客网 时间:2024/05/27 09:48
iPhone Tutorial: How to send In-App SMS
Officially, iPhone OS 4 is out of NDA and I can’t write a post on this. If you have been reading my blogs, you might already know how to send ain-app email Sending a in-app SMS is very similar to this, but with subtle differences.
Prior to iPhone OS 4, developers have to depend on
[[UIApplication sharedApplication] openURL: @"sms:12345678"];
The problem with this is not just that it closes your app, but there is no way to specify the body content of the SMS. Secondly, you are restricted to send the SMS to only one person. However, with the new MessageUI SMS controller, you can send SMS to multiple people at the same time. You can also pre-populate the SMS body field.
Developers of famous apps like Whatsapp Messenger, copy the SMS text content to clipboard and open the SMS app to allow users to paste the content. But with this newly allowed In-App SMS sheet, users can send SMS without quitting the app.
So, Let’s get started.
Step 1:
Import the MessageUI Framework into your project and #import the header file into the “.h” file of your controller where you want to open the In-App SMS sheet.
Step 2:
You might already have a button handler IBAction where you want to send the SMS. If not create a Button on your XIB file and write IBActions for it.
Step 3:
The real code
-(IBAction) sendInAppSMS:(id) sender{MFMessageComposeViewController *controller = [[[MFMessageComposeViewController alloc] init] autorelease];if([MFMessageComposeViewController canSendText]){controller.body = @"Hello from Mugunth";controller.recipients = [NSArray arrayWithObjects:@"12345678", @"87654321", nil];controller.messageComposeDelegate = self;[self presentModalViewController:controller animated:YES];}}
The most important part here is the line [MFMessageComposeViewController canSendText].
When sending a in-app email, you can choose to ignore this (atleast as of now) because most of the devices would have upgraded to iPhone OS 3 and all those devices would have the ability to send in-app email. However, the same doesn’t apply to SMS. Remember that even if a device is running iPhone OS 4, if it’s an iPod touch, it will never be abel to send SMS within app.
In this case, I have used a if condition to send the SMS. Practically speaking, you should enable/disable the button the user taps to send the sms based on this. You can add the code that does this in your viewDidLoad method.
Secondly, you have to set the messageComposeDelegate to self and not delegate. If you set the controller.delegate to self, you will not get the didFinishWithResult callback and the In-App SMS sheet will not close.
Step 4:
Implement Delegate Callbacks.
In your header file, implement the callbacks, MFMessageComposeViewControllerDelegate and UINavigationControllerDelegate. If you don’t you will get a warning at the line,
controller.delegate = self;
You have to handle a callback method of MFMessageComposeViewControllerDelegate so as to dismiss the modal view controller.
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{switch (result) {case MessageComposeResultCancelled:NSLog(@"Cancelled");break;case MessageComposeResultFailed:UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"MyApp" message:@"Unknown Error" delegate:self cancelButtonTitle:@”OK” otherButtonTitles: nil];[alert show];[alert release];break;case MessageComposeResultSent: break;default:break;} [self dismissModalViewControllerAnimated:YES];}
That’s it. Your app should now be able to send SMS using the new Message UI sheet.
Where the heck is MMS in this tutorial?
As on date, the MFMessageComposeViewController doesn’t support sending MMS. The controller.body is a NSString and setting a NSData pointer obviously crashes the app. Hopefully, one day, Apple will allow sending In-App MMS and I’ll probably blog about that too- iPhone 下应用中发送短信
- android 应用中短信发送
- 如何在iphone应用程序中发送短信
- iphone在应用程序中发送短信
- 如何在iphone应用程序中发送短信
- iPhone 应用中发送邮件
- iphone 如何发送短信
- IPhone之发送短信
- iphone发送短信
- 安卓应用中发送短信
- 【iphone应用开发】iphone应用开发之十:发送短信/邮件/打电话
- iphone 发送短信/邮件/打电话
- android应用-短信发送
- SMS短信发送应用
- 短信发送--短信发送流程--应用层
- 基于C#开发Windows Mobile应用中短信发送功能
- android 中发送短信
- android中发送短信
- 网站压力测试
- KMPC串匹配
- 俄式乘法
- jquery实现限输入整数,全选反选,批量删除,设置排序
- 网站上线前测试工作
- iPhone 下应用中发送短信
- 块级元素和级联元素
- 关于c语言中void*的解释
- CSS样式表与选择器
- 远程文件回传系统---DEMO
- 用JSP实现文件下载
- Tomcat:IOException while loading persisted sessions: java.io.EOFException解决手记
- 配置cvsanaly2
- 远离家乡的程序员困惑