Skip to content

Commit

Permalink
feat: always have an initialContext (#54)
Browse files Browse the repository at this point in the history
feat: Previously, if no initialContext no was passed to a portal, the web side of the portal would not have access to its name property. This change allows for the portal to have access to its name regardless of there being an initial value passed in or not.
  • Loading branch information
Steven0351 authored Nov 7, 2022
1 parent 4237831 commit 20af832
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions Sources/IonicPortals/PortalUIView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,16 @@ public class PortalUIView: UIView {
}

override func loadInitialContext(_ userContentViewController: WKUserContentController) {
guard portal.initialContext.isNotEmpty,
let jsonData = try? JSONSerialization.data(withJSONObject: portal.initialContext),
let jsonString = String(data: jsonData, encoding: .utf8)
else { return }
let portalInitialContext: String

if portal.initialContext.isNotEmpty,
let jsonData = try? JSONSerialization.data(withJSONObject: portal.initialContext),
let jsonString = String(data: jsonData, encoding: .utf8) {
portalInitialContext = #"{ "name": "\#(portal.name)", "value": \#(jsonString) }"#
} else {
portalInitialContext = #"{ "name": "\#(portal.name)" }"#
}

let portalInitialContext = #"{ "name": "\#(portal.name)", "value": \#(jsonString) }"#
let scriptSource = "window.portalInitialContext = " + portalInitialContext

let userScript = WKUserScript(
Expand Down

0 comments on commit 20af832

Please sign in to comment.