2019-12-18 20:34:40 +00:00
|
|
|
//
|
|
|
|
// InAppBrowserManager.swift
|
|
|
|
// flutter_inappwebview
|
|
|
|
//
|
|
|
|
// Created by Lorenzo Pichilli on 18/12/2019.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Flutter
|
|
|
|
import UIKit
|
|
|
|
import WebKit
|
|
|
|
import Foundation
|
|
|
|
import AVFoundation
|
|
|
|
|
2022-05-05 18:19:16 +00:00
|
|
|
public class InAppBrowserManager: ChannelDelegate {
|
|
|
|
static let METHOD_CHANNEL_NAME = "com.pichillilorenzo/flutter_inappbrowser"
|
|
|
|
static let WEBVIEW_STORYBOARD = "WebView"
|
|
|
|
static let WEBVIEW_STORYBOARD_CONTROLLER_ID = "viewController"
|
|
|
|
static let NAV_STORYBOARD_CONTROLLER_ID = "navController"
|
2023-05-16 18:06:53 +00:00
|
|
|
var plugin: SwiftFlutterPlugin?
|
2019-12-18 20:34:40 +00:00
|
|
|
|
|
|
|
private var previousStatusBarStyle = -1
|
|
|
|
|
2023-05-16 18:06:53 +00:00
|
|
|
init(plugin: SwiftFlutterPlugin) {
|
|
|
|
super.init(channel: FlutterMethodChannel(name: InAppBrowserManager.METHOD_CHANNEL_NAME, binaryMessenger: plugin.registrar!.messenger()))
|
|
|
|
self.plugin = plugin
|
2019-12-18 20:34:40 +00:00
|
|
|
}
|
|
|
|
|
2022-05-05 18:19:16 +00:00
|
|
|
public override func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
|
2019-12-18 20:34:40 +00:00
|
|
|
let arguments = call.arguments as? NSDictionary
|
|
|
|
|
|
|
|
switch call.method {
|
2021-03-05 22:19:50 +00:00
|
|
|
case "open":
|
|
|
|
open(arguments: arguments!)
|
2019-12-18 20:34:40 +00:00
|
|
|
result(true)
|
|
|
|
break
|
|
|
|
case "openWithSystemBrowser":
|
|
|
|
let url = arguments!["url"] as! String
|
|
|
|
openWithSystemBrowser(url: url, result: result)
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
result(FlutterMethodNotImplemented)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-19 23:31:14 +00:00
|
|
|
public func prepareInAppBrowserWebViewController(settings: [String: Any?]) -> InAppBrowserWebViewController {
|
2021-02-22 11:16:23 +00:00
|
|
|
if previousStatusBarStyle == -1 {
|
|
|
|
previousStatusBarStyle = UIApplication.shared.statusBarStyle.rawValue
|
2019-12-18 20:34:40 +00:00
|
|
|
}
|
|
|
|
|
2022-04-19 23:31:14 +00:00
|
|
|
let browserSettings = InAppBrowserSettings()
|
|
|
|
let _ = browserSettings.parse(settings: settings)
|
2019-12-18 20:34:40 +00:00
|
|
|
|
2022-04-19 23:31:14 +00:00
|
|
|
let webViewSettings = InAppWebViewSettings()
|
|
|
|
let _ = webViewSettings.parse(settings: settings)
|
2019-12-18 20:34:40 +00:00
|
|
|
|
2021-02-22 11:16:23 +00:00
|
|
|
let webViewController = InAppBrowserWebViewController()
|
2023-05-16 18:06:53 +00:00
|
|
|
webViewController.plugin = plugin
|
2022-04-19 23:31:14 +00:00
|
|
|
webViewController.browserSettings = browserSettings
|
2022-10-18 16:12:33 +00:00
|
|
|
webViewController.isHidden = browserSettings.hidden
|
2022-04-19 23:31:14 +00:00
|
|
|
webViewController.webViewSettings = webViewSettings
|
2019-12-18 20:34:40 +00:00
|
|
|
webViewController.previousStatusBarStyle = previousStatusBarStyle
|
|
|
|
return webViewController
|
|
|
|
}
|
|
|
|
|
2021-03-05 22:19:50 +00:00
|
|
|
public func open(arguments: NSDictionary) {
|
|
|
|
let id = arguments["id"] as! String
|
|
|
|
let urlRequest = arguments["urlRequest"] as? [String:Any?]
|
|
|
|
let assetFilePath = arguments["assetFilePath"] as? String
|
|
|
|
let data = arguments["data"] as? String
|
|
|
|
let mimeType = arguments["mimeType"] as? String
|
|
|
|
let encoding = arguments["encoding"] as? String
|
|
|
|
let baseUrl = arguments["baseUrl"] as? String
|
2022-04-19 23:31:14 +00:00
|
|
|
let settings = arguments["settings"] as! [String: Any?]
|
2021-03-05 22:19:50 +00:00
|
|
|
let contextMenu = arguments["contextMenu"] as! [String: Any]
|
|
|
|
let windowId = arguments["windowId"] as? Int64
|
|
|
|
let initialUserScripts = arguments["initialUserScripts"] as? [[String: Any]]
|
2022-04-20 01:05:46 +00:00
|
|
|
let pullToRefreshInitialSettings = arguments["pullToRefreshSettings"] as! [String: Any?]
|
2023-06-10 23:55:42 +00:00
|
|
|
let menuItems = arguments["menuItems"] as! [[String: Any?]]
|
2019-12-18 20:34:40 +00:00
|
|
|
|
2022-04-19 23:31:14 +00:00
|
|
|
let webViewController = prepareInAppBrowserWebViewController(settings: settings)
|
2021-02-22 11:16:23 +00:00
|
|
|
|
2021-03-01 02:21:07 +00:00
|
|
|
webViewController.id = id
|
2021-03-05 22:19:50 +00:00
|
|
|
webViewController.initialUrlRequest = urlRequest != nil ? URLRequest.init(fromPluginMap: urlRequest!) : nil
|
2021-02-22 11:16:23 +00:00
|
|
|
webViewController.initialFile = assetFilePath
|
|
|
|
webViewController.initialData = data
|
|
|
|
webViewController.initialMimeType = mimeType
|
|
|
|
webViewController.initialEncoding = encoding
|
|
|
|
webViewController.initialBaseUrl = baseUrl
|
2020-05-21 01:34:39 +00:00
|
|
|
webViewController.contextMenu = contextMenu
|
Updated onCreateWindow, onJsAlert, onJsConfirm, and onJsPrompt webview events, added onCloseWindow, onTitleChanged, onWindowFocus, and onWindowBlur webview events, added androidOnRequestFocus, androidOnReceivedIcon, androidOnReceivedTouchIconUrl, androidOnJsBeforeUnload, and androidOnReceivedLoginRequest Android-specific webview events, fix #403
2020-06-29 14:34:08 +00:00
|
|
|
webViewController.windowId = windowId
|
2021-02-22 11:16:23 +00:00
|
|
|
webViewController.initialUserScripts = initialUserScripts ?? []
|
2022-04-20 01:05:46 +00:00
|
|
|
webViewController.pullToRefreshInitialSettings = pullToRefreshInitialSettings
|
2023-06-10 23:55:42 +00:00
|
|
|
for menuItem in menuItems {
|
|
|
|
webViewController.menuItems.append(InAppBrowserMenuItem.fromMap(map: menuItem)!)
|
|
|
|
}
|
2019-12-18 20:34:40 +00:00
|
|
|
|
2021-02-22 11:16:23 +00:00
|
|
|
presentViewController(webViewController: webViewController)
|
|
|
|
}
|
|
|
|
|
|
|
|
public func presentViewController(webViewController: InAppBrowserWebViewController) {
|
2022-05-05 18:19:16 +00:00
|
|
|
let storyboard = UIStoryboard(name: InAppBrowserManager.WEBVIEW_STORYBOARD, bundle: Bundle(for: InAppWebViewFlutterPlugin.self))
|
|
|
|
let navController = storyboard.instantiateViewController(withIdentifier: InAppBrowserManager.NAV_STORYBOARD_CONTROLLER_ID) as! InAppBrowserNavigationController
|
2021-02-22 11:16:23 +00:00
|
|
|
webViewController.edgesForExtendedLayout = []
|
|
|
|
navController.pushViewController(webViewController, animated: false)
|
|
|
|
webViewController.prepareNavigationControllerBeforeViewWillAppear()
|
|
|
|
|
|
|
|
var animated = true
|
2022-10-17 00:23:05 +00:00
|
|
|
if let browserSettings = webViewController.browserSettings, browserSettings.hidden {
|
2021-02-22 11:16:23 +00:00
|
|
|
animated = false
|
2019-12-18 20:34:40 +00:00
|
|
|
}
|
2023-08-02 08:04:16 +00:00
|
|
|
|
2023-08-02 10:34:58 +00:00
|
|
|
guard let visibleViewController = UIApplication.shared.visibleViewController else {
|
2023-11-10 10:43:44 +00:00
|
|
|
assertionFailure("Failure init the visibleViewController!")
|
2023-08-02 08:04:16 +00:00
|
|
|
return
|
|
|
|
}
|
2023-12-20 11:01:40 +00:00
|
|
|
|
|
|
|
if let popover = navController.popoverPresentationController {
|
|
|
|
let sourceView = visibleViewController.view ?? UIView()
|
|
|
|
|
|
|
|
popover.sourceRect = CGRect(x: sourceView.bounds.midX, y: sourceView.bounds.midY, width: 0, height: 0)
|
|
|
|
popover.permittedArrowDirections = []
|
|
|
|
popover.sourceView = sourceView
|
|
|
|
}
|
|
|
|
|
2023-08-02 08:04:16 +00:00
|
|
|
visibleViewController.present(navController, animated: animated)
|
2019-12-18 20:34:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public func openWithSystemBrowser(url: String, result: @escaping FlutterResult) {
|
|
|
|
let absoluteUrl = URL(string: url)!.absoluteURL
|
|
|
|
if !UIApplication.shared.canOpenURL(absoluteUrl) {
|
|
|
|
result(FlutterError(code: "InAppBrowserManager", message: url + " cannot be opened!", details: nil))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if #available(iOS 10.0, *) {
|
2023-12-20 11:21:05 +00:00
|
|
|
UIApplication.shared.open(absoluteUrl)
|
2019-12-18 20:34:40 +00:00
|
|
|
} else {
|
|
|
|
UIApplication.shared.openURL(absoluteUrl)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result(true)
|
|
|
|
}
|
2022-04-23 02:02:37 +00:00
|
|
|
|
2022-05-05 18:19:16 +00:00
|
|
|
public override func dispose() {
|
|
|
|
super.dispose()
|
2023-05-16 18:06:53 +00:00
|
|
|
plugin = nil
|
2022-04-23 02:02:37 +00:00
|
|
|
}
|
2022-05-08 10:56:28 +00:00
|
|
|
|
|
|
|
deinit {
|
|
|
|
dispose()
|
|
|
|
}
|
2019-12-18 20:34:40 +00:00
|
|
|
}
|