Skip to content

Commit bd67dda

Browse files
authored
Merge pull request #257 from Iterable/MOB-1138-in-app-fix
[MOB-1138] Port in-app fix
2 parents 9c29822 + 4caf323 commit bd67dda

File tree

7 files changed

+50
-75
lines changed

7 files changed

+50
-75
lines changed

Podfile

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,3 @@ platform :ios, '9.0'
33
target 'swift-sdk-swift-tests' do
44
pod 'OHHTTPStubs/Swift'
55
end
6-
7-
target 'swift-sdk-objc-tests' do
8-
pod 'OHHTTPStubs'
9-
end

Tests/swift-sdk-objc-tests/IterableAPIObjCTests.m

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
#import <XCTest/XCTest.h>
88

99
#import <asl.h>
10-
#import <OHHTTPStubs.h>
11-
#import <OHHTTPStubs/NSURLRequest+HTTPBodyTesting.h>
1210

1311
#import "swift_sdk_objc_tests-Swift.h"
1412

Tests/swift-sdk-swift-tests/DeeplinkTests.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,21 +123,21 @@ class DeeplinkTests: XCTestCase {
123123
}
124124

125125
private func setupRedirectStubResponse(location: String, campaignId: Int, templateId: Int, messageId: String) {
126-
OHHTTPStubs.stubRequests(passingTest: { (request) -> Bool in
126+
HTTPStubs.stubRequests(passingTest: { (request) -> Bool in
127127
return true
128-
}) { (request) -> OHHTTPStubsResponse in
129-
return OHHTTPStubsResponse(data: try! JSONSerialization.data(withJSONObject: [:], options: []), statusCode: 301, headers: [
128+
}) { (request) -> HTTPStubsResponse in
129+
return HTTPStubsResponse(data: try! JSONSerialization.data(withJSONObject: [:], options: []), statusCode: 301, headers: [
130130
"Location" : location,
131131
"Set-Cookie" : self.createCookieValue(nameValuePairs: "iterableEmailCampaignId", campaignId, "iterableTemplateId", templateId, "iterableMessageId", messageId)
132132
])
133133
}
134134
}
135135

136136
private func setupStubResponse() {
137-
OHHTTPStubs.stubRequests(passingTest: { (request) -> Bool in
137+
HTTPStubs.stubRequests(passingTest: { (request) -> Bool in
138138
return true
139-
}) { (request) -> OHHTTPStubsResponse in
140-
return OHHTTPStubsResponse(data: try! JSONSerialization.data(withJSONObject: [:], options: []), statusCode: 200, headers: nil)
139+
}) { (request) -> HTTPStubsResponse in
140+
return HTTPStubsResponse(data: try! JSONSerialization.data(withJSONObject: [:], options: []), statusCode: 200, headers: nil)
141141
}
142142
}
143143

Tests/swift-sdk-swift-tests/IterableAPIResponseTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,10 @@ class IterableAPIResponseTests: XCTestCase {
144144
func testNetworkTimeoutResponse() {
145145
let xpectation = expectation(description: "timeout network response")
146146

147-
OHHTTPStubs.stubRequests(passingTest: { (request) -> Bool in
147+
HTTPStubs.stubRequests(passingTest: { (request) -> Bool in
148148
return true
149-
}) { (request) -> OHHTTPStubsResponse in
150-
let response = OHHTTPStubsResponse(data: try! JSONSerialization.data(withJSONObject: [:], options: []), statusCode: 200, headers: nil)
149+
}) { (request) -> HTTPStubsResponse in
150+
let response = HTTPStubsResponse(data: try! JSONSerialization.data(withJSONObject: [:], options: []), statusCode: 200, headers: nil)
151151
response.requestTime = 0.0
152152
response.responseTime = 2.0
153153
return response

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.xcodeproj/project.pbxproj

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
00B6FACE210E88ED007535CF /* prod-1.mobileprovision in Resources */ = {isa = PBXBuildFile; fileRef = 00B6FACD210E874D007535CF /* prod-1.mobileprovision */; };
1313
00B6FAD1210E8D90007535CF /* dev-1.mobileprovision in Resources */ = {isa = PBXBuildFile; fileRef = 00B6FAD0210E8D90007535CF /* dev-1.mobileprovision */; };
1414
00CB31B621096129004ACDEC /* TestUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 00CB31B4210960C4004ACDEC /* TestUtils.swift */; };
15-
0B7C33B37D3191A8DAAD6A87 /* libPods-swift-sdk-objc-tests.a in Frameworks */ = {isa = PBXBuildFile; fileRef = FD666A015C69498DD477DAEB /* libPods-swift-sdk-objc-tests.a */; };
1615
A5DF819C89A29E7F3A88DC7F /* libPods-swift-sdk-swift-tests.a in Frameworks */ = {isa = PBXBuildFile; fileRef = EA3B895070C574A2D2D4FC80 /* libPods-swift-sdk-swift-tests.a */; };
1716
AC03094B21E532470003A288 /* InAppPersistence.swift in Sources */ = {isa = PBXBuildFile; fileRef = AC03094A21E532470003A288 /* InAppPersistence.swift */; };
1817
AC0674E620D8766600C2806D /* NotificationExtensionTests.m in Sources */ = {isa = PBXBuildFile; fileRef = AC0674E520D8766600C2806D /* NotificationExtensionTests.m */; };
@@ -174,8 +173,6 @@
174173
00B6FACD210E874D007535CF /* prod-1.mobileprovision */ = {isa = PBXFileReference; lastKnownFileType = file; path = "prod-1.mobileprovision"; sourceTree = "<group>"; };
175174
00B6FAD0210E8D90007535CF /* dev-1.mobileprovision */ = {isa = PBXFileReference; lastKnownFileType = file; path = "dev-1.mobileprovision"; sourceTree = "<group>"; };
176175
00CB31B4210960C4004ACDEC /* TestUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestUtils.swift; sourceTree = "<group>"; };
177-
0BDA0B537E9398A298EEBD89 /* Pods-swift-sdk-objc-tests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-swift-sdk-objc-tests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-swift-sdk-objc-tests/Pods-swift-sdk-objc-tests.debug.xcconfig"; sourceTree = "<group>"; };
178-
6BBDFB5DDE58A33B50FB1494 /* Pods-swift-sdk-objc-tests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-swift-sdk-objc-tests.release.xcconfig"; path = "Pods/Target Support Files/Pods-swift-sdk-objc-tests/Pods-swift-sdk-objc-tests.release.xcconfig"; sourceTree = "<group>"; };
179176
789F9E27D756A343F1E4E523 /* libPods-swift-sdk.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-swift-sdk.a"; sourceTree = BUILT_PRODUCTS_DIR; };
180177
AC03094A21E532470003A288 /* InAppPersistence.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InAppPersistence.swift; sourceTree = "<group>"; };
181178
AC0674E420D8766600C2806D /* notification-extensionTests-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "notification-extensionTests-Bridging-Header.h"; sourceTree = "<group>"; };
@@ -264,7 +261,6 @@
264261
AE6D2D1801330BD6AD4BEB45 /* Pods-swift-sdk-swift-tests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-swift-sdk-swift-tests.release.xcconfig"; path = "Pods/Target Support Files/Pods-swift-sdk-swift-tests/Pods-swift-sdk-swift-tests.release.xcconfig"; sourceTree = "<group>"; };
265262
DDD4A68DE6BBB9230E1FF789 /* Pods-swift-sdk-swift-tests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-swift-sdk-swift-tests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-swift-sdk-swift-tests/Pods-swift-sdk-swift-tests.debug.xcconfig"; sourceTree = "<group>"; };
266263
EA3B895070C574A2D2D4FC80 /* libPods-swift-sdk-swift-tests.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-swift-sdk-swift-tests.a"; sourceTree = BUILT_PRODUCTS_DIR; };
267-
FD666A015C69498DD477DAEB /* libPods-swift-sdk-objc-tests.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-swift-sdk-objc-tests.a"; sourceTree = BUILT_PRODUCTS_DIR; };
268264
/* End PBXFileReference section */
269265

270266
/* Begin PBXFrameworksBuildPhase section */
@@ -289,7 +285,6 @@
289285
buildActionMask = 2147483647;
290286
files = (
291287
AC7B143F20D02D8700877BFE /* IterableSDK.framework in Frameworks */,
292-
0B7C33B37D3191A8DAAD6A87 /* libPods-swift-sdk-objc-tests.a in Frameworks */,
293288
);
294289
runOnlyForDeploymentPostprocessing = 0;
295290
};
@@ -611,7 +606,6 @@
611606
children = (
612607
AC98294A20D9D65E00796DAA /* Foundation.framework */,
613608
789F9E27D756A343F1E4E523 /* libPods-swift-sdk.a */,
614-
FD666A015C69498DD477DAEB /* libPods-swift-sdk-objc-tests.a */,
615609
EA3B895070C574A2D2D4FC80 /* libPods-swift-sdk-swift-tests.a */,
616610
);
617611
name = Frameworks;
@@ -620,8 +614,6 @@
620614
C83BAB75B2E351B936BA5F68 /* Pods */ = {
621615
isa = PBXGroup;
622616
children = (
623-
0BDA0B537E9398A298EEBD89 /* Pods-swift-sdk-objc-tests.debug.xcconfig */,
624-
6BBDFB5DDE58A33B50FB1494 /* Pods-swift-sdk-objc-tests.release.xcconfig */,
625617
DDD4A68DE6BBB9230E1FF789 /* Pods-swift-sdk-swift-tests.debug.xcconfig */,
626618
AE6D2D1801330BD6AD4BEB45 /* Pods-swift-sdk-swift-tests.release.xcconfig */,
627619
);
@@ -692,7 +684,6 @@
692684
isa = PBXNativeTarget;
693685
buildConfigurationList = AC7B144220D02D8700877BFE /* Build configuration list for PBXNativeTarget "swift-sdk-objc-tests" */;
694686
buildPhases = (
695-
3EE7B5E67CA533B68064E3B1 /* [CP] Check Pods Manifest.lock */,
696687
AC7B143620D02D8700877BFE /* Sources */,
697688
AC7B143720D02D8700877BFE /* Frameworks */,
698689
AC7B143820D02D8700877BFE /* Resources */,
@@ -915,24 +906,6 @@
915906
/* End PBXResourcesBuildPhase section */
916907

917908
/* Begin PBXShellScriptBuildPhase section */
918-
3EE7B5E67CA533B68064E3B1 /* [CP] Check Pods Manifest.lock */ = {
919-
isa = PBXShellScriptBuildPhase;
920-
buildActionMask = 2147483647;
921-
files = (
922-
);
923-
inputPaths = (
924-
"${PODS_PODFILE_DIR_PATH}/Podfile.lock",
925-
"${PODS_ROOT}/Manifest.lock",
926-
);
927-
name = "[CP] Check Pods Manifest.lock";
928-
outputPaths = (
929-
"$(DERIVED_FILE_DIR)/Pods-swift-sdk-objc-tests-checkManifestLockResult.txt",
930-
);
931-
runOnlyForDeploymentPostprocessing = 0;
932-
shellPath = /bin/sh;
933-
shellScript = "diff \"${PODS_PODFILE_DIR_PATH}/Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n# This output is used by Xcode 'outputs' to avoid re-running this script phase.\necho \"SUCCESS\" > \"${SCRIPT_OUTPUT_FILE_0}\"\n";
934-
showEnvVarsInLog = 0;
935-
};
936909
684E19F9B5AC1760CE8EE6C0 /* [CP] Check Pods Manifest.lock */ = {
937910
isa = PBXShellScriptBuildPhase;
938911
buildActionMask = 2147483647;
@@ -1367,7 +1340,6 @@
13671340
};
13681341
AC7B144320D02D8700877BFE /* Debug */ = {
13691342
isa = XCBuildConfiguration;
1370-
baseConfigurationReference = 0BDA0B537E9398A298EEBD89 /* Pods-swift-sdk-objc-tests.debug.xcconfig */;
13711343
buildSettings = {
13721344
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
13731345
CLANG_ENABLE_MODULES = YES;
@@ -1400,7 +1372,6 @@
14001372
};
14011373
AC7B144420D02D8700877BFE /* Release */ = {
14021374
isa = XCBuildConfiguration;
1403-
baseConfigurationReference = 6BBDFB5DDE58A33B50FB1494 /* Pods-swift-sdk-objc-tests.release.xcconfig */;
14041375
buildSettings = {
14051376
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
14061377
CLANG_ENABLE_MODULES = YES;

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)