Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions ios/Classes/SwiftContactsServicePlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,26 @@ public class SwiftContactsServicePlugin: NSObject, FlutterPlugin, CNContactViewC

public static func register(with registrar: FlutterPluginRegistrar) {
let channel = FlutterMethodChannel(name: "github.com/clovisnicolas/flutter_contacts", binaryMessenger: registrar.messenger())
let rootViewController = UIApplication.shared.delegate!.window!!.rootViewController!;
let instance = SwiftContactsServicePlugin(rootViewController)
registrar.addMethodCallDelegate(instance, channel: channel)
instance.preLoadContactView()
if let rootViewController = SwiftContactsServicePlugin.getRootViewController() {
let instance = SwiftContactsServicePlugin(rootViewController)
registrar.addMethodCallDelegate(instance, channel: channel)
instance.preLoadContactView()
} else {
print("Error contacts service native iOS: Can NOT find root view controller");
}
}

static func getRootViewController() -> UIViewController? {
var window: UIWindow?
if #available(iOS 13.0, *) {
let windowScenes: UIWindowScene? = UIApplication.shared.connectedScenes
.first(where: { $0 is UIWindowScene }) as? UIWindowScene;
window = windowScenes?.windows.first(where: { $0.isKeyWindow }) ?? windowScenes?.windows.first
} else {
window = UIApplication.shared.delegate?.window as? UIWindow;
}
let rootViewController = window?.rootViewController;
return rootViewController;
}

init(_ rootViewController: UIViewController) {
Expand Down