Question

I have a code which places a phone call using below code :

// Make a call to given phone number
- (void)callPhoneNumber:(NSString *)phoneNumber
{
    if (!self.webview)
        self.webview = [[UIWebView alloc] init];
    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]];
    [self.view addSubview:self.webview];
}

This makes a call. What I want is, I want to know when user ends a call. I have to perform an operation when user ends a call. Is there any way for it?

What I tried is, I set delegate of webview to current controller. But none of the delegate methods is called.

- (void)webViewDidStartLoad:(UIWebView *)webView
{
    DLog(@"Start Loading");
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    DLog(@"Finish Loading");
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    DLog(@"Did fail with error : %@", error);
}

I don't know if you need this info but I use webview so that when phone call is made, flow remains within the app and on call end, app screen is displayed rather than user manually coming to app from native contact app.

Was it helpful?

Solution

CoreTelephony framework has a CTCallCenter Class with callEventHandler property.

@property (nonatomic, copy) void (^callEventHandler)(CTCall*);

You will have to define a handler block in your application and assign it to this property. If your application is active when a call event takes place, the system dispatches the event to your handler immediately when call state changes. Refer apple documents found here.

OTHER TIPS

I used below code to get notified of call events.

// Create CTCallCenter object
callCenter = [[CTCallCenter alloc] init];
// Assign event handler. This will be called on each call event
self.callCenter.callEventHandler = ^(CTCall* call) {

    // If call ended
    if (call.callState == CTCallStateDisconnected)
    {
        NSLog(@"Call ended.");
    }
};

For other call states check out Call States. For more info on CTCallCenter look at Apple doc for CTCallCenter.

You should be implementing this delegate method of UIWebView

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;

on end call operation your webview will notify this delegate method about your action perform and you can handle it in there for example

-(BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    if (navigationType == UIWebViewNavigationTypeLinkClicked )
    {
        NSURL *url = [request URL];
        NSLog(@"URL ===== %@",url);
        // you can check your end call operation URL and handle it accordingly
        if ([[url absoluteString] rangeOfString:@"#"].location == NSNotFound)
        {
            [[UIApplication sharedApplication] openURL:[request URL]];
            return NO;
        }
        //return NO;

    }
    return YES;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top