Flutter provides different dependencies for achieving different features in your Flutter app. However, some libraries might not be available in Flutter or might not be as good as native SDKs. So, we Integrate native SDKs in the Flutter project.
Today, I am going to integrate the native iOS SDK “Folio Reader kit” in a flutter project using the method channel. There are different libraries available in Flutter for opening an epub file but this one fits my requirements.
You can also read more about Flutter app development services from Mobikul.
Now, let’s check how can we integrate native SDK in a Flutter project.
Create a new flutter project & add a button on the screen.
1 2 3 4 5 6 7 8 9 |
return Scaffold( appBar: AppBar(title: const Text("Folio Reader Demo"),), body: Center( child: ElevatedButton(onPressed: (){}, child: const Text("Open Book"), ), ), ); |
Now we are going to use Platform Channel to communicate between UI & platform.
Platform Channel:
A platform channel is a flexible messaging API between a Flutter app and the platform hosting the app. It’s how you can communicate with native and 3rd-party APIs.
From the official doc: “Messages are passed between the client (UI) and host (platform) using platform channels.“
Define a channel name on the dart side:
1 |
var methodChannel = const MethodChannel('com.ebookApp/folioReader'); |
Invoke the channel when required, i.e. on the tap of the button.
1 2 3 4 5 6 |
try{ var data = await methodChannel.invokeMethod("openEbook", 'assets/moby-dick.epub'); }on PlatformException catch(e){ print("Failed to invoke: ${e.message}"); } |
We have added an ebook for extension .epub in our Flutter project assets. Now, we will pass this ebook path in our method as an argument & will use this on our iOS side.
Add iOS-specific Implementation:
Add folio reader SDK pod in your pod file & run the command “pod install”. You can get this SDK from this git link:
https://github.com/FolioReader/FolioReaderKit
1 |
pod 'FolioReaderKit', git: 'https://github.com/FolioReader/FolioReaderKit.git' |
Now add this code to your App Delegate & import Folio Reader Kit.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
override func application( _ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? ) -> Bool { let controller : FlutterViewController = window?.rootViewController as! FlutterViewController let ebookChannel = FlutterMethodChannel(name: "com.ebookApp/folioReader", binaryMessenger: controller.binaryMessenger) ebookChannel.setMethodCallHandler({ (call: FlutterMethodCall, result: @escaping FlutterResult) -> Void in if call.method == "openEbook"{ do{ let bookPath = call.arguments as? String let key = controller.lookupKey(forAsset: bookPath ?? "") let path = Bundle.main.path(forResource: key, ofType: nil) let folioReader = FolioReader() let config = FolioReaderConfig(withIdentifier: "bookPath") folioReader.presentReader(parentViewController: controller, withEpubPath: path ?? "", andConfig: config) } } }) GeneratedPluginRegistrant.register(with: self) return super.application(application, didFinishLaunchingWithOptions: launchOptions) } |
We have fetched the path of the asset in Swift & used it to open our book using the Folioreader Kit in FlutterView Controller & present our reader screen.
Output:
Conclusion:
In this article, I have explained how to integrate native SDKs into the Flutter project.
Thanks for reading this article.
If I got something wrong, let me know in the comments. I would love to improve.
For more interesting blogs check out here – https://mobikul.com/blog/