add 自定义小程序api和H5 api的实例

master
wanghualei 2020-12-04 14:09:34 +08:00
parent 6e5938925e
commit e24fbd2d04
5 changed files with 122 additions and 43 deletions

View File

@ -13,6 +13,7 @@
9DD565C923EC450A0011FC4A /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 9DD565C823EC450A0011FC4A /* Assets.xcassets */; };
9DD565CC23EC450A0011FC4A /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 9DD565CA23EC450A0011FC4A /* LaunchScreen.storyboard */; };
9DD565CF23EC450A0011FC4A /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 9DD565CE23EC450A0011FC4A /* main.m */; };
A863B745257A09A300959AA1 /* FINExtensionHelper.m in Sources */ = {isa = PBXBuildFile; fileRef = A863B744257A09A300959AA1 /* FINExtensionHelper.m */; };
/* End PBXBuildFile section */
/* Begin PBXFileReference section */
@ -27,6 +28,8 @@
9DD565CB23EC450A0011FC4A /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
9DD565CD23EC450A0011FC4A /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
9DD565CE23EC450A0011FC4A /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
A863B743257A09A200959AA1 /* FINExtensionHelper.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FINExtensionHelper.h; sourceTree = "<group>"; };
A863B744257A09A300959AA1 /* FINExtensionHelper.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FINExtensionHelper.m; sourceTree = "<group>"; };
FBEBA7E73B95B66C846E76FF /* Pods-demo.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-demo.debug.xcconfig"; path = "Target Support Files/Pods-demo/Pods-demo.debug.xcconfig"; sourceTree = "<group>"; };
/* End PBXFileReference section */
@ -65,6 +68,8 @@
children = (
9DD565BC23EC45080011FC4A /* AppDelegate.h */,
9DD565BD23EC45080011FC4A /* AppDelegate.m */,
A863B743257A09A200959AA1 /* FINExtensionHelper.h */,
A863B744257A09A300959AA1 /* FINExtensionHelper.m */,
9DD565C223EC45080011FC4A /* ViewController.h */,
9DD565C323EC45080011FC4A /* ViewController.m */,
9DD565C823EC450A0011FC4A /* Assets.xcassets */,
@ -206,6 +211,7 @@
buildActionMask = 2147483647;
files = (
9DD565C423EC45080011FC4A /* ViewController.m in Sources */,
A863B745257A09A300959AA1 /* FINExtensionHelper.m in Sources */,
9DD565BE23EC45080011FC4A /* AppDelegate.m in Sources */,
9DD565CF23EC450A0011FC4A /* main.m in Sources */,
);

View File

@ -8,6 +8,7 @@
#import "AppDelegate.h"
#import "ViewController.h"
#import "FINExtensionHelper.h"
#import <FinApplet/FinApplet.h>
@ -28,6 +29,8 @@
[[FATClient sharedClient] initWithConfig:config error:nil];
[FINExtensionHelper registerCustomApis];
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
self.window.rootViewController = [[ViewController alloc] init];

View File

@ -0,0 +1,15 @@
//
// FINExtensionHelper.h
// finclip_custom_demo
//
// Created by Haley on 2020/12/3.
//
#import <Foundation/Foundation.h>
@interface FINExtensionHelper : NSObject
+ (void)registerCustomApis;
@end

View File

@ -0,0 +1,28 @@
//
// FINExtensionHelper.m
// finclip_custom_demo
//
// Created by Haley on 2020/12/3.
//
#import "FINExtensionHelper.h"
#import <FinApplet/FinApplet.h>
@implementation FINExtensionHelper
+ (void)registerCustomApis
{
[[FATClient sharedClient] registerExtensionApi:@"onNative" handle:^(id param, FATExtensionApiCallback callback) {
NSString *inputText = @"床前明月光,疑是地上霜。举头望明月,低头思故乡。";
callback(FATExtensionCodeSuccess, @{@"text":inputText});
}];
[[FATClient sharedClient] fat_registerWebApi:@"user_define_native" handle:^(id param, FATExtensionApiCallback callback) {
NSString *inputText = @"鹅鹅鹅,曲项向天歌,白毛浮绿水,红掌拨清波。";
callback(FATExtensionCodeSuccess, @{@"text":inputText});
}];
}
@end

View File

@ -9,7 +9,11 @@
#import "ViewController.h"
#import <FinApplet/FinApplet.h>
@interface ViewController ()
@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *appletList;
@end
@ -19,53 +23,76 @@
[super viewDidLoad];
// Do any additional setup after loading the view.
UIButton *btn1 = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 200, 40)];
[btn1 setTitle:@"打开绘图小程序" forState:UIControlStateNormal];
[btn1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[btn1 setBackgroundColor:[UIColor grayColor]];
[btn1 addTarget:self action:@selector(onCanvasClick:) forControlEvents:UIControlEventTouchUpInside];
[self p_loadData];
UIButton *btn2 = [[UIButton alloc] initWithFrame:CGRectMake(100, 150, 200, 40)];
[btn2 setTitle:@"打开官方小程序" forState:UIControlStateNormal];
[btn2 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[btn2 setBackgroundColor:[UIColor grayColor]];
[btn2 addTarget:self action:@selector(onDemoClick:) forControlEvents:UIControlEventTouchUpInside];
UIButton *btn3 = [[UIButton alloc] initWithFrame:CGRectMake(100, 200, 200, 40)];
[btn3 setTitle:@"我的对账单" forState:UIControlStateNormal];
[btn3 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[btn3 setBackgroundColor:[UIColor grayColor]];
[btn3 addTarget:self action:@selector(onProfileClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn1];
[self.view addSubview:btn2];
[self.view addSubview:btn3];
[self p_initSubViews];
}
- (void)onCanvasClick:(id)sender
- (void)p_initSubViews
{
NSString *appId = @"5facb3a52dcbff00017469bd";
//
[[FATClient sharedClient] startRemoteApplet:appId startParams:nil InParentViewController:self completion:^(BOOL result, NSError *error) {
NSLog(@"result:%d---error:%@", result, error);
CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, 44)];
headerView.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight) style:UITableViewStylePlain];
self.tableView.tableHeaderView = headerView;
self.tableView.tableFooterView = [UIView new];
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.view addSubview:self.tableView];
}
- (void)p_loadData
{
NSDictionary *startParams = nil;
self.appletList = [[NSMutableArray alloc] init];
[self.appletList addObject:@{@"appId":@"5facb3a52dcbff00017469bd",@"title":@"绘图小程序"}];
[self.appletList addObject:@{@"appId":@"5fa214a29a6a7900019b5cc1",@"title":@"官方示例小程序"}];
[self.appletList addObject:@{@"appId":@"5fa215459a6a7900019b5cc3",@"title":@"对账单"}];
// FAT
[self.appletList addObject:@{@"appId":@"5fc8934aefb8c600019e9747",@"title":@"自定义小程序API示例"}];
startParams = @{
@"path" : @"/pages/webview/webview"
};
[self.appletList addObject:@{@"appId":@"5fc8934aefb8c600019e9747",@"title":@"自定义H5 API示例", @"startParams":startParams}];
[self.tableView reloadData];
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.appletList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifer = @"identifer";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];
}
NSDictionary *dict = self.appletList[indexPath.row];
cell.textLabel.text = dict[@"title"];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSDictionary *dict = self.appletList[indexPath.row];
NSString *appId = dict[@"appId"];
NSDictionary *startParams = dict[@"startParams"];
[[FATClient sharedClient] startRemoteApplet:appId startParams:startParams InParentViewController:self completion:^(BOOL result, NSError *error) {
}];
}
- (void)onDemoClick:(id)sender {
NSString *appId = @"5fa214a29a6a7900019b5cc1";
//
[[FATClient sharedClient] startRemoteApplet:appId startParams:nil InParentViewController:self completion:^(BOOL result, NSError *error) {
NSLog(@"result:%d---error:%@", result, error);
}];
}
- (void)onProfileClick:(id)sender {
NSString *appId = @"5fa215459a6a7900019b5cc3";
//
[[FATClient sharedClient] startRemoteApplet:appId startParams:nil InParentViewController:self completion:^(BOOL result, NSError *error) {
NSLog(@"result:%d---error:%@", result, error);
}];
}
@end