Practice Sharing: Running WeChat Mini Games in Your Own App (iOS)

The long-awaited began to come out! FinClip finally supports mini games.

Our team is an old user of FinClip. At the beginning of the year, we proposed to the official that FinClip support WeChat mini games. With the explosion of the WeChat mini-game "Goat a Sheep" some time ago, the official has also put the mini-game support on the agenda, and recently the official opened the public beta channel. We tested it with a small game over the weekend, and recorded and shared the test situation.

At present, FinClip is conducting an internal test of mini-game-related functions, and interested friends can go and have a look. Manual guide: internal testing of mini-game related functions

Step 1: Obtain credentials (SDK KEY and SDK SECRET)

First of all, to integrate the SDK, you need to create an application on the FinClip platform and bind the Mini Program game application. After obtaining the SDK KEY and SDK SECRET specific to each application, you can then fill in the corresponding parameters when integrating the SDK. When the applet is opened, the SDK will automatically initialize and verify whether the SDK KEY, SDK SECRET and BundleID (Application ID) are correct. Only when this step is correct can the initialization succeed and be used normally.

1.1 Create Mini Program Game

You need to log in to  the FinClip management background "Application Management - Add Cooperative Application" to complete the creation of the mini game application;

1.2 Get SDK KEY and SDK SECRET

After creating an application and adding a Bundle ID, select "Copy" after the corresponding Bundle ID to export the corresponding SDK KEY and SDK SECRET.

  • SDK KEY: It is the certificate that the cooperative application can use the Mini Program SDK. If the SDK Key verification fails, all APIs of the SDK will not be available.
  • SDK SECERT: It is a security certificate for accessing services.

Step 2: Integrate the SDK

The FinClip applet SDK currently supports pod integration or manual integration. We used pod integration for this test.

2.1.1 Install pod environment

Cocoapods provides a very simple dependency management system, which is very convenient to avoid errors caused by manual import. Official Installation Guide (English) (opens new window)  or  CocoaPods Installation Tutorial (Chinese) (opens new window) .

sudo gem install cocoapods
pod setup

2.1.2 Create Podfile

If you don't need to use the extended SDK,  podfile just depend on it  FinApplet . If you need to use the API in the extension SDK, then you also need dependencies  FinAppletExt. For example: if you need to use the Bluetooth function in the applet, you can  podfile add  FinAppletBLE dependencies in it;

For the specific operation method of the set, you can check the official documentation in detail. How iOS introduces an SDK

2.1.3 Install or update dependencies

Then, execute  pod update or  pod install .

2.1.4 Open project

After execution  pod update or  pod install , open the project directory, find  xxx.xcworkspace the file, and double-click to open it.

3. Add the SDK header file

Where the FinClip applet SDK needs to be used, add the following code:

#import <FinApplet/FinApplet.h>

If the extension SDK is also integrated, then the following code needs to be added to call the api in the extension SDK:

#import <FinAppletExt/FinAppletExt.h>

Of course, the most convenient way is to add the above code in the pch file, so that there is no need to quote it when it is used.

4. Initialize the SDK

AppDelegate In the following method in the project  , call the initialization method of SDK.

NSMutableArray *storeArrayM = [NSMutableArray array];
FATStoreConfig *storeConfig = [[FATStoreConfig alloc] init];
storeConfig.sdkKey = @"您的sdkKey信息";
storeConfig.sdkSecret = @"您的sdkSecret信息";
storeConfig.apiServer = @"服务器域名";
storeConfig.apmServer = @"apm统计事件的域名";
[storeArrayM addObject:storeConfig];
    
FATStoreConfig *storeConfig2 = [[FATStoreConfig alloc] init];
storeConfig2.sdkKey = @"您的sdkKey信息";
storeConfig2.sdkSecret = @"您的sdkSecret信息";
storeConfig2.apiServer = @"服务器域名";
storeConfig2.apmServer = @"apm统计事件的域名";
storeConfig2.cryptType = FATApiCryptTypeSM;
[storeArrayM addObject:storeConfig2];
 
FATConfig *config = [FATConfig configWithStoreConfigs:storeArrayM];
[[FATClient sharedClient] initWithConfig:config error:nil];

This test requires the ability to log in with WeChat and obtain user information, so the registration component needs to be initialized.

// 微信扩展SDL初始化
[FATWXExtComponent registerComponent:@"微信appid" universalLink:@"universalLink"];

And  AppDelegate.m add the following code in it.

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
    /*  微信登录和分享    */
    if ([WXApi handleOpenURL:url delegate:[FATWXApiManager sharedManager]]) {
        return YES;
    }
    return YES;
}
 
// iOS 9.0 之前
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
    /*  微信登录和分享    */
    // `WeChatHandleURLDelegate ` 为 `WXApiDelegate`代理文件
    if ([WXApi handleOpenURL:url delegate:[FATWXApiManager sharedManager]]) {
        return YES;
    }
    return YES;
}
 
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{
    /*  微信登录和分享    */
    if ([WXApi handleOpenURL:url delegate:[FATWXApiManager sharedManager]]) {
        return YES;
    }
    return YES;
}
 
- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray<id<UIUserActivityRestoring>> * _Nullable))restorationHandler {
  return [WXApi handleOpenUniversalLink:userActivity delegate:[FATWXApiManager sharedManager]];
}

Five, handleOpenURL processing

Generally speaking, small games need to support external links to open for easy sharing. You need to do the following.

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    if ([[FATClient sharedClient] handleOpenURL:url]) {
        return YES;
    }
    return YES;
}
 
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
{
    if ([[FATClient sharedClient] handleOpenURL:url]) {
        return YES;
    }
    return YES;
}

6. Open the applet

FATAppletRequest *request = [[FATAppletRequest alloc] init];
request.appletId = @"小游戏id";
request.apiServer = @"服务器地址";
request.transitionStyle = FATTranstionStyleUp;
request.startParams = startParams;
     
[[FATClient sharedClient] startAppletWithRequest:request InParentViewController:self completion:^(BOOL result, FATError *error) {
    NSLog(@"打开小游戏:%@", error);
} closeCompletion:^{
    NSLog(@"关闭小游戏");
}];

The opening effect is as follows:

 This test uses the demo provided by the official. The overall process ran relatively smoothly, and there were no special stuck places. Currently, the official is conducting internal testing. Interested friends can go to the official platform to have a look. Mini Game Development Guide

Next, for the Android system, we plan to use our own mini-game to run the process again, and then share it with you.

Guess you like

Origin blog.csdn.net/FinoHedwig/article/details/128402784