0

I have a variable called "sellPhone". I want to add it into the URLWithString. What is proper code to do so?

- (IBAction)buttonCall:(id)sender {

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *sellPhone = [defaults objectForKey:@"sellPhone"];

[[UIApplication sharedApplication]
 openURL:[NSURL URLWithString:@"tel:%",sellPhone]]; //something like this!!!

What is correct way? Thank you!!!

mreynol
  • 309
  • 3
  • 17

1 Answers1

1

[NSURL URLWithString:[NSString stringWithFormat:@"tel:%@",sellPhone]]

To return back to app after call ended: UIWebView *callWebview = [[UIWebView alloc] init]; NSURL *telURL = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@",sellPhone]; [callWebview loadRequest:[NSURLRequest requestWithURL:telURL]];

Kostis
  • 953
  • 9
  • 21
  • You always help me! thanks! It worked! However, the only problem is that it tries to dial some random 4 digit number. I know my number is stored in NSDefaults correct because i see it on other screens. Any idea why that might be? – mreynol Nov 07 '12 at 22:41
  • After reading sellPhone put a NSLog("%@",sellPhone); to ensure it has the number you want. It should work just fine. – Kostis Nov 07 '12 at 22:48
  • Operator error!!! Was reading wrong nsdefault string! haha. You rock once again!!! I like seeing it in the log too. Thank you again! I will owe you a beer one day. hahahaha. – mreynol Nov 07 '12 at 22:58
  • hahaha i'm glad to help! :) It's a good debugging method to add NSLog() and breakpoints to check your code. It helps a lot!! – Kostis Nov 07 '12 at 23:03
  • You wouldn't happen to also know how to get it to return to my app after call completes do you? :) – mreynol Nov 07 '12 at 23:24
  • I edited the first answer..try it and let me know if it works. – Kostis Nov 07 '12 at 23:34
  • - (IBAction)buttonCall:(id)sender { NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSString *phone = [defaults objectForKey:@"phone"]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel:%@",phone]]]; NSLog(@"%@",phone); UIWebView *callWebview = [[UIWebView alloc] init]; NSURL *telURL = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@",phone]]; [callWebview loadRequest:[NSURLRequest requestWithURL:telURL]]; } – mreynol Nov 08 '12 at 03:41
  • Remove the entire [[UIApplication......] command. Also, if you run the app in simulator it won't work as simulator can not make calls. – Kostis Nov 08 '12 at 09:13
  • Ok, so i'm running on my iPhone...but its not launching the phone app now. I get the right number in my NSLog but it won't make the call. – mreynol Nov 09 '12 at 02:30
  • Here's what i got: - (IBAction)buttonCall:(id)sender { NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSString *phone = [defaults objectForKey:@"phone"]; [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@",phone]]; UIWebView *callWebview = [[UIWebView alloc] init]; NSURL *telURL = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@",phone]]; [callWebview loadRequest:[NSURLRequest requestWithURL:telURL]]; NSLog(@"%@",phone); } – mreynol Nov 09 '12 at 02:31