# Offline (Local) Mini-Program

Most operations for local Mini-Programs are the same as the APIs for online Mini-Programs, with only a few individual APIs or events being different. This section provides key explanations:

# 1 Launching an Offline Mini-Program

This scenario involves launching a Mini-Program when the App contains the Mini-Program package and the Base Library, but no backend service is deployed.

The zip package for the local Mini-Program is obtained by extracting the offline package exported from the IDE. After extracting the offline package exported by the IDE, there are two files inside: miniprogram.zip and packageConfig.json. Among these, miniprogram.zip is the local Mini-Program zip package to be passed in below; the content inside packageConfig.json is the packages to be passed in below.

/// Launch a local offline Mini-Program
/// @param request The request object
/// @param parentVC The parent view controller
/// @param completion The completion callback
/// @param closeCompletion The callback when the Mini-Program is closed
- (void)startLocalAppletWithRequest:(FATLocalAppletRequest *)request
             inParentViewController:(UIViewController *)parentVC
                         completion:(void (^)(BOOL result, FATError *error))completion
                    closeCompletion:(dispatch_block_t)closeCompletion;

Sample code:

FATLocalAppletRequest *request = [[FATLocalAppletRequest alloc] init];
// Local Mini-Program service identifier, consistent with the local server's apiServer during SDK initialization
request.apiServer = @"localServer";
// The unique identifier of the Mini-Program
request.appletId = appletId;
// The name of the Mini-Program
request.appName = appletTitle;
// The launch parameters of the Mini-Program
request.startParams = startParams;
// The version number of the Mini-Program
request.version = @"1.0.1";
// The list of accessible domains for the Mini-Program
request.domainList = [self.domainList copy];
// The logo of the Mini-Program, a UIImage object. If nil, appletLogo will be loaded.
request.logoImage = [UIImage imageNamed:@"fav_fileicon_loc90"];
//request.appletLogo = @"http://inews.gtimg.com/newsapp_bt/0/13083551761/641.jpg";

NSString *appPath = [[NSBundle mainBundle] pathForResource:offline_appZipName ofType:nil];
// This path can be a full path within the bundle or a full path within the sandbox
request.offlineMiniprogramZipPath = appPath;
// The password for the Mini-Program zip package
request.zipPassword = password;
NSString *libPath = [[NSBundle mainBundle] pathForResource:offline_libZipName ofType:nil];
// This path can be a full path within the bundle or a full path within the sandbox
request.offlineFrameworkZipPath = libPath;

NSString *packageConfigPath = [[NSBundle mainBundle] pathForResource:packageConfig ofType:nil];
NSData *packagesData = [[NSData alloc] initWithContentsOfFile:packageConfigPath];
NSArray *packages = [NSJSONSerialization JSONObjectWithData:packagesData options:0 error:nil];
// The subpackage information dictionary for the Mini-Program
request.packages = packages;
// Whether to use Mini-Program cache
request.useAppletCache = NO;
// Whether to use Base Library cache
request.useFrameworkCache = NO;
// Custom API whitelist
request.extApiWhiteList = @[@"finclipLogin", @"finclipGetUser"];

[[FATClient sharedClient] startLocalAppletWithRequest:request inParentViewController:self completion:^(BOOL result, FATError *error) {
    NSLog(@"result:%d---error:%@", result, error);
} closeCompletion:^{

}];

# 2 Delegate Methods

For online Mini-Programs, navigating to another Mini-Program (i.e., navigateToMiniprogram) will directly fetch and open it from the server. However, for local Mini-Programs, both the Mini-Program package and the Base Library package are provided locally. Therefore, to implement this flow, the App layer must provide the information of the Mini-Program to be opened, which is passed via delegate methods.

# 2.1 navigateToMiniProgram Event

/**
 Triggered when a request to open another local Mini-Program is made.
 This event is only triggered for offline Mini-Programs.
 @param appletInfo The Mini-Program object
 @param request The Mini-Program request object. This request object should be used directly; do not create a new one.
 @param currentVC The top-level view controller of the current Mini-Program
 @param completion The callback after the Mini-Program is opened
 */
- (void)applet:(FATAppletInfo *)appletInfo
navigateToMiniProgram:(FATLocalAppletRequest *)request
     currentVC:(UIViewController *)currentVC
    completion:(void (^)(FATExtensionCode code, NSDictionary *result))completion;

Sample code:

