mirror of
https://github.com/hub-team/OnlineChatSdk-SwiftPM.git
synced 2026-04-15 09:03:05 +00:00
update 0.0.8
This commit is contained in:
@@ -18,6 +18,7 @@ open class ChatController: UIViewController, WKNavigationDelegate, WKScriptMessa
|
|||||||
public static let event_contactsUpdated = "contactsUpdated";
|
public static let event_contactsUpdated = "contactsUpdated";
|
||||||
public static let event_sendRate = "sendRate";
|
public static let event_sendRate = "sendRate";
|
||||||
public static let event_clientId = "clientId";
|
public static let event_clientId = "clientId";
|
||||||
|
public static let event_closeSupport = "closeSupport";
|
||||||
|
|
||||||
public static let method_setClientInfo = "setClientInfo";
|
public static let method_setClientInfo = "setClientInfo";
|
||||||
public static let method_setTarget = "setTarget";
|
public static let method_setTarget = "setTarget";
|
||||||
@@ -142,22 +143,32 @@ open class ChatController: UIViewController, WKNavigationDelegate, WKScriptMessa
|
|||||||
|
|
||||||
|
|
||||||
var frame = UIScreen.main.bounds
|
var frame = UIScreen.main.bounds
|
||||||
if self.parent != nil && self.parent?.view != nil && self.parent?.view.bounds != nil {
|
if parent != nil && parent?.view != nil && parent?.view.bounds != nil {
|
||||||
frame = (self.parent?.view.bounds)!
|
frame = (parent?.view.bounds)!
|
||||||
}
|
}
|
||||||
self.chatView = WKWebView(frame: frame, configuration: config)
|
chatView = WKWebView(frame: frame, configuration: config)
|
||||||
self.chatView.navigationDelegate = self
|
chatView.navigationDelegate = self
|
||||||
self.view = self.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!) {
|
||||||
self.didFinish = true
|
didFinish = true
|
||||||
if self.callJs != nil && !self.callJs.isEmpty {
|
if callJs != nil && !callJs.isEmpty {
|
||||||
for script in self.callJs {
|
for script in callJs {
|
||||||
callJs(script)
|
callJs(script)
|
||||||
}
|
}
|
||||||
self.callJs = nil
|
callJs = nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -169,11 +180,9 @@ open class ChatController: UIViewController, WKNavigationDelegate, WKScriptMessa
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
decisionHandler(.cancel)
|
decisionHandler(.cancel)
|
||||||
self.onLinkPressed(url: navigationAction.request.url!)
|
onLinkPressed(url: navigationAction.request.url!)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private func getCallJsMethod(_ name: String, params: Array<Any>) -> String {
|
private func getCallJsMethod(_ name: String, params: Array<Any>) -> String {
|
||||||
var res: String = "window.MeTalk('"
|
var res: String = "window.MeTalk('"
|
||||||
res.append(name)
|
res.append(name)
|
||||||
@@ -197,7 +206,7 @@ open class ChatController: UIViewController, WKNavigationDelegate, WKScriptMessa
|
|||||||
}
|
}
|
||||||
|
|
||||||
private func callJs(_ script: String) {
|
private func callJs(_ script: String) {
|
||||||
self.chatView.evaluateJavaScript(script)
|
chatView.evaluateJavaScript(script)
|
||||||
}
|
}
|
||||||
|
|
||||||
private func toJson(_ jsonObj: AnyObject) -> String {
|
private func toJson(_ jsonObj: AnyObject) -> String {
|
||||||
@@ -223,29 +232,36 @@ open class ChatController: UIViewController, WKNavigationDelegate, WKScriptMessa
|
|||||||
if !clientId.isEmpty {
|
if !clientId.isEmpty {
|
||||||
setup["clientId"] = clientId
|
setup["clientId"] = clientId
|
||||||
}
|
}
|
||||||
self.widgetUrl = "https://admin.verbox.ru/support/chat/\(id)/\(domain)"
|
widgetUrl = "https://admin.verbox.ru/support/chat/\(id)/\(domain)"
|
||||||
var url = URL(string: self.widgetUrl)
|
var url = URL(string: widgetUrl)
|
||||||
|
var urlComponents = URLComponents(url: url!, resolvingAgainstBaseURL: false)
|
||||||
if !setup.isEmpty {
|
if !setup.isEmpty {
|
||||||
var urlComponents = URLComponents(url: url!, resolvingAgainstBaseURL: false)
|
urlComponents?.queryItems = [
|
||||||
urlComponents?.queryItems = [URLQueryItem(name: "setup", value: toJson(setup as AnyObject))]
|
URLQueryItem(name: "setup", value: toJson(setup as AnyObject)),
|
||||||
url = urlComponents!.url!
|
URLQueryItem(name: "sdk-show-close-button", value: "1")
|
||||||
|
]
|
||||||
|
} else {
|
||||||
|
urlComponents?.queryItems = [
|
||||||
|
URLQueryItem(name: "sdk-show-close-button", value: "1")
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
url = urlComponents!.url!
|
||||||
if url == nil {
|
if url == nil {
|
||||||
url = URL(string: self.widgetUrl)
|
url = URL(string: widgetUrl)
|
||||||
}
|
}
|
||||||
|
|
||||||
self.chatView.load(URLRequest(url: url!))
|
chatView.load(URLRequest(url: url!))
|
||||||
self.chatView.allowsBackForwardNavigationGestures = true
|
chatView.allowsBackForwardNavigationGestures = true
|
||||||
}
|
}
|
||||||
|
|
||||||
public func callJsMethod(_ name: String, params: Array<Any>) {
|
public func callJsMethod(_ name: String, params: Array<Any>) {
|
||||||
if self.didFinish {
|
if didFinish {
|
||||||
callJs(getCallJsMethod(name, params: params))
|
callJs(getCallJsMethod(name, params: params))
|
||||||
} else {
|
} else {
|
||||||
if self.callJs == nil {
|
if callJs == nil {
|
||||||
self.callJs = []
|
callJs = []
|
||||||
}
|
}
|
||||||
self.callJs.append(getCallJsMethod(name, params: params))
|
callJs.append(getCallJsMethod(name, params: params))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -303,6 +319,9 @@ open class ChatController: UIViewController, WKNavigationDelegate, WKScriptMessa
|
|||||||
}
|
}
|
||||||
let name = body!["name"] as! String
|
let name = body!["name"] as! String
|
||||||
switch name {
|
switch name {
|
||||||
|
case ChatController.event_closeSupport:
|
||||||
|
onCloseSupport()
|
||||||
|
break
|
||||||
case ChatController.event_clientId:
|
case ChatController.event_clientId:
|
||||||
let clientId = data!["clientId"] != nil ? data!["clientId"] as! String : ""
|
let clientId = data!["clientId"] != nil ? data!["clientId"] as! String : ""
|
||||||
ChatConfig.setClientId(clientId)
|
ChatConfig.setClientId(clientId)
|
||||||
@@ -332,6 +351,11 @@ open class ChatController: UIViewController, WKNavigationDelegate, WKScriptMessa
|
|||||||
onEvent(name, data!)
|
onEvent(name, data!)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
open func onCloseSupport() {
|
||||||
|
navigationController?.popViewController(animated: true)
|
||||||
|
dismiss(animated: true, completion: nil)
|
||||||
|
}
|
||||||
|
|
||||||
open func onLinkPressed(url: URL) {
|
open func onLinkPressed(url: URL) {
|
||||||
UIApplication.shared.openURL(url)
|
UIApplication.shared.openURL(url)
|
||||||
}
|
}
|
||||||
@@ -361,7 +385,7 @@ open class ChatController: UIViewController, WKNavigationDelegate, WKScriptMessa
|
|||||||
}
|
}
|
||||||
|
|
||||||
open func onOperatorSendMessage(_ data: NSDictionary) {
|
open func onOperatorSendMessage(_ data: NSDictionary) {
|
||||||
self.playSound(1315)
|
playSound(1315)
|
||||||
}
|
}
|
||||||
|
|
||||||
open func onEvent(_ name: String, _ data: NSDictionary) {
|
open func onEvent(_ name: String, _ data: NSDictionary) {
|
||||||
|
|||||||
Reference in New Issue
Block a user