Skip to content

Mini Program Management

Mini program management mainly introduces the API for operating mini programs, including: open mini program, close mini program, search mini program, etc.

1. Open the mini program

Different scenarios use different api's to open mini programs. Therefore, we provide several different api for opening mini programs.

  1. open the online mini program, here generally only need the mini program id and server address can be, the api can only open the official version and audit version of the mini program.
  2. open the mini program by QR code, this scenario is to scan the QR code on the mini program platform, get the content in the QR code, and then use the content to open the mini program. The QR code of the mini program of the official version, experience version, audit version, development version and preview version can be opened with this Api.
  3. URL Scheme open mini program, this scenario is to embed URL Scheme URI in H5 web page, which triggers to open mini program in App, only supports to open official version of mini program on the shelf.

1.1 Normal open mini program

When opening the mini program, it will first determine whether there is a cached mini program locally, if not, it will automatically download the mini progmini from the remote server and then open it; if there is a cached mini program, it will first open the local mini program and then check whether there is a new version on the server side.

If there is a new version, the new version of the mini program will be downloaded and the next time it is opened, the new version of the mini program will be used; if there is no new version, nothing will be done.

objective-c
/// Launch the mini program
/// @param request Start of the request
/// @param parentVC Parent Page
/// @param completion Complete the callback
/// @param closeCompletion Callbacks when closing mini programs
- (void)startAppletWithRequest:(FATAppletRequest *)request
        InParentViewController:(UIViewController *)parentVC
                    completion:(void (^)(BOOL result, FATError *error))completion
               closeCompletion:(dispatch_block_t)closeCompletion;

FATAppletRequest

AttributetypeDescription
appletIdNSStringmini program id, required
apiServerNSStringThe server to which the mini program belongs, required
appNameNSStringmini program name, not required
appletLogoNSStringNetwork address of the mini program icon, not required
startParamsNSDictionarymini program start parameter, supported key, please refer to FATStartParamKey, not required
transitionStyleFATTranstionStyleThe transition animation method when opening the mini program, not required, the default value is FATTranstionStyleUp
animatedBOOLWhether to use animation, not required, the default value is YES
sequenceNSNumbermini program index, not required
offlineMiniprogramZipPathNSStringOffline mini program zip path, you can pass in a local mini program package path to speed up the first start, not required
offlineFrameworkZipPathNSStringOffline base library zip path, you can pass in a base library path to speed up the first start, not required

Note: Both offlineMiniprogramZipPath and offlineFrameworkZipPath must be passed if you want to start offline for the first time.

Example code:

objective-c
FATAppletRequest *request = [[FATAppletRequest alloc] init];
request.appletId = @"Applet id"; // Required fields
request.apiServer = @"Server Address"; // Required fields
request.transitionStyle = FATTranstionStyleUp;
request.startParams = startParams; // Launch parameters of the mini program

[[FATClient sharedClient] startAppletWithRequest:request InParentViewController:self completion:^(BOOL result, FATError *error) {
    NSLog(@"Open the applet:%@", error);
} closeCompletion:^{
    NSLog(@"mini program release process");
}];

1.2 QR code to open the mini program

In this case, the process is generally more complicated, you need to scan the QR code on the FinClip mini program open platform, get the content of the QR code, and then use the QR code content to call the interface to open the mini program.

objective-c
/// QR code information to start the mini program
/// @param request Request object
/// @param parentVC Parent page
/// @param requestBlock A callback to verify the request completion of the QR code
/// @param completion Completed callbacks
/// @param closeCompletion A callback when closing an mini program
- (void)startAppletWithQrCodeRequest:(FATAppletQrCodeRequest *)request
              inParentViewController:(UIViewController *)parentVC
                        requestBlock:(void (^)(BOOL result, FATError *error))requestBlock
                          completion:(void (^)(BOOL result, FATError *error))completion
                     closeCompletion:(dispatch_block_t)closeCompletion;

FATAppletQrCodeRequest

AttributetypeDescription
qrCodeNSStringQR code content, required
transitionStyleFATTranstionStyleThe transition animation method when opening the mini program, not required, the default value is FATTranstionStyleUp
animatedBOOLWhether to use animation, not required, the default value is YES

