有没有人知道,通过官方SDK / Cocoa Touch,从iPhone发送短信是否可行,以及如何实现?
当前回答
添加MessageUI。框架并使用下面的代码
#import <MessageUI/MessageUI.h>
然后:
if ([MFMessageComposeViewController canSendText]) {
MFMessageComposeViewController *messageComposer =
[[MFMessageComposeViewController alloc] init];
NSString *message = @"Your Message here";
[messageComposer setBody:message];
messageComposer.messageComposeDelegate = self;
[self presentViewController:messageComposer animated:YES completion:nil];
}
以及委托方法-
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller
didFinishWithResult:(MessageComposeResult)result {
[self dismissViewControllerAnimated:YES completion:nil];
}
其他回答
//使用名称和数字调用方法。
-(void)openMessageViewWithName:(NSString*)contactName withPhone:(NSString *)phone{
CTTelephonyNetworkInfo *networkInfo=[[CTTelephonyNetworkInfo alloc]init];
CTCarrier *carrier=networkInfo.subscriberCellularProvider;
NSString *Countrycode = carrier.isoCountryCode;
if ([Countrycode length]>0) //Check If Sim Inserted
{
[self sendSMS:msg recipientList:[NSMutableArray arrayWithObject:phone]];
}
else
{
[AlertHelper showAlert:@"Message" withMessage:@"No sim card inserted"];
}
}
//发送消息的方法
- (void)sendSMS:(NSString *)bodyOfMessage recipientList:(NSMutableArray *)recipients{
MFMessageComposeViewController *controller1 = [[MFMessageComposeViewController alloc] init] ;
controller1 = [[MFMessageComposeViewController alloc] init] ;
if([MFMessageComposeViewController canSendText])
{
controller1.body = bodyOfMessage;
controller1.recipients = recipients;
controller1.messageComposeDelegate = self;
[self presentViewController:controller1 animated:YES completion:Nil];
}
}
用这个:
- (void)showSMSPicker
{
Class messageClass = (NSClassFromString(@"MFMessageComposeViewController"));
if (messageClass != nil) {
// Check whether the current device is configured for sending SMS messages
if ([messageClass canSendText]) {
[self displaySMSComposerSheet];
}
}
}
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
//feedbackMsg.hidden = NO;
// Notifies users about errors associated with the interface
switch (result)
{
case MessageComposeResultCancelled:
{
UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"Message" message:@"SMS sending canceled!!!" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alert1 show];
[alert1 release];
}
// feedbackMsg.text = @"Result: SMS sending canceled";
break;
case MessageComposeResultSent:
{
UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:@"Message" message:@"SMS sent!!!" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alert2 show];
[alert2 release];
}
// feedbackMsg.text = @"Result: SMS sent";
break;
case MessageComposeResultFailed:
{
UIAlertView *alert3 = [[UIAlertView alloc] initWithTitle:@"Message" message:@"SMS sending failed!!!" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alert3 show];
[alert3 release];
}
// feedbackMsg.text = @"Result: SMS sending failed";
break;
default:
{
UIAlertView *alert4 = [[UIAlertView alloc] initWithTitle:@"Message" message:@"SMS not sent!!!" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alert4 show];
[alert4 release];
}
// feedbackMsg.text = @"Result: SMS not sent";
break;
}
[self dismissModalViewControllerAnimated: YES];
}
你可以使用这种方法:
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"sms:MobileNumber"]]
iOS会自动从你的应用程序导航到消息应用程序的消息合成页面。由于URL的方案是从sms:开始的,这被标识为消息应用程序可以识别的类型,并启动它。
如果您愿意,您可以使用名为CTMessageCenter类的私有框架CoreTelephony。有一些发送短信的方法。
You must add the MessageUI.framework to your Xcode project Include an #import <MessageUI/MessageUI.h> in your header file Add these delegates to your header file MFMessageComposeViewControllerDelegate & UINavigationControllerDelegate In your IBAction method declare instance of MFMessageComposeViewController say messageInstance To check whether your device can send text use [MFMessageComposeViewController canSendText] in an if condition, it'll return Yes/No In the if condition do these: First set body for your messageInstance as: messageInstance.body = @"Hello from Shah"; Then decide the recipients for the message as: messageInstance.recipients = [NSArray arrayWithObjects:@"12345678", @"87654321", nil]; Set a delegate to your messageInstance as: messageInstance.messageComposeDelegate = self; In the last line do this: [self presentModalViewController:messageInstance animated:YES];
推荐文章
- 如何在iOS上进行base64编码?
- 如何停止不必要的UIButton动画标题变化?
- 是否有可能更新一个本地化的故事板的字符串?
- 以编程方式获取Bundle Identifier
- 为iPad和iPhone设计输入按钮
- 如何在我的iPhone应用程序中使用NSError ?
- 我的预发行应用已经在iTunes Connect中“处理”了一周多,这是怎么回事?
- Xcode iOS项目只显示“我的Mac 64位”,但不显示模拟器或设备
- Objective-C中的自动引用计数不能防止或减少什么样的泄漏?
- 在Xcode 9中如何从打开的多个模拟器中退出或关闭单个模拟器?
- 如何使用Swift播放声音?
- UINavigationBar自定义返回按钮没有标题
- 如何精确地以毫秒为单位记录方法的执行时间?
- 在整个UIWindow中获取UIView的位置
- 如何解散ViewController在Swift?