Skip to content

初始化

WARNING

初始化接口,需要在游戏启动时,尽快调用, 否则影响后续接口的调用。

引入库文件

所有接口调用,都通过UGSDKPlatform 单例类来调用。 在调用接口之前,需要先引入头文件:

objc
#import <UGSDK/UGSDK.h>

设置SDK回调代理

当初始化、登录、登出、支付等接口调用之后, 响应的调用结果,SDK会触发UGSDKDelegate中的对应的回调函数, 您可以实现UGSDKDelegate来监听对应的回调函数,并针对性地处理业务逻辑。

点击查看代码
objc

// 初始化成功后回调
-(void) onUGInitSuccess{
    NSLog(@"sdk init success");
}

// 初始化失败后回调
-(void) onUGInitFailed:(NSString*)msg{
    NSLog(@"sdk init failed.%@", msg);
}

// 登录成功后回调
-(void) onUGLoginSuccess:(UG_UserDataModel*)result{
    NSLog(@"sdk login success: %@", result);
}

// 游客账号升级成功
-(void) onUGVisitorUpgradeSuccess:(UG_UserDataModel*)result{
    NSLog(@"sdk bind success: %@", result);
}

// 登录失败后回调
-(void) onUGLoginFailed:(NSString*)msg{
    NSLog(@"sdk login failed: %@", msg);
}

// 登出成功后回调
- (void)onUGLogoutSuccess{
    NSLog(@"logout from sdk");

}

// 登出失败后回调
-(void) onUGLogoutFailed:(NSString*)msg{
    NSLog(@"sdk logout failed");
}

// 支付成功后回调
-(void) onUGPaySuccess:(NSString*)msg{
    NSLog(@"sdk pay success");
}

// 支付失败后回调
-(void) onUGPayFailed:(NSString*)msg{
    NSLog(@"sdk pay failed: %@", msg);
}

// 查询商品信息回调
-(void) onProductsResult:(nullable NSArray<UG_Product*> *) products
{
    NSLog(@"onProductsResult:%@", products);
    
    if (products) {
        for (UG_Product* item in products) {
            NSLog(@"product id:%@; localized price:%@", item.localProductID, item.localePrice);
        }
    }
}

调用初始化接口

调用初始化接口,一般在游戏启动的时候调用。 后续其他接口的调用都必须在初始化接口调用之后进行。

objc
NSString *appID = @"1";
NSString *appKey = @"11111111111111";
NSString * googleIOSClientID = @"976037278824-icimes2m5c8c11n444mf5n25ros4nr62.apps.googleusercontent.com";
UG_SDKParams* params = [[UG_SDKParams alloc] initWithAppID:appID appKey:appKey orientation:@"landscape" googleCientID:googleIOSClientID];
[[UGSDKPlatform sharedInstance] initWithParams:params delegate:self]; //第二个delegate参数,就是实现了上面UGSDKDelegate的类

初始化参数说明:

参数名称参数类型参数说明
appIDString当前游戏的appID参数,如果还没有该参数,请参考:获取参数
appKeyString当前游戏的appKey参数,如果还没有该参数,请参考:获取参数
orientationString游戏横竖屏,portrait:竖屏; landscape:横屏
googleCientIDStringGoogle Api Console控制台创建的iOS客户端凭据后生成的clientID

注意:为了方便游戏层修改SDK参数, 也提供了另一个不带参数配置项的初始化接口:

objc
// SDK的参数配置,读取UGSDK_Images.bundle/ug_config.plist文件中配置的
[[UGSDKPlatform sharedInstance] initWithDelegate:self];     //delegate参数,就是实现了上面UGSDKDelegate的类

使用该初始化接口,参数读取UGSDK_Images.bundle/ug_config.plist文件中的配置项。 该文件配置内容如下:

plist
<plist version="1.0">
<dict>
    <key>ug.appid</key>
    <string>3</string>
    <key>ug.appkey</key>
    <string>982d9834774b48c5bc4933116aa9db4b</string>
    <key>ug.orientation</key>
    <string>landscape</string>
    <key>ug.server.url</key>
    <string>http://ugapi.bytesdk.com</string>
    <key>ug.google.ios.clientid</key>
    <string>976037278824-icimes2m5c8c11n444mf5n25ros4nr62.apps.googleusercontent.com</string>
</dict>
</plist>

配置项说明:

参数名称参数类型参数说明
ug.appidString当前游戏的appID参数,如果还没有该参数,请参考:获取参数
ug.appkeyString当前游戏的appKey参数,如果还没有该参数,请参考:获取参数
ug.orientationString游戏横竖屏,1:竖屏; 2:横屏
ug.server.urlStringSDK服务端URL地址
ug.google.ios.clientidStringGoogle Api Console控制台创建的iOS客户端凭据后生成的clientID

实现生命周期函数

需要在AppDelegate以下对应的生命周期方法中调用U8SDK种对应这些方法:

点击查看代码
objc
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    return [[UGSDKPlatform sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions];
} 

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url options:(nonnull NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options {
    return [[UGSDKPlatform sharedInstance] application:application openURL:url options:options];
}
 
-(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
    return [[UGSDKPlatform sharedInstance] application:application handleOpenURL:url];
}

-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    
    return [[UGSDKPlatform sharedInstance] application:application openURL:url sourceApplication:sourceApplication annotation:annotation];
}

-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    [[UGSDKPlatform sharedInstance] application:application didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(nonnull NSDictionary *)userInfo
{
    [[UGSDKPlatform sharedInstance] application:application didReceiveRemoteNotification:userInfo];
}

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
    [[UGSDKPlatform sharedInstance] application:application didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
}

-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    [[UGSDKPlatform sharedInstance] application:application didReceiveLocalNotification:notification];
}

- (void)applicationWillResignActive:(UIApplication *)application
{
    [[UGSDKPlatform sharedInstance] applicationWillResignActive:application];
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    [[UGSDKPlatform sharedInstance] applicationDidEnterBackground:application];
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    [[UGSDKPlatform sharedInstance] applicationWillEnterForeground:application];
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    [[UGSDKPlatform sharedInstance] applicationDidBecomeActive:application];
}

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

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