Writing Flutter plugin package — 2

Introduction

This is the second part of my two part series where I develop Flutter package plugin. If you have not read the first part I recommend doing so from this link. In the first part we went through the basics of package development and how to create a package, we also covered the Android side of developing a package. In this article we are exploring the iOS side of things. We are going to be using PDFKit library to extract the text from the PDF documents.

Requirements

  • We need a Mac to run the code on.
  • Your Mac will need to have XCode installed.
  • I recommend writing the native code in separate project to make sure it works before adding it to plugin.
  • VS Code or other code editor to write the plugin with.

Writing iOS package plugin

Navigate to the ios directory in of your package (the package we created in the first article). In the ios directory there are two files that are important to us: SwiftPdfTextPackagePlugin.swift and pdf_text_package.podspec.

The swift file hosts our native code that we are going to be writing. Here we have a class with two methods. register method is linking this swift host with our Flutter package and handle method is called when there is a MethodCall from Flutter-side. We don’t need to care about the register method. We are going to be implementing our code to the handle method.

In the podspec-file we have some meta-data relating to our swift host. Here we need to up the iOS version of our package so that we can use PDFKit. The default value is 8, but we need it to be 11 or higher.

Now we can import PDFKit to our swift-file.

We can now add the code to our swift-file, into the handle method.

What happens here?

  • We are first getting the path of our PDF file from the arguments.
let args = call.arguments as! NSDictionary          
let path = args["path"] as! String
  • Then we check if the PDF document exists and if it does we loop through the pages and append the contents to a single string variable pdfText.
  • If the file doesn’t exist or the call MethodCall is invalid we are sending error back.

Now if we run this we will get an error (at least I did). This error says that we need to add #available tags to the SwiftPdfTextPackagePlugin class and to handle method.

Error about the #available tags missing

We can add them using this line.

@available(iOS 11, *)

Running the example

Navigate to the example folder and run the project.

flutter run

If everything is working correctly you should see this screen.

Example application running
Select pdf file, using file_picker
The plugin gets the text from PDF file

Well done!

Forewords

Thank you for reading, I hope this helped you. Clap, comment and let me know what you thought about this tutorial! :)

>>> Writing Flutter plugin package — 1<<<

Links

My Youtube

My website

GitHub for the code

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store