Tejas Sharma
Tejas Sharma

Reputation: 170

Flutter & Swift: How to use Platform Channel to display View instead of UIView for Swift?

With Flutter Platform Channel and Swift, I got native code to display, but I have one big problem. Flutter needs the View to be a UIView, but my Swift code returns View

I'm using UIHostingController to convert it and it works, but I feel as if there is some alternative way? Currently, there is a big black box around my view, and my approach seems hackish.

This is my current setup with comments:

// factory that returns FluffView
public class FluffViewFactory : NSObject, FlutterPlatformViewFactory {
    public func create(
        withFrame frame: CGRect,
        viewIdentifier viewId: Int64,
        arguments args: Any?
    ) -> FlutterPlatformView {
        return FluffView(frame, viewId: viewId, args: args)
    }
}

// the view
public class FluffView: NSObject, FlutterPlatformView {

let frame: CGRect

let viewId: Int64

init(_ frame: CGRect, viewId: Int64, args: Any?) {
    self.frame = frame
    self.viewId = viewId
}

let context: DeviceActivityReport.Context = .totalActivity
let filter = DeviceActivityFilter(
    segment:.daily(
        during: DateInterval(
            start: Date().addingTimeInterval(-24 * 3600),
            end: Date()
        )
    )
)

// converts View into UIView for Flutter
public func view() -> UIView {
    var child = UIHostingController(rootView: ContentView())
    var parent = UIViewController()
    child.view.translatesAutoresizingMaskIntoConstraints = false
    child.view.frame = parent.view.bounds
    NSLayoutConstraint.activate([
        child.view.topAnchor.constraint(equalTo: parent.view.topAnchor),
        child.view.leadingAnchor.constraint(equalTo: parent.view.leadingAnchor),
        child.view.bottomAnchor.constraint(equalTo: parent.view.bottomAnchor),
        child.view.trailingAnchor.constraint(equalTo: parent.view.trailingAnchor)
    ])
    // First, add the view of the child to the view of the parent
    parent.view.addSubview(child.view)
    // TODO: set background color here
    // parent.view.backgroundColor = BackgroundStyle()
    // Then, add the child to the parent
    parent.addChild(child)
    
    return parent.view
}

}

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
    override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
    ) -> Bool {
        let controller : FlutterViewController = window?.rootViewController as! FlutterViewController
    
    @State var context: DeviceActivityReport.Context = .totalActivity
    @State var filter = DeviceActivityFilter(segment: .daily(during: thisWeek))

    // calls and registers the fluffview that will handle the conversion
    let viewFactory = FluffViewFactory()
    registrar(forPlugin: "Kitty")?.register(viewFactory, withId: "FluffView")


    GeneratedPluginRegistrant.register(with: self)
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}

Upvotes: 1

Views: 378

Answers (0)

Related Questions