Skip to content

Commit

Permalink
Merge pull request #20 from deskpro/fix/remove-sensitive-file
Browse files Browse the repository at this point in the history
Script and webview updates
  • Loading branch information
qsd-faris authored May 15, 2024
2 parents 9a4d9bd + f072147 commit e57d3b8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 40 deletions.
5 changes: 5 additions & 0 deletions Sources/UI/DeskproButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,8 @@ public class DeskproButton: UIButton {
}
}

/// Main app colors.
extension UIColor {
static let deskproLightBlue = UIColor(red: 58/255.0, green: 141/255.0, blue: 222/255.0, alpha: 1.0)
static let deskproDarkBlue = UIColor(red: 5/255.0, green: 100/255.0, blue: 192/255.0, alpha: 1.0)
}
63 changes: 23 additions & 40 deletions Sources/WebView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@ protocol WebViewDelegate: AnyObject {
func eventOccured(_ event: DeskproEvent)
}

/// Main app colors.
extension UIColor {
static let deskproLightBlue = UIColor(red: 58/255.0, green: 141/255.0, blue: 222/255.0, alpha: 1.0)
static let deskproDarkBlue = UIColor(red: 5/255.0, green: 100/255.0, blue: 192/255.0, alpha: 1.0)
}

/// The ViewController hosting WebView for DeskPro Messenger functionality. It manages the WebView, handles WebView configuration, and sets up communication with JavaScript. It also handles WebView lifecycle events and error handling.
final class CustomWebView: UIViewController {

Expand Down Expand Up @@ -47,14 +41,6 @@ final class CustomWebView: UIViewController {
super.viewWillDisappear(true)
delegate?.webViewDismissed()
}

/// Triggered when device changes the interface style to light or dark.
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
if #available(iOS 12.0, *) {
webView.scrollView.backgroundColor = traitCollection.userInterfaceStyle == .dark ? .deskproDarkBlue : .deskproLightBlue
}
}

/// Basic WebView setup.
private final func setupWebView() {
Expand All @@ -78,9 +64,6 @@ final class CustomWebView: UIViewController {
} else {
activityIndicator.style = .whiteLarge
}
if #available(iOS 12.0, *) {
activityIndicator.color = traitCollection.userInterfaceStyle == .dark ? .deskproDarkBlue : .deskproLightBlue
}

view.addSubview(activityIndicator)
}
Expand All @@ -99,12 +82,17 @@ final class CustomWebView: UIViewController {
final func loadUrlInWebView() {
if let url = url {
let request = URLRequest(url: url)
clearCookies()
webView.load(request)
} else {
dprint("Url not provided")
}
}

private final func clearCookies() {
HTTPCookieStorage.shared.cookies?.forEach(HTTPCookieStorage.shared.deleteCookie)
}

private final func showActivityIndicator(show: Bool) {
if show {
activityIndicator.startAnimating()
Expand Down Expand Up @@ -133,17 +121,15 @@ final class CustomWebView: UIViewController {

extension CustomWebView: WKNavigationDelegate {

/// We are allowing users to swipe back to navigate, but only when they leave the chat, e.g. when they tap the "Powered by Deskpro" label which leads to the Deskpro web.
final func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
if let host = navigationAction.request.url?.host {
if host.contains("deskprodemo") {
webView.allowsBackForwardNavigationGestures = false
} else {
webView.allowsBackForwardNavigationGestures = true
}
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
guard case .linkActivated = navigationAction.navigationType,
let url = navigationAction.request.url
else {
decisionHandler(.allow)
return
}

decisionHandler(.allow)
decisionHandler(.cancel)
UIApplication.shared.open(url)
}

final func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
Expand All @@ -152,9 +138,6 @@ extension CustomWebView: WKNavigationDelegate {

DispatchQueue.main.asyncAfter(deadline: .now() + 1) { [weak self] in
self?.showActivityIndicator(show: false)
if #available(iOS 12.0, *) {
webView.scrollView.backgroundColor = self?.traitCollection.userInterfaceStyle == .dark ? .deskproDarkBlue : .deskproLightBlue
}
}
}

Expand Down Expand Up @@ -307,7 +290,7 @@ private enum PostMessageFunctions: String {
private enum InjectionScripts {

static let initAndOpenScript = """
window.DESKPRO_MESSENGER_OPTIONS = {
window.DpMessengerOptions = {
showLauncherButton: false,
openOnInit: true,
userInfo: window.webkit.messageHandlers.iosListener.postMessage("\(PostMessageFunctions.getUserInfo)"),
Expand All @@ -317,16 +300,16 @@ private enum InjectionScripts {
urlCacheableConfig: undefined,
};
window.DESKPRO_MESSENGER_CONNECTION = {
window.DpMessengerConnection = {
parentMethods: {
ready: async (messengerId) => {
const data = await window.DESKPRO_MESSENGER_CONNECTION.childMethods?.init(messengerId, {
showLauncherButton: DESKPRO_MESSENGER_OPTIONS.showLauncherButton,
userInfo: window.DESKPRO_MESSENGER_OPTIONS?.userInfo,
launcherButtonConfig: DESKPRO_MESSENGER_OPTIONS.launcherButtonConfig,
messengerAppConfig: DESKPRO_MESSENGER_OPTIONS.messengerAppConfig,
const data = await window.DpMessengerConnection.childMethods?.init(messengerId, {
showLauncherButton: DpMessengerOptions.showLauncherButton,
userInfo: window.DpMessengerOptions?.userInfo,
launcherButtonConfig: DpMessengerOptions.launcherButtonConfig,
messengerAppConfig: DpMessengerOptions.messengerAppConfig,
parentViewDimensions: "fullscreen",
open: DESKPRO_MESSENGER_OPTIONS.openOnInit,
open: DpMessengerOptions.openOnInit,
});
if (data) {
Expand All @@ -335,7 +318,7 @@ private enum InjectionScripts {
const deviceToken = await window.webkit.messageHandlers.iosListener.postMessage("\(PostMessageFunctions.getDeviceToken)");
window.DESKPRO_MESSENGER_CONNECTION.childMethods?.setDeviceToken(messengerId, {
window.DpMessengerConnection.childMethods?.setDeviceToken(messengerId, {
token: deviceToken
});
},
Expand Down Expand Up @@ -365,6 +348,6 @@ private enum InjectionScripts {
"""

static let logoutScript = """
window.DESKPRO_MESSENGER_CONNECTION.childMethods.logout("1");
window.DpMessengerConnection.childMethods.logout("1");
"""
}

0 comments on commit e57d3b8

Please sign in to comment.