Search code examples
iosobjective-ciphonecallopenurl

How to know call has been made from my application in objective c?


I am new to iOS so pardon me when i make mistake. i want call a person from my application and i wrote these code for that :-

- (IBAction)onClickCallIcon:(id)sender {
    NSString *phoneNumber =_lblLeadMobileNumber.text;
    NSURL *phoneUrl = [NSURL URLWithString:[@"telprompt://" stringByAppendingString:phoneNumber]];
    NSURL *phoneFallbackUrl = [NSURL URLWithString:[@"tel://" stringByAppendingString:phoneNumber]];

    if ([UIApplication.sharedApplication canOpenURL:phoneUrl]){
        [UIApplication.sharedApplication openURL:phoneUrl];
    } else if ([UIApplication.sharedApplication canOpenURL:phoneFallbackUrl]){
        [UIApplication.sharedApplication openURL:phoneFallbackUrl];
    } 
}

and i want to know, Is call has been made or not? if possible how much length of call. how can achieve it?


Solution

  • @Anbu.karthik & @Rocky for helping me. i used allKit/CXCallObserver to observe the call which is the answer of second part of my question and from that part i also get answer of first i.e call is connected or not. by using following code:-

    In viewdidload:

    CXCallObserver *callObserver = [[CXCallObserver alloc] init];
            [callObserver setDelegate:self queue:nil];
            self.callObserver = callObserver;
    

    and a method:

    - (void)callObserver:(CXCallObserver *)callObserver callChanged:(CXCall *)call {
    
    
        if (call.isOutgoing == YES && call.hasConnected == NO) {
            NSLog(@"CXCallState : Dialing");
        }
    
        if (call.isOutgoing == NO  && call.hasConnected == NO && call.hasEnded == NO && call != nil) {
            NSLog(@"CXCallState : Incoming");
        }
    
        if (call.hasConnected == YES && call.hasEnded == NO) {
            NSLog(@"CXCallState : Connected");
            startDate = [NSDate date];
        }
        if (call.hasConnected == YES && call.hasEnded == YES){
            NSLog(@"********** voice call disconnected **********/n");
            endDate = [NSDate date];
            NSDateComponents *components = [[NSCalendar currentCalendar] components: NSCalendarUnitSecond
                                                                           fromDate: startDate toDate: endDate options: 0];
            NSInteger second = [components second];
            NSLog(@"call duration == %ld",(long)second);
            NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
            formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
            [formatter setDateFormat:@"MM/dd/yyyy hh:mm:ss"];
            NSString *startDateFormatted = [formatter stringFromDate: startDate];
            NSString *endDateFormatted = [formatter stringFromDate: endDate];
            NSMutableDictionary *Dict = [[NSMutableDictionary alloc] init];
            [Dict setValue:startDateFormatted forKey:@"startDate"];
            [Dict setValue:endDateFormatted forKey:@"endDate"];
            [Dict setValue:[NSString stringWithFormat:@"%ld", (long)second] forKey:@"interval"];
            [currentShowingData updateCommunications:Dict];
        }
    

    these give me all what i wanted. thanks again who helped me.