sending sms programmatically in iphone
How can you send an SMS programmatically to a particul开发者_如何学Goar number selected in the contact list in iPhone?
MFMessageComposeController
is what you're looking for.
To send an SMS, you're looking at something like this:
#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
That's the only way to send SMSs from your app at the moment. Unless you just want to open the SMS app. If you aren't worried about the body of the message, you can do this:
NSString *smsURL = @"sms:12345678";
NSURL *url = [NSURL URLWithString:smsURL];
[[UIApplication sharedApplication] openURL:url];
Uh... I think a little discussion would be helpful here. I (maybe mistakenly) take the question, "... send an SMS programmatically ..." to mean sending SMS behind the scenes, w o the MFMessageComposeViewController popping up.
The green checkmark for the answer above is incorrect IF that's the question. I am going to assume that that is the question (I bet I'm not the only one) and offer some bullets to save others the time I have spent getting here.
- There is quiet a bit of discussion in stack that this cannot be done in iOS. and here
- The cordova plugin for Android does it just fine
- the cordova plugin for iOS does not (implying that it cant be done.)
- There is no way the code above runs. Its a sort of pseudo-code. The animated:NO on presentModalViewController, prevents the vc from popping up, but I always end up at didFinishWithResult with MessageCancelled.
- Its right in character for Apple to prevent this.
精彩评论