【问题标题】:Xamarin forms callkit integrationXamarin 形成 callkit 集成
【发布时间】:2025-12-01 20:30:01
【问题描述】:

我正在尝试开发一个用户可以拨打电话的 xamarin 表单应用程序 (导航到拨号器)从点击应用程序上显示的数字。在android中我通过依赖服务完成了这个。但是在ios中我被卡住了。我听说过callkit。我在https://docs.microsoft.com/en-us/xamarin/ios/platform/callkit?tabs=windows中看到了它的文档。但是我怎样才能在我的应用程序中实际实现呢?我将该文档中的所有类添加到我的应用程序中。但是如何从 xamal.cs 调用 ios 指定代码?通过使用依赖服务?

编辑:我知道如何将应用导航到拨号器或电话应用。我使用 callkit 的原因是我想获得通话时间。

我创建了一个实例

 public interface IosCallerDialer
    {
        void StartCall();
    }

在 ios 上的实现

class IosCallDial: IosCallerDialer
    {
        private CXCallController CallController = new CXCallController();


        private void SendTransactionRequest(CXTransaction transaction)
        {
            // Send request to call controller
            CallController.RequestTransaction(transaction, (error) => {
                // Was there an error?
                if (error == null)
                {
                    // No, report success
                    Console.WriteLine("Transaction request sent successfully.");
                }
                else
                {
                    // Yes, report error
                    Console.WriteLine("Error requesting transaction: {0}", error);
                }
            });
        }

        public void StartCall()
        {
            // Build call action
            string contact = "8547085532";
            var handle = new CXHandle(CXHandleType.Generic, contact);
            var startCallAction = new CXStartCallAction(new NSUuid(), handle);

            // Create transaction
            var transaction = new CXTransaction(startCallAction);

            // Inform system of call request
            SendTransactionRequest(transaction);
        }


    }

我的 xaml.cs

async void btnCall_Clicked(object sender, System.EventArgs e)
    {        
       DependencyService.Get<IosCallerDialer>().StartCall();      
    }  

除此之外,我添加了文档中定义的所有类。我只想要拨出电话。这是正确的方法吗?我在 xamarin 上找不到任何关于 callkit 的教程。任何帮助表示赞赏。

编辑:我理解 Callkit 仅适用于 voip。那么是否有任何其他解决方法,例如在移动到手机应用程序时启动计时器并在返回应用程序时停止计时器?是否可以?请提供任何见解。

【问题讨论】:

  • 您阅读过 CallKit 的文档吗?它用于 VOIP 集成,而不是与电话应用程序的一般集成。
  • @Jason oh..so 有没有其他方法可以获取 ios 中最后一次通话的通话时长?
  • 我对此表示怀疑。 iOS 并没有真正公开任何具有此类数据的 API
  • @Jason 如果用户打开手机应用程序并在调用后用户将返回应用程序。我可以计算应用程序的非活动时间吗?

标签: xamarin.forms


【解决方案1】:

您可以尝试下面的代码来检测来电的状态。

public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
    //
    // This method is invoked when the application has loaded and is ready to run. In this 
    // method you should instantiate the window, load the UI into it and then make the window
    // visible.
    //
    // You have 17 seconds to return from this method, or iOS will terminate your application.
    //

   public CTCallCenter c { get; set; }

    public override bool FinishedLaunching(UIApplication app, NSDictionary options)
    {
        global::Xamarin.Forms.Forms.Init();
        LoadApplication(new App());

        c = new CTCallCenter();

        c.CallEventHandler = delegate (CTCall call)
        {

            if (call.CallState == call.StateIncoming)
            {
                //start the timer 
            }
            else if (call.CallState == call.StateDialing)
            {

            }
            else if (call.CallState == call.StateConnected)
            {

            }
            else if(call.CallState == call.StateDisconnected)
            {
                //end the timer 
                //use messagecenter to send duartion

                 MessagingCenter.Send<Object>(new Object(), "Hi");

            }

        };

        return base.FinishedLaunching(app, options);
    }
}

以及 Xamarin.forms 中的任何位置:

    MessagingCenter.Subscribe<Object>(this, "Hi", (sender) => {
        // do something whenever the "Hi" message is sent

        Console.WriteLine("hihihi");
    });

注意:我还没有测试它,因为我没有足够的设备。您可以对其进行测试并告诉我它是否有效。

【讨论】:

  • @AndroDevil 在您身边尝试一下,这很容易。我现在无法测试它。
  • 如何在app delelate中实现消息中心?您能否将其包含在上述答案中?
  • 兄弟谢谢。如果我使用它,苹果会在 appstore 中阻止我的应用吗?
  • @AndroDevil 我不认为苹果会拒绝这个。这是一个来自 CoreTelephony 的 api。