From e7fb05c0272d016c616d1212feba53e68608733e Mon Sep 17 00:00:00 2001 From: Alexander Strup Date: Mon, 28 Mar 2022 02:50:24 +0300 Subject: [PATCH 1/2] Fix getting rootViewController for iOS 13+ --- ios/Classes/SwiftContactsServicePlugin.swift | 25 ++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/ios/Classes/SwiftContactsServicePlugin.swift b/ios/Classes/SwiftContactsServicePlugin.swift index 8babff12..11a20521 100644 --- a/ios/Classes/SwiftContactsServicePlugin.swift +++ b/ios/Classes/SwiftContactsServicePlugin.swift @@ -13,10 +13,27 @@ 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() { + print("Contact Service navite iOS: Find root view controller"); + let instance = SwiftContactsServicePlugin(rootViewController) + registrar.addMethodCallDelegate(instance, channel: channel) + instance.preLoadContactView() + } else { + print("Contact Service navite 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) { From 7bb95cb7d0851a2c347c446820ccd5b7bb66a49d Mon Sep 17 00:00:00 2001 From: Alexander Strup Date: Mon, 28 Mar 2022 03:56:26 +0300 Subject: [PATCH 2/2] Fix message log for native iOS --- ios/Classes/SwiftContactsServicePlugin.swift | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/ios/Classes/SwiftContactsServicePlugin.swift b/ios/Classes/SwiftContactsServicePlugin.swift index 11a20521..7fb0ab4d 100644 --- a/ios/Classes/SwiftContactsServicePlugin.swift +++ b/ios/Classes/SwiftContactsServicePlugin.swift @@ -14,12 +14,11 @@ 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()) if let rootViewController = SwiftContactsServicePlugin.getRootViewController() { - print("Contact Service navite iOS: Find root view controller"); let instance = SwiftContactsServicePlugin(rootViewController) registrar.addMethodCallDelegate(instance, channel: channel) instance.preLoadContactView() } else { - print("Contact Service navite iOS: Can NOT find root view controller"); + print("Error contacts service native iOS: Can NOT find root view controller"); } }