Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix getting rootViewController #286

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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