Example code:

objective-c
FATAppletQrCodeRequest *qrcodeRequest = [[FATAppletQrCodeRequest alloc] init];
qrcodeRequest.qrCode = qrCode;

[[FATClient sharedClient] startAppletWithQrCodeRequest:qrcodeRequest inParentViewController:self requestBlock:^(BOOL result, FATError *error) {
    NSLog(@"The request is complete:%@", error);
} completion:^(BOOL result, FATError *error) {
    NSLog(@"Open Finish:%@", error);
} closeCompletion:^{
    NSLog(@"close");
}];

1.3 Open mini programs using URL Scheme

Sometimes we want to be able to open our mini programs from a safari-loaded web page or from a third-party app, so we can use URL Scheme to open the mini programs from our own app.

First, you need to add a URL Type, select Target -> [Info] -> [URL Types] and add a URL Schemes. The format of URL Schemes is md5 of fat+sdkKey, see the following example The format of URL Schemes is the md5 of fat+sdkKey, see the following example The format of URL Schemes is the md5 of fat+sdkKey, see the following example

Then, implement the OpenURL proxy method in AppDelagate. The sample code is as follows.

objective-c
- (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;
}

Finally, just add the Scheme link in the H5 page. The format of the link: fat{md5 of sdkKey}://applet/appid/{applet id}. The example is as follows.

<a href='fat705b46f78820c7a8://applet/appid/5e017a61c21ecf0001343e31'>打开小程序</a>

Of course, you can also type the full url (e.g. fat705b46f78820c7a8://applet/appid/5e017a61c21ecf0001343e31) into the address bar of your safari browser, then enter and your App will open and launch the mini program.

2. Close the mini program

Closing the mini program is equivalent to Clicking the mini program in the capsule in the upper right corner, at this time the mini program is retired to the background and the mini program is not destroyed.

2.1 Close the specified mini program

objective-c
/**
Close the opened specified mini program
@param animated Whether to show animation
@param completion Callback for closing completion
*/
- (void)closeApplet:(NSString *)appletId animated:(BOOL)animated completion:(dispatch_block_t)completion;

2.2 Close all mini programs

In some scenarios, mini program A may open mini program B, and mini program B opens mini program C. If you want to close all the open mini programs, you can use this method.

objective-c
/**
Close all currently open mini programs
@param completion Callback for closing completion
*/
- (void)closeAllAppletsWithCompletion:(dispatch_block_t)completion;

2.3 Close the current mini program

objective-c
/**
Close the current mini program
@param animated Whether to show animation
@param completion Callback for closing completion
*/
- (void)closeCurrentApplet:(BOOL)animated completion:(dispatch_block_t)completion;

3. End the mini program

When an mini program is closed, it doesn't really end, but hangs in the background. When the mini program is opened next time, it will immediately switch the mini program to run in the foreground. So, if we want the mini program to be closed and really ended, we can use the following API to end the specified mini program or all mini programs, depending on the actual situation.

3.1 End the specified mini program

After the mini program is closed, call the api to delete the cache and destroy the mini program.

objective-c
/**
Delete the specified mini program from memory
*/
- (void)clearMemeryApplet:(NSString *)appletId;

3.2 End all mini programs

This api can be used when you have opened and closed multiple mini programs and want to end all the mini programs hanging in the background at once.

objective-c
/**
 Clear all mini programs from memory cache
 */
- (void)clearMemoryCache;

4. Delete mini program

As the mini program is running, it will cache the mini program package and mini program information locally, and the cache will be used first when it is opened later, so it will be fast when it is opened. So, if you want to delete all the information of an mini program, you can use the following api to delete an apmini programplet or delete all the mini programs.

4.1 Delete the specified mini program

objective-c
/**
 Delete specified mini program from local storage

 @param appletId mini program id
 @return BOOL Results
 */
- (BOOL)removeAppletFromLocalCache:(NSString *)appletId;

4.2 Delete all mini programs

objective-c
///  Delete local cache of mini programs
- (void)clearLocalApplets;