Skip to content

CocosCreator接入-iOS平台配置

WARNING

在完成Cocos层API调用之后, 如果要出iOS平台的ipa包,还需要根据本文档做一些额外的配置

准备工作

CocosCreator中接入好api后,我们可以编译发布,导出XCode工程了。我们可以从SDKDemo中,将一些原生平台的资源文件拷贝到游戏工程:

1、SDKDemo/native/engine/ios/U8SDK: SDK的依赖库文件,拷贝到游戏工程导出的AS工程对应目录下;
2、SDKDemo/native/engine/ios/U8SDK/U8SDKWrapper.mm: 该文件封装了iOS层SDK的调用;
3、SDKDemo/native/engine/ios/U8SDK/U8SDKForCocos.mm: 该文件处理iOS原生平台和cocos层的交互;

导出工程

完成Cocos层的接入后,可以导出iOS工程。 导出工程后,将U8SDK目录拖入XCode工程中(添加到cocos-mobile这个target),之后可能需要做如下处理:

bash

1、 Build Settings中, Other Link Flags中, 增加-ObjC
2、 Build Settings中, Header Search Path中, 增加U8SDK的路径(要不然可能会出现U8SDKWrapper.h/U8SDKForCocos.h文件找不到的错误)

处理生命周期函数

打开XCode工程/native/engine/ios/AppDelegate.mm文件, 在里面生命周期函数中,增加UGSDK相关的调用:

点击查看生命周期函数的调用
objc
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [[SDKWrapper shared] application:application didFinishLaunchingWithOptions:launchOptions];
    appDelegateBridge = [[AppDelegateBridge alloc] init];
    
    ...
    

    // SDK添加
    // 初始化SDK,并调用didFinishLaunchingWithOptions生命周期函数
    [U8SDKForCocos initSDK];
    [[U8SDK sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions];

    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    /*
     Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
     Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
     */
    [[U8SDK sharedInstance] applicationWillResignActive:application];
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    /*
     Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
     */
    [[U8SDK sharedInstance] applicationDidBecomeActive:application];
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    /*
     Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
     If your application supports background execution, called instead of applicationWillTerminate: when the user quits.
     */
    [[U8SDK sharedInstance] applicationDidEnterBackground:application];
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    /*
     Called as part of  transition from the background to the inactive state: here you can undo many of the changes made on entering the background.
     */
    [[U8SDK sharedInstance] applicationWillEnterForeground:application];
}

- (void)applicationWillTerminate:(UIApplication *)application {
    [[U8SDK sharedInstance] applicationWillTerminate:application];
}

- (BOOL)application:(UIApplication*)application openURL:(NSURL*)url sourceApplication:(NSString*)sourceApplication annotation:(id)annotation
{
    [[U8SDK sharedInstance] application:application openURL:url sourceApplication:sourceApplication annotation:annotation];
}
- (BOOL)application:(UIApplication *)app openURL:(NSURL*)url options:(NSDictionary *)options
{
    [[U8SDK sharedInstance] application:app openURL:url options:options];
}

- (BOOL)application:(UIApplication*)application handleOpenURL:(NSURL *)url
{
    [[U8SDK sharedInstance] application:application handleOpenURL:url];
}

XCode工程配置

用XCode打开工程后, 还需要进行一些原生平台的配置, 方能出包测试。 这部分可以参考: iOS接入配置

版权所有© 2021-2030 上海丞诺网络科技有限公司