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
|
|
|
|
|
|
|
|
let WEBVIEW_STORYBOARD = "WebView"
|
|
|
|
let WEBVIEW_STORYBOARD_CONTROLLER_ID = "viewController"
|
2021-02-22 11:16:23 +00:00
|
|
|
let NAV_STORYBOARD_CONTROLLER_ID = "navController"
|
2019-12-18 20:34:40 +00:00
|
|
|
|
|
|
|
public class InAppBrowserManager: NSObject, FlutterPlugin {
|
|
|
|
static var registrar: FlutterPluginRegistrar?
|
|
|
|
static var channel: FlutterMethodChannel?
|
|
|
|
|
|
|
|
private var previousStatusBarStyle = -1
|
|
|
|
|
|
|
|
public static func register(with registrar: FlutterPluginRegistrar) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
init(registrar: FlutterPluginRegistrar) {
|
|
|
|
super.init()
|
|
|
|
InAppBrowserManager.registrar = registrar
|
|
|
|
InAppBrowserManager.channel = FlutterMethodChannel(name: "com.pichillilorenzo/flutter_inappbrowser", binaryMessenger: registrar.messenger())
|
|
|
|
registrar.addMethodCallDelegate(self, channel: InAppBrowserManager.channel!)
|
|
|
|
}
|
|
|
|
|
|
|
|
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
|
|
|
|
let arguments = call.arguments as? NSDictionary
|
|
|
|
|
|
|
|
switch call.method {
|
2021-02-22 11:16:23 +00:00
|
|
|
case "openUrlRequest":
|
2020-05-23 10:32:27 +00:00
|
|
|
let uuid = arguments!["uuid"] as! String
|
2021-02-22 11:16:23 +00:00
|
|
|
let urlRequest = arguments!["urlRequest"] as! [String:Any?]
|
2019-12-18 20:34:40 +00:00
|
|
|
let options = arguments!["options"] as! [String: Any?]
|
2020-05-21 01:34:39 +00:00
|
|
|
let contextMenu = arguments!["contextMenu"] as! [String: Any]
|
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
|
|
|
let windowId = arguments!["windowId"] as? Int64
|
2021-02-01 14:55:27 +00:00
|
|
|
let initialUserScripts = arguments!["initialUserScripts"] as? [[String: Any]]
|
2021-02-22 11:16:23 +00:00
|
|
|
openUrlRequest(uuid: uuid, urlRequest: urlRequest, options: options, contextMenu: contextMenu, windowId: windowId, initialUserScripts: initialUserScripts)
|
2019-12-18 20:34:40 +00:00
|
|
|
result(true)
|
|
|
|
break
|
|
|
|
case "openFile":
|
2020-05-23 10:32:27 +00:00
|
|
|
let uuid = arguments!["uuid"] as! String
|
2021-02-22 11:16:23 +00:00
|
|
|
let assetFilePath = arguments!["assetFilePath"] as! String
|
2019-12-18 20:34:40 +00:00
|
|
|
let options = arguments!["options"] as! [String: Any?]
|
2020-05-21 01:34:39 +00:00
|
|
|
let contextMenu = arguments!["contextMenu"] as! [String: Any]
|
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
|
|
|
let windowId = arguments!["windowId"] as? Int64
|
2021-02-01 14:55:27 +00:00
|
|
|
let initialUserScripts = arguments!["initialUserScripts"] as? [[String: Any]]
|
2021-02-22 11:16:23 +00:00
|
|
|
openFile(uuid: uuid, assetFilePath: assetFilePath, options: options, contextMenu: contextMenu, windowId: windowId, initialUserScripts: initialUserScripts)
|
2019-12-18 20:34:40 +00:00
|
|
|
result(true)
|
|
|
|
break
|
|
|
|
case "openData":
|
2020-05-23 10:32:27 +00:00
|
|
|
let uuid = arguments!["uuid"] as! String
|
2019-12-18 20:34:40 +00:00
|
|
|
let options = arguments!["options"] as! [String: Any?]
|
|
|
|
let data = arguments!["data"] as! String
|
|
|
|
let mimeType = arguments!["mimeType"] as! String
|
|
|
|
let encoding = arguments!["encoding"] as! String
|
|
|
|
let baseUrl = arguments!["baseUrl"] as! String
|
2020-05-21 01:34:39 +00:00
|
|
|
let contextMenu = arguments!["contextMenu"] as! [String: Any]
|
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
|
|
|
let windowId = arguments!["windowId"] as? Int64
|
2021-02-01 14:55:27 +00:00
|
|
|
let initialUserScripts = arguments!["initialUserScripts"] as? [[String: Any]]
|
|
|
|
openData(uuid: uuid, options: options, data: data, mimeType: mimeType, encoding: encoding, baseUrl: baseUrl,
|
|
|
|
contextMenu: contextMenu, windowId: windowId, initialUserScripts: initialUserScripts)
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public func prepareInAppBrowserWebViewController(options: [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
|
|
|
}
|
|
|
|
|
|
|
|
let browserOptions = InAppBrowserOptions()
|
|
|
|
let _ = browserOptions.parse(options: options)
|
|
|
|
|
|
|
|
let webViewOptions = InAppWebViewOptions()
|
|
|
|
let _ = webViewOptions.parse(options: options)
|
|
|
|
|
2021-02-22 11:16:23 +00:00
|
|
|
let webViewController = InAppBrowserWebViewController()
|
2019-12-18 20:34:40 +00:00
|
|
|
webViewController.browserOptions = browserOptions
|
|
|
|
webViewController.webViewOptions = webViewOptions
|
|
|
|
webViewController.previousStatusBarStyle = previousStatusBarStyle
|
|
|
|
return webViewController
|
|
|
|
}
|
|
|
|
|
2021-02-22 11:16:23 +00:00
|
|
|
public func openUrlRequest(uuid: String, urlRequest: [String:Any?], options: [String: Any?],
|
2021-02-01 14:55:27 +00:00
|
|
|
contextMenu: [String: Any], windowId: Int64?, initialUserScripts: [[String: Any]]?) {
|
2019-12-18 20:34:40 +00:00
|
|
|
let webViewController = prepareInAppBrowserWebViewController(options: options)
|
|
|
|
|
|
|
|
webViewController.uuid = uuid
|
2021-02-22 11:16:23 +00:00
|
|
|
webViewController.initialUrlRequest = URLRequest.init(fromPluginMap: urlRequest)
|
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 ?? []
|
2019-12-18 20:34:40 +00:00
|
|
|
|
2021-02-22 11:16:23 +00:00
|
|
|
presentViewController(webViewController: webViewController)
|
|
|
|
}
|
|
|
|
|
|
|
|
public func openFile(uuid: String, assetFilePath: String, options: [String: Any?],
|
|
|
|
contextMenu: [String: Any], windowId: Int64?, initialUserScripts: [[String: Any]]?) {
|
|
|
|
let webViewController = prepareInAppBrowserWebViewController(options: options)
|
|
|
|
|
|
|
|
webViewController.uuid = uuid
|
|
|
|
webViewController.initialFile = assetFilePath
|
|
|
|
webViewController.contextMenu = contextMenu
|
|
|
|
webViewController.windowId = windowId
|
|
|
|
webViewController.initialUserScripts = initialUserScripts ?? []
|
|
|
|
|
|
|
|
presentViewController(webViewController: webViewController)
|
2019-12-18 20:34:40 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
public func openData(uuid: String, options: [String: Any?], data: String, mimeType: String, encoding: String,
|
2021-02-01 14:55:27 +00:00
|
|
|
baseUrl: String, contextMenu: [String: Any], windowId: Int64?, initialUserScripts: [[String: Any]]?) {
|
2019-12-18 20:34:40 +00:00
|
|
|
let webViewController = prepareInAppBrowserWebViewController(options: options)
|
|
|
|
|
|
|
|
webViewController.uuid = uuid
|
2021-02-22 11:16:23 +00:00
|
|
|
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 ?? []
|
2019-12-18 20:34:40 +00:00
|
|
|
|
2021-02-22 11:16:23 +00:00
|
|
|
presentViewController(webViewController: webViewController)
|
|
|
|
}
|
|
|
|
|
|
|
|
public func presentViewController(webViewController: InAppBrowserWebViewController) {
|
|
|
|
let storyboard = UIStoryboard(name: WEBVIEW_STORYBOARD, bundle: Bundle(for: InAppWebViewFlutterPlugin.self))
|
|
|
|
let navController = storyboard.instantiateViewController(withIdentifier: NAV_STORYBOARD_CONTROLLER_ID) as! InAppBrowserNavigationController
|
|
|
|
webViewController.edgesForExtendedLayout = []
|
|
|
|
navController.pushViewController(webViewController, animated: false)
|
|
|
|
webViewController.prepareNavigationControllerBeforeViewWillAppear()
|
|
|
|
|
|
|
|
let frame: CGRect = UIScreen.main.bounds
|
|
|
|
let tmpWindow = UIWindow(frame: frame)
|
|
|
|
|
|
|
|
let tmpController = UIViewController()
|
|
|
|
let baseWindowLevel = UIApplication.shared.keyWindow?.windowLevel
|
|
|
|
tmpWindow.rootViewController = tmpController
|
|
|
|
tmpWindow.windowLevel = UIWindow.Level(baseWindowLevel!.rawValue + 1.0)
|
|
|
|
tmpWindow.makeKeyAndVisible()
|
|
|
|
navController.tmpWindow = tmpWindow
|
|
|
|
|
|
|
|
var animated = true
|
|
|
|
if let browserOptions = webViewController.browserOptions, browserOptions.hidden {
|
|
|
|
tmpWindow.isHidden = true
|
|
|
|
UIApplication.shared.delegate?.window??.makeKeyAndVisible()
|
|
|
|
animated = false
|
2019-12-18 20:34:40 +00:00
|
|
|
}
|
2021-02-22 11:16:23 +00:00
|
|
|
tmpWindow.rootViewController!.present(navController, animated: animated, completion: nil)
|
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, *) {
|
2021-02-22 11:16:23 +00:00
|
|
|
UIApplication.shared.open(absoluteUrl, options: [:], completionHandler: nil)
|
2019-12-18 20:34:40 +00:00
|
|
|
} else {
|
|
|
|
UIApplication.shared.openURL(absoluteUrl)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result(true)
|
|
|
|
}
|
|
|
|
}
|