Make phone call on iPhone and take user back to app? (UIWebView does it)

前端 未结 2 1283
说谎
说谎 2020-12-11 11:46

I used this code to make phone call:

NSString *phoneNumber = [@\"tel://\" stringByAppendingString:mymobileNO.titleLabel.text];
[[UIApplication sharedApplicat         


        
相关标签:
2条回答
  • 2020-12-11 12:02

    The website: http://www.raizlabs.com/dev/2014/04/getting-the-best-behavior-from-phone-call-requests-using-tel-in-an-ios-app/ has a discussion of whether to use telprompt (undocumented, Apple could potentially change the API without notice), and instead using a category that sends the number to a web view which opens it using telprompt. This has the advantage of not breaking if Apple does something odd.

    0 讨论(0)
  • 2020-12-11 12:15

    You need to use the telprompt URL, not tel.

    So:

    NSString *phoneNumber = [@"telprompt://" stringByAppendingString:mymobileNO.titleLabel.text];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
    

    This will also give the user a confirmation box before calling the number.

    Edit:

    This question covers the same issue.

    Edit 2:

    For those wondering if this URL will result in rejection from the App Store, the answer is generally no. The greater risk is that Apple will suddenly stop supporting the telprompt scheme. As explained by this article, there is a slightly 'safer' way of implementing telprompt with UIWebView (which uses telprompt internally, even if you call it with tel). The relevant code from the article shows how using the documented tel scheme can still give you the effect of telprompt:

    + (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]];
    }
    

    (Code taken from the article reference in my second edit)

    0 讨论(0)
提交回复
热议问题