Xamarin iOS 获取电话状态 [英] Xamarin iOS get status back of a phone call

查看:31
本文介绍了Xamarin iOS 获取电话状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在从应用程序拨打电话后恢复通话状态.我正在使用以下方式拨打电话

Is there a way to get the call status back after a call is made from an app. I am using following to make a call

  NSUrl url = new NSUrl("tel://" + phoneStr);
  UIApplication.SharedApplication.OpenUrl(url);

它显示一个带有取消"和呼叫"按钮的弹出窗口.如果用户取消它会留在应用程序中,如果用户点击呼叫它会发起呼叫.如果用户拨打电话或取消电话,我想获得点击操作.有没有办法获得那个状态

It shows a pop up with "Cancel" and "Call" button. If user cancels it stays back in the app, if user clicks call it initiates a call. I would like to get the click action if user made a call or canceled it. Is there a way to get that status

推荐答案

在 iOS 10+ 上,我们可以使用 CXCallObserver 来捕获用户拨打电话时的事件,例如:

On iOS 10+ We can use CXCallObserver to capture the event when user make a phone call like:

//Make sure both CXCallObserver and ObserverDelegate a strong reference
private CXCallObserver callObserver;
private MyCallObserverDelegate myCallDelegate;

callObserver = new CXCallObserver();
myCallDelegate = new MyCallObserverDelegate();
callObserver.SetDelegate(myCallDelegate, null);

CXCallObserver实现委托:

public class MyCallObserverDelegate : CXCallObserverDelegate
{
    public override void CallChanged(CXCallObserver callObserver, CXCall call)
    {
        Console.WriteLine(call.Outgoing);
        Console.WriteLine(call.HasConnected);
        Console.WriteLine(call.OnHold);
        Console.WriteLine(call.HasEnded);
    }
}

但不幸的是,当用户点击取消时它不会触发.

But unfortunately, it will not fire when user click cancel.

用户点击按钮(取消或呼叫)后,应用程序将触发 DidBecomeActiveNotification,因此我建议您在 DidBecomeActiveNotification 触发时创建延迟方法.然后我们可以检测用户点击了哪个按钮:

After user click the button(cancel or call), the app will fire DidBecomeActiveNotification, so I recommend you create a delay method when DidBecomeActiveNotification fires. Then we can detect which button user clicks:

NSNotificationCenter.DefaultCenter.AddObserver(UIApplication.DidBecomeActiveNotification, async (notification) =>
{
    await Task.Delay(500);
    detectCalling();
});

在早期,我们可以定义一个字段 bool isDialing = false,如果 CallChanged() 触发将其设置为 true.最后我们可以检测detectCalling();中的字段.

In early time we can define a field bool isDialing = false, if CallChanged() fires set it true. At last we can detect the field in detectCalling();.

这篇关于Xamarin iOS 获取电话状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