Delegate to know when phone call ends which was initiated by app

不想你离开。 提交于 2019-12-12 09:00:08

问题


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.


回答1:


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.




回答2:


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.




回答3:


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;
}


来源:https://stackoverflow.com/questions/23491047/delegate-to-know-when-phone-call-ends-which-was-initiated-by-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!