赞
踩
MonoPInvokeCallback
从C#层向C层注册回调函数- /// <summary>
- /// 定义 C#调用 ios 原生函数标识 固定写法
- /// </summary>
- private const string DLLName = "__Internal";
- [UsedImplicitly, DllImport(DLLName)]
- private static extern void MobileNumberAuthiOS_Initialize(string secret, AliMobileAuthLogCallback logCallback,
- NumberAuthInitCallback initCallback);
- /// <summary>
- /// 定义c# 传递到原生的的委托代理,接收ios原生回传的值
- /// </summary>
- private delegate void AliMobileAuthLogCallback(string log);
-
- private delegate void NumberAuthInitCallback(int success, string msg);
- /// <summary>
- /// 定义C#端的代理函数,用于接收ios 传递过来的值
- /// </summary>
- /// <param name="log"></param>
- [MonoPInvokeCallback(typeof(AliMobileAuthLogCallback))]
- private static void HandleOnAliMobileAuthLogCallback(string log)
- {
- InfoLog(log);
- }
-
- [MonoPInvokeCallback(typeof(NumberAuthInitCallback))]
- private static void HandleOnSmsAuthInitCallback(int success, string msg)
- {
- InfoLog($"开始初始化回调,success:{success} msg: {msg}");
- _initializeCallback?.Invoke(success, msg);
- }
- /// C# 调用上面定的调用ios 的函数 ,secret 参数,HandleOnAliMobileAuthLogCallback,HandleOnSmsAuthInitCallback 要传递过去的代理函数
- MobileNumberAuthiOS_Initialize(secret, HandleOnAliMobileAuthLogCallback, HandleOnSmsAuthInitCallback);
- ///ios 定义c#调用的ios 方法 固定写法
- #if defined(__cplusplus)
- extern "C" {
- #endif
- void MobileNumberAuthiOS_Initialize(const char* secret ,void(*aliMobileAuthLogCallback)(const char* log), void(*initSDKCallback)(int, const char * )){
- ///将c#的代理指针赋值给ios的块指针
- AliMobileAuthLogCallback = aliMobileAuthLogCallback;
- InitSDKCallback = initSDKCallback;
- ///调用ios的方法
- [[AliMobileNumberAuth sharedManager] Initialize:secret];
- }
-
- #if defined(__cplusplus)
- }
- #endif
-
- ///定义接收c#要传递过来的代理指针
- static void (*AliMobileAuthLogCallback)(const char* log);
- static void(*InitSDKCallback)(int,const char *);
- -(void)Initialize:(const char*)key {
- ///接收c#传递过来的值
- NSString * dataString = [NSString stringWithFormat:@"%s",key];
- ///ios 回调c#的代码函数,将ios这边的值传递到c#
- if (InitSDKCallback) {
- NSString * dataString = @"回传值";
- int code = 1;
- InitSDKCallback(code,[dataString UTF8String]);
- }
-
- }
以上就是unity和ios 原生交互 通过指针传值和代理(delegate)回传值 定义和接收值的完整实现过程
- //ios 代码
- #import "AliMobileNumberAuth.h"
-
-
- ///定义接收c#要传递过来的代理指针
- static void (*AliMobileAuthLogCallback)(const char* log);
- static void(*InitSDKCallback)(int,const char *);
-
-
- static bool EnableLog;
- @interface AliMobileNumberAuth()
- @end
-
- @implementation AliMobileNumberAuth
-
- ///ios 定义c#调用的ios 方法 固定写法
- #if defined(__cplusplus)
- extern "C" {
- #endif
- void MobileNumberAuthiOS_Initialize(const char* secret ,void(*aliMobileAuthLogCallback)(const char* log), void(*initSDKCallback)(int, const char * )){
- ///将c#的代理指针赋值给ios的块指针
- AliMobileAuthLogCallback = aliMobileAuthLogCallback;
- InitSDKCallback = initSDKCallback;
- ///调用ios的方法
- [[AliMobileNumberAuth sharedManager] Initialize:secret];
- }
-
- #if defined(__cplusplus)
- }
- #endif
-
- -(void)Initialize:(const char*)key {
- ///接收c#传递过来的值
- NSString * dataString = [NSString stringWithFormat:@"%s",key];
- ///ios 回调c#的代码函数,将ios这边的值传递到c#
- if (InitSDKCallback) {
- NSString * dataString = @"回传值";
- int code = 1;
- InitSDKCallback(code,[dataString UTF8String]);
- }
-
- }
- #pragma mark - LifeCycle
- +(instancetype)sharedManager {
- static dispatch_once_t onceToken;
- static AliMobileNumberAuth *instance;
- dispatch_once(&onceToken, ^{
- instance = [[AliMobileNumberAuth alloc] init];
- });
- return instance;
- }
- }
-
- using System;
- using System.Runtime.InteropServices;
- using AOT;
- using Bao.Debug;
- using JetBrains.Annotations;
-
- namespace Bao.Account.SDK
- {
- public class MobileNumberAuthiOS
- {
- #region 初始化声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/502963推荐阅读
相关标签
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。