diff --git a/example/integration_test/find_interaction_controller/find_interactions.dart b/example/integration_test/find_interaction_controller/find_interactions.dart index b649e97c..144a06be 100644 --- a/example/integration_test/find_interaction_controller/find_interactions.dart +++ b/example/integration_test/find_interaction_controller/find_interactions.dart @@ -120,5 +120,7 @@ void findInteractions() { await controller.findAll(find: "InAppWebViewInitialFileTest"); final int numberOfMatches = await numberOfMatchesCompleter.future; expect(numberOfMatches, 2); + final session = await findInteractionController.getActiveFindSession(); + expect(session!.resultCount, 2); }, skip: shouldSkip); } diff --git a/ios/Classes/FindInteraction/FindInteractionChannelDelegate.swift b/ios/Classes/FindInteraction/FindInteractionChannelDelegate.swift index 490623cf..fb195062 100644 --- a/ios/Classes/FindInteraction/FindInteractionChannelDelegate.swift +++ b/ios/Classes/FindInteraction/FindInteractionChannelDelegate.swift @@ -132,12 +132,8 @@ public class FindInteractionChannelDelegate : ChannelDelegate { } break case "getActiveFindSession": - if #available(iOS 16.0, *) { - if let interaction = findInteractionController?.webView?.findInteraction { - result(interaction.activeFindSession?.toMap()) - } else { - result(nil) - } + if let findInteractionController = findInteractionController { + result(findInteractionController.activeFindSession?.toMap()) } else { result(nil) } @@ -149,6 +145,12 @@ public class FindInteractionChannelDelegate : ChannelDelegate { } public func onFindResultReceived(activeMatchOrdinal: Int, numberOfMatches: Int, isDoneCounting: Bool) { + if isDoneCounting, let findInteractionController = findInteractionController { + findInteractionController.activeFindSession = FindSession(resultCount: activeMatchOrdinal, + highlightedResultIndex: numberOfMatches, + searchResultDisplayStyle: 2) // matches UIFindSession.SearchResultDisplayStyle.none + } + let arguments: [String : Any?] = [ "activeMatchOrdinal": activeMatchOrdinal, "numberOfMatches": numberOfMatches, diff --git a/ios/Classes/FindInteraction/FindInteractionController.swift b/ios/Classes/FindInteraction/FindInteractionController.swift index d3d451e2..73be4d69 100644 --- a/ios/Classes/FindInteraction/FindInteractionController.swift +++ b/ios/Classes/FindInteraction/FindInteractionController.swift @@ -16,6 +16,22 @@ public class FindInteractionController : NSObject, Disposable { var settings: FindInteractionSettings? var shouldCallOnRefresh = false + private var _activeFindSession: FindSession? = nil + var activeFindSession: FindSession? { + get { + if #available(iOS 16.0, *), let interaction = webView?.findInteraction { + if let activeFindSession = interaction.activeFindSession { + return FindSession.fromUIFindSession(uiFindSession: activeFindSession) + } + return nil + } + return _activeFindSession + } + set { + self._activeFindSession = newValue + } + } + public init(registrar: FlutterPluginRegistrar, id: Any, webView: InAppWebView, settings: FindInteractionSettings?) { super.init() self.webView = webView @@ -99,6 +115,7 @@ public class FindInteractionController : NSObject, Disposable { channelDelegate?.dispose() channelDelegate = nil webView = nil + activeFindSession = nil } deinit { diff --git a/ios/Classes/Types/UIFindSession.swift b/ios/Classes/Types/UIFindSession.swift index fb19d340..be1e84d1 100644 --- a/ios/Classes/Types/UIFindSession.swift +++ b/ios/Classes/Types/UIFindSession.swift @@ -7,6 +7,33 @@ import Foundation +public class FindSession: NSObject { + var resultCount: Int + var highlightedResultIndex: Int + var searchResultDisplayStyle: Int + + public init(resultCount: Int, highlightedResultIndex: Int, searchResultDisplayStyle: Int) { + self.resultCount = resultCount + self.highlightedResultIndex = highlightedResultIndex + self.searchResultDisplayStyle = searchResultDisplayStyle + } + + @available(iOS 16.0, *) + public static func fromUIFindSession(uiFindSession: UIFindSession) -> FindSession { + return FindSession(resultCount: uiFindSession.resultCount, + highlightedResultIndex: uiFindSession.highlightedResultIndex, + searchResultDisplayStyle: uiFindSession.searchResultDisplayStyle.rawValue) + } + + public func toMap () -> [String:Any?] { + return [ + "resultCount": resultCount, + "highlightedResultIndex": highlightedResultIndex, + "searchResultDisplayStyle": searchResultDisplayStyle + ] + } +} + @available(iOS 16.0, *) extension UIFindSession { public func toMap () -> [String:Any?] { diff --git a/lib/src/find_interaction/find_interaction_controller.dart b/lib/src/find_interaction/find_interaction_controller.dart index 7463c506..b22ca35f 100644 --- a/lib/src/find_interaction/find_interaction_controller.dart +++ b/lib/src/find_interaction/find_interaction_controller.dart @@ -210,9 +210,7 @@ class FindInteractionController { await _channel?.invokeMethod('dismissFindNavigator', args); } - ///If there's a currently active find session (on iOS, implying [isFindNavigatorVisible] is `true`), returns the active find session. - /// - ///**NOTE**: available on iOS only if [InAppWebViewSettings.isFindInteractionEnabled] is `true`. + ///If there's a currently active find session, returns the active find session. /// ///**Supported Platforms/Implementations**: ///- Android native WebView