我是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];
}
}
我想知道,是否已拨打电话?如果可能的话,呼叫的长度。怎么能实现呢?
答案 0 :(得分:0)
您可以通过以下方式拨打电话:
如果您想知道用户按Call
按钮或Cancel
按钮,则可以使用completionHandler
status
。
Swift Code :
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");
}
}];
}
对于您的另一个问题" 可能的通话长度",答案是否,您无法获得通话时长,没有公共API。 请参阅运行代码的屏幕截图:
查找Attached正在运行的项目
答案 1 :(得分:0)
@ 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];
}
这些给了我想要的一切。再次感谢谁帮助了我。