TYPESDK Mobile Game Aggregation SDK Client Design Ideas and Architecture Part 3: iOS Platform Unified Interface Structure and Ideas

In the previous article "TypeSDK Android Platform Unified Interface Structure and Ideas", we explained the interface structure and ideas of the Android platform. Here we will explain the interface structure and ideas under the iOS platform.

       The ios platform is mainly based on the Objective-C language. The overall design idea is similar to that of Android. The details of the specific implementation and the structure of the interface will be slightly different.

       On the ios platform, we will involve part of the C syntax and the mixed compilation of OC and C, and we need to pay more attention to it than Android.

       Well, let's explain in detail the structure of the interface and the design ideas.

       Like Android, let's first look at a few requirements that need to be considered.

1. Relevant needs

       For the unified interface of the iOS platform, we need to consider the following points:

       1. There needs to be a unified interface externally to ensure that different channel SDKs call the same interface and pass the same parameters for the same game

       2. Internally, a framework with good scalability is required to cope with the SDK differences of different channels

2. The designed modules

       So for these considerations, we will design the unified interface of the Android platform in the following parts:

       1. Infrastructure design

       2. Specific channel implementation class

       3. Unified external platform interface

       4. Design of cross-platform interaction

3. Specific details

 
 

We mainly implemented such a set of structures

1. We create a common base abstract class (BaseBonjour).

       Based on the characteristics of oc, we added two protocols to the basic class, one is the basic interface that must be implemented, and the other is the declaration of the extended interface that can not be implemented

       1.1 The basic interface that must be implemented, we define as follows

       1.1.1 Initialization interface  @required -(void)InitSDK:(NSString*)_in_data;

       1.1.2 Login interface @required -(void)ShowLogin:(NSString*)_in_data;

       1.1.3 Logout interface  @required -(void)ShowLogout;

       1.1.4 Payment interface @required -(NSString*)PayItem:(NSString*)_in_data;

       1.1.5 Display share interface @required -(void)ShowShare:(NSString*)_in_data;


       1.2 Extension interfaces that are not required to be implemented

       1.2.1 Get the user information cached in the ios layer @optional -(NSString*)GetUserData;

       1.2.2 Get the configuration of the local channel @optional -(NSString*)GetPlatformData;

       1.2.3 According to the function name, call the function in the concrete implementation class @optional -(NSString*)DoAnyFunction:(NSString*)_funcName withArgs:(NSString*)_json_string;

       1.2.4 Exit the game (kill the process)  @optional -(void)ExitGame;

       1.2.4 Submit player data - (void)SetPlayerInfo:(NSString*)_in_data;

2. We create each channel's own implementation class Bonjour_xx separately according to the sdk of each channel.

       2.1 This class inherits from the common framework base class (BaseBonjour).

       2.2 Under this framework, implement all necessary abstract interfaces of the base class. If there is no function of this interface, corresponding processing is also required, such as outputting log logs.

       2.3 同时该类可以增加渠道自有的特殊接口(例如获取好友列表)

3.发布平台有一个统一的给外部调用接口实现的类:TypeSDK

       3.1该类作为一个单例类,可以给在任何地方方便的提供接口的调用。

       3.2 该类的单利对象是框架基类对象(BaseBonjour)

       3.3 该类不是框架基类对象的子类

       3.4 通过配置文件,利用类名,创建基类对象的子类,该子类是该渠道的具体实现类,该子类赋值给单利对象

       3.5 所有的接口调用,通过获取TypeSDK类的单利对象来调用

具体的实现,可以参照以下一部分代码

单例对象


点击(此处)折叠或打开

  1. static TypeBaseBonjour* sharedInstance = nil;



获取单例对象的静态函数


点击(此处)折叠或打开

  1. @implementation TypeSDK
  2.  
  3. +(TypeBaseBonjour*)GetIns
  4. {
  5.  
  6.     if(nil == sharedInstance)
  7.     {
  8.         [self InitBonjourClass];
  9.     }
  10.     return sharedInstance;
  11. }
  12. @end


具体渠道实现类


点击(此处)折叠或打开

  1. @interface TypeBonjour_demo : TypeBaseBonjour
  2. @end



通过类名动态创建子类对象