- (void)applet:(FATAppletInfo *)appletInfo
navigateToMiniProgram:(FATLocalAppletRequest *)request
     currentVC:(UIViewController *)currentVC
    completion:(void (^)(FATExtensionCode, NSDictionary *))completion
{
    FATLocalAppletRequest *localRequest = [[FATLocalAppletRequest alloc] init];
    
    NSString *offline_appZipName = @"finapplet_demo.zip";
    NSString *offline_libZipName = @"mop-framework-3.0.1.zip";
    NSString *packageConfig = @"finapplet_demo.json";
 NSString *password = @"1234";
    
    NSString *appPath = [[NSBundle mainBundle] pathForResource:offline_appZipName ofType:nil];
    localRequest.offlineMiniprogramZipPath = appPath;
    NSString *libPath = [[NSBundle mainBundle] pathForResource:offline_libZipName ofType:nil];
    localRequest.offlineFrameworkZipPath = libPath;

    NSString *packageConfigPath = [[NSBundle mainBundle] pathForResource:packageConfig ofType:nil];
    NSData *packagesData = [[NSData alloc] initWithContentsOfFile:packageConfigPath];
    NSArray *packages = [NSJSONSerialization JSONObjectWithData:packagesData options:0 error:nil];
    
    localRequest.appletId = @"finapplet_demo";
    localRequest.apiServer = request.apiServer;
    localRequest.startParams = request.startParams;
    localRequest.packages = packages;
    localRequest.zipPassword = password;
    localRequest.useAppletCache = NO;
    
    [[FATClient sharedClient] startLocalAppletWithRequest:localRequest inParentViewController:currentVC completion:^(BOOL result, FATError *error) {
        if (result) {
            completion(FATExtensionCodeSuccess, nil);
        } else {
            completion(FATExtensionCodeFailure, @{@"errMsg":@"Failed to open"});
        }
    } closeCompletion:^{

    }];
}

# 2.2 Subpackage Loading Event for Local Mini-Programs

Since the Mini-Program zip package that needs to be passed in 1 Launching an Offline Mini-Program can be too large and does not utilize the features of subpackaged Mini-Programs effectively, subsequent optimizations have been made. When launching a local Mini-Program, it is no longer mandatory to pass the Mini-Program zip package path. In this case, when loading a subpackage of the Mini-Program, a delegate method will be called to obtain the zip file for the Mini-Program's subpackage.

/**
 The Host-App provides the zip subpackage path for a local Mini-Program (Applicable for Mini-Program subpackage loading, must be implemented for subpackage loading)
 If multiple Mini-Program subpackages are included in the app bundle, to avoid naming conflicts for zip subpackages, place subpackages of different Mini-Programs in different folders and import them using the 'create folder references' method. In code, obtain the subpackage via FolderName/SubpackageName.zip.
 @param appletInfo Mini-Program information
 @param packDict Subpackage information dictionary
 @param zipPathCallback Callback for the zip subpackage path. Call zipPathCallback(nil) if path retrieval fails.
 */
- (void)localApplet:(FATAppletInfo *)appletInfo packDict:(NSDictionary *)packDict zipPathCallback:(void (^)(NSString *zipPath))zipPathCallback;

# 2.3 getAccountInfo Delegate Event

The getAccountInfo API within the Mini-Program needs to return the Mini-Program's ID, version, and online version number. However, the information for local Mini-Programs is maintained by the App. Therefore, a delegate event must also be implemented to return the correct information.

/// Get local Mini-Program account information
/// @brief Gets the local Mini-Program account information. The returned information structure:
/// @{
///     @"miniProgram": @{
///         @"appId": ##Mini-Program appId##,
///         @"envVersion": ##Mini-Program version##,
///         @"version": ##Online Mini-Program version number##
///     },
///     @"plugin": @{
///         @"appId": ##Plugin appId##,
///         @"version": ##Plugin version number##
///     }
/// }
/// @param appletInfo Mini-Program information
/// @return Mini-Program account information
- (NSDictionary *)localAppletAccountInfo:(FATAppletInfo *)appletInfo;

# 3 New Features for Offline (Local) Mini-Programs

# 3.1 Offline (Local) Mini-Program Version Types

Added version types for offline (local) Mini-Programs. Set the appletVersionType property of the FATLocalAppletRequest object when launching an offline (local) Mini-Program. This property is an enumeration with a total of 6 types:

typedef NS_ENUM(NSUInteger, FATAppletVersionType) {
    FATAppletVersionTypeRelease,     // Release version, default value
    FATAppletVersionTypeTrial,       // Trial version
    FATAppletVersionTypeTemporary,   // Temporary version, IDE preview version
    FATAppletVersionTypeRemoteDebug, // Remote debugging version
    FATAppletVersionTypeReview,      // Review version
    FATAppletVersionTypeDevelopment  // Development version
};

Sample code:

FATLocalAppletRequest *request = [[FATLocalAppletRequest alloc] init];
...
request.appletVersionType = FATAppletVersionTypeTrial;
[[FATClient sharedClient] startLocalAppletWithRequest:request inParentViewController:self completion:^(BOOL result, FATError *error) {
    
} closeCompletion:^{
    
}];
© FinClip with ❤ , Since 2017