-2

I want to make phone call using UIWebView. I tried below code which works fine in if I simply put a button and on button click execute below code. But currently on button click, I call an api and on response I execute below code.

// Make a call to given phone number
- (void)callPhoneNumber:(NSString *)phoneNumber
{
    if (!self.webView)
    {
        webView = [[UIWebView alloc] init];
        [self.view addSubview:self.webView];
        self.webView.delegate = self;
    }

    // Remove non-digits from phone number
    phoneNumber = [[phoneNumber componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]] componentsJoinedByString:@""];

    // Make a call
    NSURL * url = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@", phoneNumber]];
    [self.webView loadRequest:[NSURLRequest requestWithURL:url]];
}

Its not even calling webview delegate methods.

What can be the reason?

Please note that I want to call using webview only, so please don't suggest to use native contact app. Using webview keeps flow within the app. When call is ended users is in app only. Using native app, if user wants to come back to my app user has to manually open the app.

Geek
  • 8,280
  • 17
  • 73
  • 137
  • `[NSURLRequest requestWithURL:url]` can never work. – Michael May 08 '14 at 09:54
  • @Michael Why it can never work? – Geek May 08 '14 at 09:56
  • just check out my answer. why do you want to dial the number by calling a method on the webview? it just does not work this way. – Michael May 08 '14 at 09:58
  • duplicate : http://stackoverflow.com/questions/12065498/make-phone-call-on-iphone-and-take-user-back-to-app-uiwebview-does-it – IamAnil May 08 '14 at 09:58
  • @Michael It DOES work in other controller. – Geek May 08 '14 at 10:00
  • I'm not entirely sure what you mean when you say make a call using a `UIWebView`. Do you mean when you select a number in a `UIWebView` it should go to the dialer? – Popeye May 08 '14 at 10:01
  • in the webview, one can just have an ordinary link to some phone number URL, no need to do anything native here. If you want to call it native, use `-[UIApplication openURL:]` – Michael May 08 '14 at 10:03
  • I think there is a lot of confusion here to what you are actually wanting, please can you make your question clearer. – Popeye May 08 '14 at 10:05
  • @Popeye Using webview keeps flow within the app. When call is ended users is in app only. Using native app, if user wants to come back to my app user has to manually open the app. Try the code I have posted. – Geek May 08 '14 at 10:06
  • So all you want is for the user to return automatically to your app when the call is over? If so just use `telprompt://` instead of `tel://` – Popeye May 08 '14 at 10:10
  • @Popeye Also, I have read that `telprompt://` places a call without user confirmation. And apple rejects app that does so. Using webview will ask user for confirmation before placing a call. – Geek May 08 '14 at 10:13
  • @Popeye I tried `NSString *phoneNumberURL = [@"telprompt://" stringByAppendingString: phoneNumber]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumberURL]];`. And its not working, too. `tel://` works but it does not fulfill my requirement. – Geek May 08 '14 at 10:25
  • I've used `telprompt://` before and never had an app rejected. – Popeye May 08 '14 at 10:27
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/52287/discussion-between-geek-and-popeye) – Geek May 08 '14 at 10:28

4 Answers4

0

To dial a phone number you have to call -[UIApplication openURL:]:

NSString *phoneNumber = @"+5512345678";
NSURL *phoneNumberURL = [NSURL URLWithString:[NSString stringWithFormat:@"tel://%@", phoneNumber]];
[[UIApplication sharedApplication] openURL:phoneNumberURL];
Michael
  • 6,451
  • 5
  • 31
  • 53
-1

Why not use this?

NSString *phoneNumberURL = [@"telprompt://" stringByAppendingString: phoneNumber];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumberURL]];

This takes the user back to the app automatically once the call is finished. I hope that is what you want to achieve using webview.

https://stackoverflow.com/a/12065542/569497

Community
  • 1
  • 1
Selvin
  • 12,333
  • 17
  • 59
  • 80
  • `telprompt://` also does not work. But `tel://` does places a call. But thats not what I want because it does not ask for confirmation. – Geek May 08 '14 at 10:27
-1

Are you sure self.webView has been initialized?

Change: webView = [[UIWebView alloc] init];
To: self.webView = [[UIWebView alloc] init];

and this: [NSString stringWithFormat:@"tel:phoneNumber"] don't work; try: [NSString stringWithFormat:@"tel:%@",phoneNumber]

Personally, however, I never tried to make a phone call in this way.

weso
  • 189
  • 3
  • 14
-4

If you want to call via UIWebView then use this example:

+ (void)callWithString:(NSString *)phoneString
{
  [self callWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel:%@",phoneString]]];
}
+ (void)callWithURL:(NSURL *)url
{
  static UIWebView *webView = nil;
  static dispatch_once_t onceToken;
  dispatch_once(&onceToken, ^{  
    webView = [UIWebView new];
  });
  [webView loadRequest:[NSURLRequest requestWithURL:url]];
}
nerowolfe
  • 4,787
  • 3
  • 20
  • 19