Skip to content

Commit b129eaa

Browse files
committed
Port in-app fix
1 parent 9c29822 commit b129eaa

File tree

2 files changed

+41
-31
lines changed

2 files changed

+41
-31
lines changed

host-app/ViewController.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class ViewController: UIViewController {
109109
config.inAppDelegate = mockInAppDelegate
110110
let payload = ["inAppMessages" : [[
111111
"content" : [
112-
"html" : "<a href='https://www.google.com/q=something'>Click Here</a>",
112+
"html" : "<body style='height:100px'><a href='https://www.google.com/q=something'>Click Here</a></body><meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no'>",
113113
"inAppDisplaySettings" : ["backgroundAlpha" : 0.5, "left" : ["percentage" : 60], "right" : ["percentage" : 60], "bottom" : ["displayOption" : "AutoExpand"], "top" : ["displayOption" : "AutoExpand"]]
114114
],
115115
"messageId" : "messageId",
@@ -167,7 +167,10 @@ class ViewController: UIViewController {
167167

168168
let messageId = "zeeMessageId"
169169
let html = """
170+
<body style='height:100px'>
170171
<a href="http://website/resource#something">Click Me</a>
172+
<meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no'>
173+
</body>
171174
"""
172175
let content = IterableHtmlInAppContent(edgeInsets: UIEdgeInsets(top: -1, left: 10, bottom: -1, right: 10), backgroundAlpha: 0.5, html: html)
173176
let message = IterableInAppMessage(messageId: messageId, campaignId: "zeeCampaignId", content: content)

swift-sdk/Internal/IterableInAppHTMLViewController.swift

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -122,21 +122,13 @@ class IterableInAppHTMLViewController: UIViewController {
122122
}
123123

124124
override var prefersStatusBarHidden: Bool {return true}
125-
126-
override func viewWillLayoutSubviews() {
127-
super.viewWillLayoutSubviews()
128-
if let webView = webView {
129-
resizeWebView(webView)
130-
}
131-
}
132125

133126
private let htmlString: String
134127
private var insetPadding: UIEdgeInsets = UIEdgeInsets.zero
135128
private var customBlockCallback: ITBURLCallback?
136129
private var trackParams: IterableNotificationMetadata?
137130
private var webView: WKWebView?
138131
private var location: InAppNotificationType = .full
139-
private var loaded = false
140132

141133
required init?(coder aDecoder: NSCoder) {
142134
self.htmlString = aDecoder.decodeObject(forKey: "htmlString") as? String ?? ""
@@ -150,47 +142,62 @@ class IterableInAppHTMLViewController: UIViewController {
150142
- parameter: aWebView the webview
151143
*/
152144
private func resizeWebView(_ aWebView: WKWebView) {
153-
guard loaded else {
154-
return
155-
}
156145
guard location != .full else {
157146
webView?.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height)
158147
return
159148
}
160149

161-
// Resizes the frame to match the HTML content with a max of the screen size.
162-
var frame = aWebView.frame
163-
frame.size.height = 1
164-
aWebView.frame = frame;
165-
let fittingSize = aWebView.scrollView.contentSize
166-
frame.size = fittingSize
150+
aWebView.evaluateJavaScript("document.body.offsetHeight", completionHandler: { height, _ in
151+
guard let floatHeight = height as? CGFloat, floatHeight >= 20 else {
152+
ITBError("unable to get height")
153+
return
154+
}
155+
self.resize(webView: aWebView, withHeight: floatHeight)
156+
})
157+
}
158+
159+
private func resize(webView: WKWebView, withHeight height: CGFloat) {
160+
ITBInfo("height: \(height)")
161+
// set the height
162+
webView.frame.size.height = height
163+
164+
// now set the width
167165
let notificationWidth = 100 - (insetPadding.left + insetPadding.right)
168166
let screenWidth = view.bounds.width
169-
frame.size.width = screenWidth*notificationWidth/100
170-
frame.size.height = min(frame.height, self.view.bounds.height)
171-
aWebView.frame = frame;
167+
webView.frame.size.width = screenWidth * notificationWidth / 100
172168

173-
let resizeCenterX = screenWidth*(insetPadding.left + notificationWidth/2)/100
174-
175169
// Position webview
176-
var center = self.view.center
177-
let webViewHeight = aWebView.frame.height/2
170+
var center = view.center
171+
172+
// set center x
173+
center.x = screenWidth * (insetPadding.left + notificationWidth / 2) / 100
174+
175+
// set center y
176+
let halfWebViewHeight = webView.frame.height / 2
178177
switch location {
179178
case .top:
180-
center.y = webViewHeight
179+
if #available(iOS 11, *) {
180+
center.y = halfWebViewHeight + view.safeAreaInsets.top
181+
} else {
182+
center.y = halfWebViewHeight
183+
}
181184
case .bottom:
182-
center.y = view.frame.height - webViewHeight
183-
case .center,.full: break
185+
if #available(iOS 11, *) {
186+
center.y = view.frame.height - halfWebViewHeight - view.safeAreaInsets.bottom
187+
} else {
188+
center.y = view.frame.height - halfWebViewHeight
189+
}
190+
default: break
184191
}
185-
center.x = resizeCenterX;
186-
aWebView.center = center;
192+
193+
webView.center = center
187194
}
188195
}
189196

190197
extension IterableInAppHTMLViewController : WKNavigationDelegate {
191198
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
192199
ITBInfo()
193-
loaded = true
200+
194201
if let myWebview = self.webView {
195202
resizeWebView(myWebview)
196203
}

0 commit comments

Comments
 (0)