CompdfkitFlutterPlugin.m 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #import "CompdfkitFlutterPlugin.h"
  2. #import <ComPDFKit/ComPDFKit.h>
  3. #import "CPDFViewController.h"
  4. @interface CompdfkitFlutterPlugin () <CPDFViewBaseControllerDelete>
  5. @end
  6. @implementation CompdfkitFlutterPlugin
  7. + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
  8. FlutterMethodChannel* channel = [FlutterMethodChannel
  9. methodChannelWithName:@"com.compdfkit.flutter.plugin"
  10. binaryMessenger:[registrar messenger]];
  11. CompdfkitFlutterPlugin* instance = [[CompdfkitFlutterPlugin alloc] init];
  12. [registrar addMethodCallDelegate:instance channel:channel];
  13. }
  14. - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
  15. if ([@"sdk_version_code" isEqualToString:call.method]) {
  16. result([CPDFKit sharedInstance].versionString);
  17. } else if ([@"init_sdk" isEqualToString:call.method]) {
  18. NSDictionary *initInfo = (NSDictionary *)call.arguments;
  19. NSString *key = initInfo[@"key"] ?: @"";
  20. [CPDFKit verifyWithKey:key];
  21. } else if ([@"sdk_build_tag" isEqualToString:call.method]) {
  22. result([@"iOS build tag:" stringByAppendingFormat:@"%ld", [CPDFKit sharedInstance].buildNumber]);
  23. } else if ([@"openDocument" isEqualToString:call.method]){
  24. NSDictionary *initInfo = (NSDictionary *)call.arguments;
  25. NSString *path = initInfo[@"document"] ?: @"";
  26. UIViewController *tRootViewControl = [UIApplication sharedApplication].keyWindow.rootViewController;
  27. if ([tRootViewControl presentedViewController]) {
  28. tRootViewControl = [tRootViewControl presentedViewController];
  29. }
  30. CPDFConfiguration *configuration = [[CPDFConfiguration alloc] init];
  31. CNavBarButtonItem *thumbnail = [[CNavBarButtonItem alloc]initWithViewLeftBarButtonItem:CPDFViewBarLeftButtonItem_Thumbnail];
  32. CNavBarButtonItem *back = [[CNavBarButtonItem alloc]initWithViewLeftBarButtonItem:CPDFViewBarLeftButtonItem_Back];
  33. CNavBarButtonItem *search = [[CNavBarButtonItem alloc]initWithViewRightBarButtonItem:CPDFViewBarRightButtonItem_Search];
  34. CNavBarButtonItem *bota = [[CNavBarButtonItem alloc]initWithViewRightBarButtonItem:CPDFViewBarRightButtonItem_Bota];
  35. CNavBarButtonItem *more = [[CNavBarButtonItem alloc]initWithViewRightBarButtonItem:CPDFViewBarRightButtonItem_More];
  36. configuration.showleftItems = @[back,thumbnail];
  37. configuration.showRightItems = @[search,bota,more];
  38. CPDFViewController *pdfViewController = [[CPDFViewController alloc] initWithFilePath:path password:nil configuration:configuration];
  39. pdfViewController.delegate = self;
  40. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:pdfViewController];
  41. navigationController.modalPresentationStyle = UIModalPresentationFullScreen;
  42. [tRootViewControl presentViewController:navigationController animated:YES completion:nil];
  43. } else if ([@"getTemporaryDirectory" isEqualToString:call.method]){
  44. result([self getTemporaryDirectory]);
  45. } else {
  46. result(FlutterMethodNotImplemented);
  47. }
  48. }
  49. - (NSString*)getTemporaryDirectory {
  50. NSArray* paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  51. return paths.firstObject;
  52. }
  53. #pragma mark - CPDFViewBaseControllerDelete
  54. - (void)PDFViewBaseControllerDissmiss:(CPDFViewBaseController *)baseControllerDelete {
  55. [baseControllerDelete dismissViewControllerAnimated:YES completion:nil];
  56. }
  57. @end