Return to app behavior after phone call different in native code than UIWebView

后端 未结 4 1196
感情败类
感情败类 2020-11-27 04:19

According to Apple\'s documentation, in order to make phone call from my app, I need to implement the following protocols:

HTML link:



        
相关标签:
4条回答
  • 2020-11-27 04:46

    Allow me to simplify a bit. All you need is this little snippet:

    UIWebView *callWebview = [[UIWebView alloc] init];
    NSURL *telURL = [NSURL URLWithString:@"tel:number-to-call"];
    [callWebview loadRequest:[NSURLRequest requestWithURL:telURL]];
    

    which I got here.

    *Recently tested successfully on iOS 5.0.

    0 讨论(0)
  • 2020-11-27 04:47

    The Eric's Brotto method still works in 5.1. You have to add the webview to the main view before the loadRequest, like this:

    NSString *cleanedString = [[phoneNumber componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:@"0123456789-+()"] invertedSet]] componentsJoinedByString:@""];
    NSString *escapedPhoneNumber = [cleanedString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *telURL = [NSURL URLWithString:[NSString stringWithFormat:@"tel://%@", escapedPhoneNumber]];
    UIWebView *mCallWebview = [[UIWebView alloc] init]  ;
    [self.view addSubview:mCallWebview];
    [mCallWebview loadRequest:[NSURLRequest requestWithURL:telURL]]; 
    

    (I added a phone number cleaner, to delete any non-number char that blocks this method)

    0 讨论(0)
  • 2020-11-27 04:50
    1. Behavior does differ between calling -[UIApplication openURL:] with a tel: URL, and clicking a link to the same URL in a UIWebView.

    2. Using a UIWebView instead of a UILabel might have some downsides, but you don't have to actually display the UIWebView to get its tel URL handling behavior. Instead, just load a tel URL request in an instance of UIWebView without adding it to your view hierarchy.

    For example:

    #import <Foundation/Foundation.h>
    #import <UIKit/UIKit.h>
    
    @interface PhoneCaller : NSObject
    {
      @private
        UIWebView *webview;
    }
    - (void)callTelURL:(NSURL *)url;
    @end
    
    @implementation
    - (id)init
    {
        self = [super init];
        if (self)
        {
            webview = [[UIWebView alloc] init];
        }
        return self;
    }
    - (void)callTelURL:(NSURL *)url
    {
        [webview loadRequest:[NSURLRequest requestWithURL:url]];
    }
    - (void)dealloc
    {
        [webview release];
        [super dealloc];
    }
    @end
    
    0 讨论(0)
  • 2020-11-27 04:56

    The simplest way seems to be:

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"telprompt:0123456789"]];
    

    You will get a prompt and your app will regain focus after the call is finished.

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