10

从我的应用程序拨打电话后,我需要返回我的应用程序,因此我使用以下代码:

NSURL *url = [NSURL URLWithString:@"telprompt://123-4567-890"]; 
[[UIApplication  sharedApplication] openURL:url]; 

当用户按下按钮执行此代码时,会显示带有 2 个按钮的警报,“呼叫”和“取消”。

如何确定用户是否按下了“呼叫”按钮?

4

6 回答 6

12

这是适用于Swift 4.2的iOS 10+解决方案,在 iOS 12 上进行了测试,可以检测到“取消”和“呼叫”按钮。

不要忘记导入 CallKit 并使您的课程符合CXCallObserverDelegate!

let callObserver = CXCallObserver()

var didDetectOutgoingCall = false

func showCallAlert() {
    guard let url = URL(string: "tel:+36201234567"),
        UIApplication.shared.canOpenURL(url) else {
            return
    }

    callObserver.setDelegate(self, queue: nil)

    didDetectOutgoingCall = false

    //we only want to add the observer after the alert is displayed,
    //that's why we're using asyncAfter(deadline:)
    UIApplication.shared.open(url, options: [:]) { [weak self] success in
        if success {
            DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
                self?.addNotifObserver()
            }
        }
    }
}

func addNotifObserver() {
    let selector = #selector(appDidBecomeActive)
    let notifName = UIApplication.didBecomeActiveNotification
    NotificationCenter.default.addObserver(self, selector: selector, name: notifName, object: nil)
}

@objc func appDidBecomeActive() {
    //if callObserver(_:callChanged:) doesn't get called after a certain time,
    //the call dialog was not shown - so the Cancel button was pressed
    DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { [weak self] in
        if !(self?.didDetectOutgoingCall ?? true) {
            print("Cancel button pressed")
        }
    }
}

func callObserver(_ callObserver: CXCallObserver, callChanged call: CXCall) {
    if call.isOutgoing && !didDetectOutgoingCall {
        didDetectOutgoingCall = true

        print("Call button pressed")
    }
}
于 2018-06-23T08:45:46.713 回答
9

这并不完美,但是您可以通过侦听 UIApplicationSuspendedNotification 来识别按下“呼叫”而不是“取消”(当有人按下“home”键时,您必须添加一些逻辑来忽略此事件,或者接受来电...可能通过在显示电话号码的逻辑周围添加/删除观察者):

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(suspended:) name:@"UIApplicationSuspendedNotification" object:nil];

-(void)suspended:(NSNotification *) notification
{
    NSLog(@"Suspended");
}
于 2012-12-06T12:46:49.113 回答
3
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(suspended:) name:@"UIApplicationSuspendedNotification" object:nil];

@J Saprio 的上述代码工作得很好。在它暂停应用程序NSNotificationCenter调用之前UIApplicationSuspendedNotification。单击“调用”后,它将执行该(suspended:)方法。这里要注意的是,每次调用NSNotificationCenter方法时,它都会以(suspended:)1 为增量进行调用。解决方案是删除观察者NSNotificationCenter。以下是它的片段。它将帮助您只执行一次以下方法。

-(void)suspended:(NSNotification *) notification
{
    NSLog(@"Suspended");
   [[NSNotificationCenter defaultCenter] removeObserver:self name:@"UIApplicationSuspendedNotification" object:nil];

}
于 2013-06-17T18:28:40.353 回答
2

根据iOS 条款,这是不可能的。完成通话时无法重定向到应用程序。

只有在越狱手机中才有可能。

于 2012-12-06T12:22:43.677 回答
1

当应用程序执行上述代码时,它将退出当前应用程序并导航到调用 iPhone 的应用程序。因此,应用程序无法识别用户是否按下了呼叫或取消。希望,它会帮助你。

于 2012-12-06T12:24:35.687 回答
0

试试这个,我处理这个问题如下:

添加-(void)viewDidLoad以下内容:

NSNotificationCenter *center = [NSNotificationCenter defaultCenter];

[center addObserverForName:UIApplicationDidBecomeActiveNotification
                    object:nil
                     queue:[NSOperationQueue mainQueue]
                usingBlock:^(NSNotification *note)
 {
     // use instance flag to know
     if (self.isBeginCallPhone)
     {
         //do some thing
         self.isBeginCallPhone = NO;
     }
 }];

别忘了:</p>

- (void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
  }
于 2013-10-30T12:07:10.897 回答