mirror of
https://github.com/hub-team/OnlineChatSdk-SwiftPM.git
synced 2026-04-14 16:53:52 +00:00
update 0.0.9
This commit is contained in:
@@ -11,23 +11,24 @@ import WebKit
|
|||||||
import AVFoundation
|
import AVFoundation
|
||||||
|
|
||||||
open class ChatController: UIViewController, WKNavigationDelegate, WKScriptMessageHandler {
|
open class ChatController: UIViewController, WKNavigationDelegate, WKScriptMessageHandler {
|
||||||
|
|
||||||
|
public static let event_operatorSendMessage = "operatorSendMessage"
|
||||||
|
public static let event_clientSendMessage = "clientSendMessage"
|
||||||
|
public static let event_clientMakeSubscribe = "clientMakeSubscribe"
|
||||||
|
public static let event_contactsUpdated = "contactsUpdated"
|
||||||
|
public static let event_sendRate = "sendRate"
|
||||||
|
public static let event_clientId = "clientId"
|
||||||
|
public static let event_closeSupport = "closeSupport"
|
||||||
|
|
||||||
public static let event_operatorSendMessage = "operatorSendMessage";
|
public static let method_setClientInfo = "setClientInfo"
|
||||||
public static let event_clientSendMessage = "clientSendMessage";
|
public static let method_setTarget = "setTarget"
|
||||||
public static let event_clientMakeSubscribe = "clientMakeSubscribe";
|
public static let method_openReviewsTab = "openReviewsTab"
|
||||||
public static let event_contactsUpdated = "contactsUpdated";
|
public static let method_openTab = "openTab"
|
||||||
public static let event_sendRate = "sendRate";
|
public static let method_sendMessage = "sendMessage"
|
||||||
public static let event_clientId = "clientId";
|
public static let method_receiveMessage = "receiveMessage"
|
||||||
public static let event_closeSupport = "closeSupport";
|
public static let method_setOperator = "setOperator"
|
||||||
|
public static let method_getContacts = "getContacts"
|
||||||
public static let method_setClientInfo = "setClientInfo";
|
private static let method_destroy = "destroy"
|
||||||
public static let method_setTarget = "setTarget";
|
|
||||||
public static let method_openReviewsTab = "openReviewsTab";
|
|
||||||
public static let method_openTab = "openTab";
|
|
||||||
public static let method_sendMessage = "sendMessage";
|
|
||||||
public static let method_receiveMessage = "receiveMessage";
|
|
||||||
public static let method_setOperator = "setOperator";
|
|
||||||
public static let method_getContacts = "getContacts";
|
|
||||||
|
|
||||||
public var chatView: WKWebView!
|
public var chatView: WKWebView!
|
||||||
private var callJs: Array<String>!
|
private var callJs: Array<String>!
|
||||||
@@ -141,7 +142,6 @@ open class ChatController: UIViewController, WKNavigationDelegate, WKScriptMessa
|
|||||||
config.userContentController = contentController
|
config.userContentController = contentController
|
||||||
config.preferences = preferences
|
config.preferences = preferences
|
||||||
|
|
||||||
|
|
||||||
var frame = UIScreen.main.bounds
|
var frame = UIScreen.main.bounds
|
||||||
if parent != nil && parent?.view != nil && parent?.view.bounds != nil {
|
if parent != nil && parent?.view != nil && parent?.view.bounds != nil {
|
||||||
frame = (parent?.view.bounds)!
|
frame = (parent?.view.bounds)!
|
||||||
@@ -149,19 +149,8 @@ open class ChatController: UIViewController, WKNavigationDelegate, WKScriptMessa
|
|||||||
chatView = WKWebView(frame: frame, configuration: config)
|
chatView = WKWebView(frame: frame, configuration: config)
|
||||||
chatView.navigationDelegate = self
|
chatView.navigationDelegate = self
|
||||||
view = chatView
|
view = chatView
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
override public func viewWillAppear(_ animated: Bool) {
|
|
||||||
super.viewWillAppear(animated)
|
|
||||||
navigationController?.setNavigationBarHidden(true, animated: animated)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override public func viewWillDisappear(_ animated: Bool) {
|
|
||||||
super.viewWillDisappear(animated)
|
|
||||||
navigationController?.setNavigationBarHidden(false, animated: animated)
|
|
||||||
}
|
|
||||||
|
|
||||||
public func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
|
public func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
|
||||||
didFinish = true
|
didFinish = true
|
||||||
if callJs != nil && !callJs.isEmpty {
|
if callJs != nil && !callJs.isEmpty {
|
||||||
@@ -220,7 +209,7 @@ open class ChatController: UIViewController, WKNavigationDelegate, WKScriptMessa
|
|||||||
return "{}"
|
return "{}"
|
||||||
}
|
}
|
||||||
|
|
||||||
public func load(_ id: String, _ domain: String, _ language: String = "", _ clientId: String = "", _ apiToken: String = "") {
|
public func load(_ id: String, _ domain: String, _ language: String = "", _ clientId: String = "", _ apiToken: String = "", _ showCloseButton: Bool = false) {
|
||||||
if apiToken != "" {
|
if apiToken != "" {
|
||||||
ChatConfig.setApiToken(apiToken)
|
ChatConfig.setApiToken(apiToken)
|
||||||
}
|
}
|
||||||
@@ -236,20 +225,27 @@ open class ChatController: UIViewController, WKNavigationDelegate, WKScriptMessa
|
|||||||
var url = URL(string: widgetUrl)
|
var url = URL(string: widgetUrl)
|
||||||
var urlComponents = URLComponents(url: url!, resolvingAgainstBaseURL: false)
|
var urlComponents = URLComponents(url: url!, resolvingAgainstBaseURL: false)
|
||||||
if !setup.isEmpty {
|
if !setup.isEmpty {
|
||||||
urlComponents?.queryItems = [
|
if (showCloseButton) {
|
||||||
URLQueryItem(name: "setup", value: toJson(setup as AnyObject)),
|
urlComponents?.queryItems = [
|
||||||
URLQueryItem(name: "sdk-show-close-button", value: "1")
|
URLQueryItem(name: "setup", value: toJson(setup as AnyObject)),
|
||||||
]
|
URLQueryItem(name: "sdk-show-close-button", value: "1")
|
||||||
|
]
|
||||||
|
} else {
|
||||||
|
urlComponents?.queryItems = [
|
||||||
|
URLQueryItem(name: "setup", value: toJson(setup as AnyObject))
|
||||||
|
]
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
urlComponents?.queryItems = [
|
if (showCloseButton) {
|
||||||
URLQueryItem(name: "sdk-show-close-button", value: "1")
|
urlComponents?.queryItems = [
|
||||||
]
|
URLQueryItem(name: "sdk-show-close-button", value: "1")
|
||||||
|
]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
url = urlComponents!.url!
|
url = urlComponents!.url!
|
||||||
if url == nil {
|
if url == nil {
|
||||||
url = URL(string: widgetUrl)
|
url = URL(string: widgetUrl)
|
||||||
}
|
}
|
||||||
|
|
||||||
chatView.load(URLRequest(url: url!))
|
chatView.load(URLRequest(url: url!))
|
||||||
chatView.allowsBackForwardNavigationGestures = true
|
chatView.allowsBackForwardNavigationGestures = true
|
||||||
}
|
}
|
||||||
@@ -296,21 +292,26 @@ open class ChatController: UIViewController, WKNavigationDelegate, WKScriptMessa
|
|||||||
public func callJsGetContacts() {
|
public func callJsGetContacts() {
|
||||||
callJsMethod(ChatController.method_getContacts, params: [Command("window.getContactsCallback")])
|
callJsMethod(ChatController.method_getContacts, params: [Command("window.getContactsCallback")])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private func callJsDestroy() {
|
||||||
|
callJsMethod(ChatController.method_destroy, params: [])
|
||||||
|
}
|
||||||
|
|
||||||
public func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
|
public func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
|
||||||
if message.name != "chatInterface" {
|
if message.name != "chatInterface" {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
let jsonBody = (message.body as! String).data(using: .utf8)!
|
let jsonBody = (message.body as! String).data(using: .utf8)!
|
||||||
let body = try? (JSONSerialization.jsonObject(with: jsonBody, options: .mutableLeaves) as! NSDictionary)
|
let body = try? (JSONSerialization.jsonObject(with: jsonBody, options: .mutableLeaves) as! NSDictionary)
|
||||||
if body == nil {
|
if body == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if body!["name"] == nil {
|
if body!["name"] == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if chatView == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
var data: NSDictionary?
|
var data: NSDictionary?
|
||||||
if body!["data"] != nil {
|
if body!["data"] != nil {
|
||||||
data = (body!["data"] as! NSDictionary)
|
data = (body!["data"] as! NSDictionary)
|
||||||
@@ -350,10 +351,20 @@ open class ChatController: UIViewController, WKNavigationDelegate, WKScriptMessa
|
|||||||
}
|
}
|
||||||
onEvent(name, data!)
|
onEvent(name, data!)
|
||||||
}
|
}
|
||||||
|
|
||||||
open func onCloseSupport() {
|
open func onCloseSupport() {
|
||||||
navigationController?.popViewController(animated: true)
|
if chatView == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
dismiss(animated: true, completion: nil)
|
dismiss(animated: true, completion: nil)
|
||||||
|
navigationController?.popViewController(animated: true)
|
||||||
|
}
|
||||||
|
|
||||||
|
open override func viewDidDisappear(_ animated: Bool) {
|
||||||
|
super.viewDidDisappear(animated)
|
||||||
|
chatView.stopLoading()
|
||||||
|
callJsDestroy()
|
||||||
|
chatView = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
open func onLinkPressed(url: URL) {
|
open func onLinkPressed(url: URL) {
|
||||||
|
|||||||
Reference in New Issue
Block a user