如何知道我的应用程序已在 objective c 中进行了调用?

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

我是 iOS 的新手,所以请原谅我的错误。我想从我的应用程序中打电话给一个人,我为此编写了这些代码:-

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

而且我想知道,是否已拨打电话?如果可能的话,通话时长是多少。如何实现?

您可以通过以下方式拨打电话:

  • tel:// 尝试直接拨打 phone 号码
  • telprompt:// 显示确认呼叫的提示

如果你想知道用户按下 Call 按钮或 Cancel 按钮然后可以使用 completionHandler status

Swift代码:

let phoneNumber = "XXXXXXXXXX" // Your phone number here
        if let url = URL(string: "tel://\(phoneNumber)") {
            if #available(iOS 10.0, *) {
                UIApplication.shared.open(url, options: [:], completionHandler: { (status) in
                    if status{
                        print("User Press Call Button")
                    }else{
                        print("User Press Cancel Button")
                    }
                })
            } else {
                // Fallback on earlier versions
            }
        }

Objective-C代码

    NSString *phoneNumber = @"XXXXXXXXXX";
    NSURL *phoneUrl = [NSURL URLWithString:[@"tel://" stringByAppendingString:phoneNumber]];
    NSDictionary *options = [NSDictionary new];
    if ([UIApplication.sharedApplication canOpenURL:phoneUrl])
    {
        [UIApplication.sharedApplication openURL:phoneUrl options:options completionHandler:^(BOOL success) {
            if (success){
                NSLog(@"User Press Call Button");
            }else{
                NSLog(@"User Press Cancel Button");
            }
        }];
    }

对于你的另一个问题“可能的通话时长”,答案是,你无法获得通话时长,这里没有 public API。 请参阅带有 运行 代码的屏幕截图:

查找Attached运行项目

@Anbu.karthik & @Rocky 帮助了我。 我用 allKit/CXCallObserver 来观察电话,这是我问题第二部分的答案,从那部分我也得到了第一部分的答案,即电话是否接通。通过使用以下代码:-

在viewdidload中:

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

和一个方法:

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

这些给了我所有我想要的。再次感谢帮助我的人