Having tough time converting Swift method to C#

Viewed 93

I am writing iOS plugins for Unity. I need to call the function below from Unity. The target function is written in Swift.

How can I convert Swift completion block/closure to C#. What should I write in place of completionHandler?

#if UNITY_IOS
[DllImport("__Internal")]
private static extern void initializeSDK(string name, string apiKey, completionHandler );  
#endif
static func init(name: String?, apiKey: String?, completionHandler: @escaping (Bool) -> Void) {
    // do some stuffs..
    completionHandler(status) // status is of Bool type.
}

also based on suggestions which i have received, i have updated code: updated code as below, i am getting compile errors in ios side. enter image description here enter image description here

enter image description here

enter image description here

2nd suggestion i have tried for which i am getting crash: attaching images of code snip for reference. enter image description here

enter image description here

enter image description here

1 Answers

When dealing with Unity, include a MonoPInvokeCallback attribute decorated method. So, your definition would look like this:

[DllImport("__Internal")]
private static extern void initializeSDK( string name, string apiKey, Action<bool> completionHandler );

And your callback method would look like this:

[MonoPInvokeCallback ( typeof ( Action<bool> ) )]
private static void completionHandler ( bool success )
{
    Debug.Log ( $"completionHandler ({success})" );
}

And to start the ball rolling, you would call your plugin method like so:

initializeSDK("myApp1","122332443",completionHandler);

Full Code:

using UnityEngine;
using System.Runtime.InteropServices;
using System;
using AOT;
  
public static class AwesomeShow
{
#if UNITY_IOS 
    [DllImport("__Internal")]
    private static extern void AwesomeSDK_runNativeCode(string name);
 
    [DllImport("__Internal")]
    static extern long AwesomeSDK_toNumber(string numberString); 
 
    [DllImport("__Internal")]
    private static extern void AwesomeSDK_initializeSDK(string name, string apiKey,  Action<bool> completionHandler); 
#endif

    [MonoPInvokeCallback ( typeof ( Action<bool> ) )]
    private static void completionHandler ( bool success )
    {
        Debug.Log ( "result is: " + success );
    }

    public static void RunNativeCode ( string name )
    {
#if UNITY_IOS
        AwesomeSDK_runNativeCode(name);
        Debug.Log("number is " + AwesomeSDK_toNumber("30")); 
        AwesomeSDK_initializeSDK("myApp1","122332443",completionHandler);
#else
        Debug.Log ( "No ios device" );
#endif
    }
}
Related