diff --git a/demo.xcodeproj/project.pbxproj b/demo.xcodeproj/project.pbxproj index c6f941d..9e07453 100644 --- a/demo.xcodeproj/project.pbxproj +++ b/demo.xcodeproj/project.pbxproj @@ -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 = ""; }; 9DD565CD23EC450A0011FC4A /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 9DD565CE23EC450A0011FC4A /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; + A863B743257A09A200959AA1 /* FINExtensionHelper.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FINExtensionHelper.h; sourceTree = ""; }; + A863B744257A09A300959AA1 /* FINExtensionHelper.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FINExtensionHelper.m; sourceTree = ""; }; 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 = ""; }; /* 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 */, ); diff --git a/demo/AppDelegate.m b/demo/AppDelegate.m index f2b6efb..12ead9d 100644 --- a/demo/AppDelegate.m +++ b/demo/AppDelegate.m @@ -8,6 +8,7 @@ #import "AppDelegate.h" #import "ViewController.h" +#import "FINExtensionHelper.h" #import @@ -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]; diff --git a/demo/FINExtensionHelper.h b/demo/FINExtensionHelper.h new file mode 100644 index 0000000..47a4b2c --- /dev/null +++ b/demo/FINExtensionHelper.h @@ -0,0 +1,15 @@ +// +// FINExtensionHelper.h +// finclip_custom_demo +// +// Created by Haley on 2020/12/3. +// + +#import + +@interface FINExtensionHelper : NSObject + ++ (void)registerCustomApis; + +@end + diff --git a/demo/FINExtensionHelper.m b/demo/FINExtensionHelper.m new file mode 100644 index 0000000..9a5c6d8 --- /dev/null +++ b/demo/FINExtensionHelper.m @@ -0,0 +1,28 @@ +// +// FINExtensionHelper.m +// finclip_custom_demo +// +// Created by Haley on 2020/12/3. +// + +#import "FINExtensionHelper.h" +#import + +@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 diff --git a/demo/ViewController.m b/demo/ViewController.m index a4bd56c..a8822aa 100644 --- a/demo/ViewController.m +++ b/demo/ViewController.m @@ -9,7 +9,11 @@ #import "ViewController.h" #import -@interface ViewController () +@interface ViewController () + +@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