点击(此处)折叠或打开

  1. Class _cls = NSClassFromString(_type_name);
  2.    
  3.    if(_cls && [_cls isSubclassOfClass:[TypeBaseBonjour class]])
  4.    {
  5.             NSLog(@"success get bonjour class");
  6.           sharedInstance = [[ _cls alloc]init];
  7.                NSLog(@"share instance address %p",sharedInstance);
  8.     }



这样 我们就可以让基础框架,具体实现,对外调用接口三方的耦合性进一步的降低。这三部分是可以完全各自独立维护。   

4.跨平台交互部分

       跨平台交互我们需要考虑两个方面

              a.如何将从发布平台调用开发平台函数

              b.如何将从开发平台调用发布平台函数

4.1.在ios层,我们只需要将需要给到unity调用的函数

       ios给unity调用,需要写c的接口。c和oc混编,需要吧相关的类文件后缀名从.m修改成.mm

       提供以下代码参考


点击(此处)折叠或打开

  1. @implementation TypeSDKExtern
  2.  
  3. @end
  4.  
  5. #if defined(__cplusplus)
  6. extern "C"
  7. {
  8. #endif
  9.  
  10.     void CallShowLogin ()
  11.     {
  12.        
  13.              [[TypeSDK GetIns]ShowLogin:@""];
  14.     }
  15.    
  16.    
  17. #if defined(__cplusplus)
  18. }
  19. #endif


有关c接口代码的一个注意点

       所有的返回值,需要返回的是常量,所以不能直接把原始oc代码里的char*返回出去,需要拷贝一份常量返回出去

       切记c的代码不要写在oc的类申明里面

另有情提供2个非常有用的小函数


点击(此处)折叠或打开

  1. #if defined(__cplusplus)
  2. extern "C"{
  3. #endif
  4.    
  5.     //字符串转化的工具函数
  6.    
  7.     NSString* SDKCreateNSString (const char* string)
  8.     {
  9.         if (string)
  10.             return [NSString stringWithUTF8String: string];
  11.         else
  12.             return [NSString stringWithUTF8String: ""];
  13.     }
  14.    
  15.     char* SDKMakeStringCopy( const char* string)
  16.     {
  17.         if (NULL == string) {
  18.             return NULL;
  19.         }
  20.         char* res = (char*)malloc(strlen(string)+1);
  21.         strcpy(res, string);
  22.         return res;
  23.     }
  24.    
  25.    
  26. #if defined(__cplusplus)
  27. }
  28. #endif



第一个函数是把 char*类型数据转换成nsstring*

第二个函数是 char*的拷贝函数

在unity中的调用oc层面的c接口举例


点击(此处)折叠或打开

  1. [DllImport("__Internal")]
  2. private static extern void CallShowLogin ();
  3. public void ShowLogin()
  4. {
  5.        CallShowLogin();
  6. }


在cocos2dx中调用c接口举例


点击(此处)折叠或打开

  1. extern void CallShowLogin ();
  2.  
  3. void showLogin()
  4. {
  5.        CallShowLogin();
  6. }


4.2.在unity层,提供了我们通用的跨平台调用接口


点击(此处)折叠或打开

  1. extern void UnitySendMessage(const char *, const char *, const char *);



我们只需要知道unity部分用来接收消息的脚本名字,需要执行的脚本函数名,以及传递的参数,就可以调用unity的响应函数了。

       以下给出调用举例


点击(此处)折叠或打开

  1. -(void)SendEvent:(NSString *)_notify_class_name withJson:(NSString *)_json_string
  2.        {
  3.                UnitySendMessage("TypeSDK" , [_notify_class_name UTF8String], [_json_string UTF8String]);
  4.        }



       综上ios向unity平台传递数据和调用函数,主要通过消息机制发送消息

       unity向安ios平台传递参数和调用函数,则是直接调用oc层的c接口。


       以上就是我们ios发布平台的聚合sdk设计思路细节,下一章我们将讲述unity聚合sdk的设计思路细节。

这个项目已开源,大家有兴趣可以自己研究或者参照项目编写自己的聚合SDK
项目地址:https://code.csdn.net/typesdk_code
项目地址:https://github.com/typesdk

Guess you like

Origin blog.csdn.net/kasimshi/article/details/54693097