首页 > 解决方案 > Xamarin 形成 callkit 集成

问题描述

我正在尝试开发一个 xamarin 表单应用程序,用户可以在其中通过点击应用程序上显示的号码拨打电话(导航到拨号器)。在 android 中,我通过依赖服务完成了此操作。但在 ios 中我被卡住了。我听说过 callkit.I在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);
        }


    }

我的xml.cs

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

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

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

标签: xamarin.forms

解决方案


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

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");
    });

注意:我还没有测试它,因为我没有足够的设备。你可以测试它并让我知道它是否有效。


推荐阅读