how to send sms via mobile number in iphone
i have created a music greetings application in iphone. I want that when a user enters his mobile number in th开发者_开发问答e text field and clicks on the send button data from another view should be fetched and send an sms to user's mobile number.
Please anybody help me in solving this problem
Apple only supports sending sms through the UI they provide. To do so, first Check that you can send a sms. Use NSClassFromString to detect the presence of MFMessageComposeViewController in case you are deploying to an older version of iOS that does not support this, and check that if MFMessageComposeViewController does exist that canSendText return true. It would return false if the user's device does not have sms capability:
if ([[NSClassFromString(@"MFMessageComposeViewController") canSendText]) {
[self presentMessageComposeViewController];
}
If the device and OS support it now present a MFMessageComposeViewController from a view controller in your app. This method should be implement in a subclass of UIViewController that is managing the current view:
-(void) presentMessageComposeViewController {
id smsViewController = [[NSClassFromString(@"MFMessageComposeViewController") alloc] init];
[smsViewController setTitle:NSLocalizedString(@"Your Title",nil)];
[smsViewController setBody:NSLocalizedString(@"Your Message. urls work too www.example.com",nil)];
[smsViewController setMessageComposeDelegate: (id)self];
[self presentModalViewController:smsViewController animated:YES];
[smsViewController release];
}
You will also need to implement the delegate method to dismiss the MFMessageComposeViewController:
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result {
[self dismissModalViewControllerAnimated:YES];
}
If you need to use your own UI, you are going to have to talk to your own server, or a third party service that can send the sms for you. For example, using the Twilio API (and the ASIHTTPRequest project):
NSString *urlString = [NSString stringWithFormat:
@"http://api.twilio.com/2010-04-01/Accounts/%@/SMS/Messages,kYourTwillioSID];
NSURL = [NSURL urlWithString:urlString];
SIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:yourPhoneNumber forKey:@"TO"];
[request setPostValue:@yourBody forKey:@"Body"];
[request setFile:kYourOutgoingTwilioNumber forKey:@"From"];
精彩评论