简体   繁体   中英

how do i launch the iphone sms text application in objective-c via the sms: url scheme?

I have a button in my obj-c application that I would like to launch the iphone text application when a button is pressed.

i've looked at the solution here How to use iPhone Custom URL schemes and have attached an action to my button (via the 'touch up inside' event), but the text app doesn't launch when the button is pressed.

here is my code

(IBAction)sendMsgBtnPressed:(id)sender {

    NSLog(@"sendMsgBtnPressed");

    NSString *stringURL = @"sms:+14155551212";
    NSURL *url = [NSURL URLWithString:stringURL];
    [[UIApplication sharedApplication] openURL:url];

    [stringURL release];
}

i know this is being called because i can see the NSLog() output in my console. When I use a http:// scheme it works fine & launches Safari but sms: does not appear to work. Any idea what I am missing here?

Try removing the leading plus sign before the numbers in your url. The plus sign is used for international numbers and you can safely replace it with two leading zeroes. Moreover, be sure to provide a string in which no space occurs between numbers: something like

NSString *stringURL = @"sms:00141555 51212"; will NOT work correctly.

One more thing. The statement

[stringURL release];

is not correct because stringURL has not being retained. You should remove it from your method.

Notice that the SMS application will be opened ONLY in iPhone device - not simulator and not iPod Touch.

The SMS application doesn't exist on simulator and iPod Touch...

您还可以检查设备是否可以通过以下方式调用SMS应用程序:

[[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:stringURL]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM