本文共 2402 字,大约阅读时间需要 8 分钟。
Unity3d
项目开发,在iOS
平台接SDK
的时候,就需要实现自己的 AppController
来维护 SDK
的生命周期。Unity3d
提供了一套插件机制,可以很方便地在项目中使用自己的CustomAppController
继承并重写默认的UnityAppController
的方法。
Assets/Plugins/iOS/CustomAppController.mm
文件名必须是 xxxxAppController.mm
,xxxx
前缀可自选,但不能省略,如CustomAppController.mm
;
Build
项目的时候,会被移动到错误的目录中去。 // CustomAppController.mm#import "UnityAppController.h"@interface CustomAppController : UnityAppController@endIMPL_APP_CONTROLLER_SUBCLASS (CustomAppController)@implementation CustomAppController- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions{ [super application:application didFinishLaunchingWithOptions:launchOptions]; return YES;}@end
注意,上面的CustomAppController.mm
中有一个宏IMPL_APP_CONTROLLER_SUBCLASS
,Unity 就是通过 IMPL_APP_CONTROLLER_SUBCLASS
知道要使用我们定制的 CustomAppController
而不是使用默认的UnityAppController
。
极光推送SDK为例(SDK官方文档:),定制CustomAppController
// CustomAppController.mm#import "JPUSHService.h"#import "JPushEventCache.h"#import// 如需使用广告标识符 IDFA 则添加该头文件,否则不添加。#import #import "UnityAppController.h"@interface CustomAppController : UnityAppController@endIMPL_APP_CONTROLLER_SUBCLASS (CustomAppController)@implementation CustomAppController- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [[JPushEventCache sharedInstance] handFinishLaunchOption:launchOptions]; /* 不使用 IDFA 启动 SDK。 参数说明: appKey: 极光官网控制台应用标识。 channel: 频道,暂无可填任意。 apsForProduction: YES: 发布环境;NO: 开发环境。 */ [JPUSHService setupWithOption:launchOptions appKey:@"b8213ec93d03ff11a560f516" channel:@"" apsForProduction:NO]; return YES;}- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { // Required. [JPUSHService registerDeviceToken:deviceToken];}- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { // Required. [[JPushEventCache sharedInstance] sendEvent:userInfo withKey:@"JPushPluginReceiveNotification"]; [JPUSHService handleRemoteNotification:userInfo];}- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler { [[JPushEventCache sharedInstance] sendEvent:userInfo withKey:@"JPushPluginReceiveNotification"];}@end
转载地址:http://zxzvf.baihongyu.com/