Giter VIP home page Giter VIP logo

flutterswiftplugin-template's Introduction

Flutter plugin written in swift

⚠️ UPDATE : 12/05/2017 : This template is not working with the last Plugin API.

This is an experimental "empty" project template to write Flutter plugins in Swift ( a complete example : Flutter Audio : swift-based Flutter plugin example to play audio files).

Documentation about Flutter plugins and native Apis access from Flutter

Basically, a Flutter plugin is a platform channel subscriber/pusher, packaged and shared as a Dart pub package.

There is different types of channels :

  • MethodChannel, allowing call native methods from Dart, and vice-versa.
  • and BasicMessageChannel, which is an async stream between Flutter Dart code and the host platform.

When you create a plugin project with flutter CLI flutter create --plugin my_plugin, all generated iOS files are written in ObjectiveC.

In this template, I replaced the plugin generated .h & .m files with swift 3 files.

⚠️ default objectiveC plugin are static libraries. To use swift you need to change to dynamic library in the Pods_Runner build settings.

The plugin main file is /ios/Classes/SwiftPluginPoc.swift

import Foundation
import Flutter

@objc public class SwiftPluginPocPlugin:NSObject{
    
    var channel:FlutterMethodChannel
    
    public init( controller:FlutterViewController){
        channel = FlutterMethodChannel(name: "swift_plugin_poc", binaryMessenger: controller)
        super.init()
        channel.setMethodCallHandler(onMethodCall)
    }
    
    public func onMethodCall( _ call:FlutterMethodCall, result:FlutterResult ){
        if call.method == "getPlatformVersion" {
            result("iOS response : \(UIDevice.current.systemVersion)")
        } else {
            result( FlutterMethodNotImplemented)
        }
    }
}

I also replaced all the example project files :

import UIKit
import AVFoundation
import Flutter

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {

  var plugins: PluginRegistry?

  override func application(_ application: UIApplication,
                            didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?
  ) -> Bool {

    guard let flutterController = self.window.rootViewController as? Flutter.FlutterViewController else {
      print("no FlutterViewController")
      return true
    }

    plugins = PluginRegistry(flutterController)

    return true
  }
}

Here we import the swift_plugin_poc module, to access the SwiftPluginPocPlugin class.

import Foundation
import Flutter
import swift_plugin_poc

class PluginRegistry:NSObject{
    
    var swift_plugin_poc:SwiftPluginPocPlugin
    
    init(withController controller:FlutterViewController){
        swift_plugin_poc = SwiftPluginPocPlugin(withController:controller)
    }
}

flutterswiftplugin-template's People

Contributors

rxlabz avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.