如何以编程方式将短信发送到iPhone中联系人列表中选择的特定号码?
答案 0 :(得分:4)
MFMessageComposeController
正是您正在寻找的。
要发送短信,您会看到以下内容:
#import <MessageUI/MessageUI.h>
@interface myClass : NSObject <MFMessageComposeViewControllerDelegate>{
}
@end
@implementation
-(void)sendMessage{
if([MFMessageComposeController canSendText]){
MFMessageComposeController *smsComposer =
[[MFMessageComposeController alloc] init];
smsComposer.recipients = [NSArray arrayWithObject:@"12345678"];
smsComposer.body = @"SMS BODY HERE";
smsComposer.delegate = self;
[self presentModalViewController:smsComposer animated:NO];
}
else{
//You probably want to show a UILocalNotification here.
}
}
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller
didFinishWithResult:(MessageComposeResult)result{
/* You can use the MessageComposeResult to determine what happened to the
message. I believe it tells you about sent, stored for sending later, failed
or cancelled. */
[self dismissModalViewControllerAnimated:NO];
}
@end
这是目前从您的应用发送短信的唯一方式。除非你只是想打开短信应用程序。如果您不担心邮件正文,可以这样做:
NSString *smsURL = @"sms:12345678";
NSURL *url = [NSURL URLWithString:smsURL];
[[UIApplication sharedApplication] openURL:url];
答案 1 :(得分:1)
如果这是问题,上面答案的绿色选中标记是不正确的。我将假设这是一个问题(我打赌我不是唯一一个)并提供一些子弹来拯救其他人我花在这里的时间。