11
22import UIKit
3+ import Rokt_Widget
4+ import mParticle_Apple_SDK
35
46class HomeViewController : UIViewController {
57
@@ -25,7 +27,6 @@ class HomeViewController: UIViewController {
2527 private lazy var stackView : UIStackView = {
2628 let stackView = UIStackView ( )
2729 stackView. axis = . vertical
28- stackView. spacing = 8
2930 stackView. translatesAutoresizingMaskIntoConstraints = false
3031 stackView. distribution = . fill
3132 return stackView
@@ -62,40 +63,107 @@ class HomeViewController: UIViewController {
6263
6364 private let captionLabel : UILabel = {
6465 let label = UILabel ( )
65- label. text = " ® Rokt 2024 — All rights reserved "
66+ label. text = " ® Rokt 2025 — All rights reserved "
6667 label. font = . systemFont( ofSize: 12 , weight: . regular)
6768 label. textColor = . textColor
6869 label. textAlignment = . center
6970 label. numberOfLines = 0
71+ label. translatesAutoresizingMaskIntoConstraints = false
7072 return label
7173 } ( )
7274
75+ private let spacer : UIView = {
76+ let view = UIView ( )
77+ view. setContentHuggingPriority ( . defaultLow, for: . vertical)
78+ view. setContentCompressionResistancePriority ( . defaultLow, for: . vertical)
79+ return view
80+ } ( )
81+
82+ private lazy var roktEmbeddedView : RoktEmbeddedView = RoktEmbeddedView ( )
83+
84+ private lazy var loadingIndicator : UIActivityIndicatorView = {
85+ let indicator = UIActivityIndicatorView ( style: . medium)
86+ indicator. hidesWhenStopped = true
87+ indicator. isHidden = true
88+ indicator. translatesAutoresizingMaskIntoConstraints = false
89+
90+ view. addSubview ( indicator)
91+ NSLayoutConstraint . activate ( [
92+ indicator. centerXAnchor. constraint ( equalTo: view. centerXAnchor) ,
93+ indicator. centerYAnchor. constraint ( equalTo: view. centerYAnchor)
94+ ] )
95+ return indicator
96+ } ( )
97+
7398 override func viewWillAppear( _ animated: Bool ) {
7499 super. viewWillAppear ( animated)
75100 view. backgroundColor = . white
76101 view. addSubview ( stackView)
102+ view. addSubview ( captionLabel)
77103
78104 [
79105 imageView,
80106 titleLabel,
81- UIView ( ) ,
107+ spacer ,
82108 displayButton,
83- UIView ( ) ,
84- captionLabel
109+ roktEmbeddedView
85110 ] . forEach { stackView. addArrangedSubview ( $0) }
86111
87112 NSLayoutConstraint . activate ( [
88113 stackView. topAnchor. constraint ( equalTo: view. safeAreaLayoutGuide. topAnchor, constant: Constants . padding) ,
89114 stackView. leadingAnchor. constraint ( equalTo: view. leadingAnchor, constant: Constants . padding) ,
90115 stackView. trailingAnchor. constraint ( equalTo: view. trailingAnchor, constant: - Constants. padding) ,
91- stackView. bottomAnchor. constraint ( equalTo: view. safeAreaLayoutGuide. bottomAnchor, constant: - Constants. padding) ,
116+ stackView. bottomAnchor. constraint ( lessThanOrEqualTo: captionLabel. topAnchor) ,
117+
118+ captionLabel. leadingAnchor. constraint ( equalTo: view. leadingAnchor, constant: Constants . padding) ,
119+ captionLabel. trailingAnchor. constraint ( equalTo: view. trailingAnchor, constant: - Constants. padding) ,
120+ captionLabel. bottomAnchor. constraint ( equalTo: view. safeAreaLayoutGuide. bottomAnchor) ,
92121
93- displayButton. centerYAnchor. constraint ( equalTo: view. centerYAnchor) ,
94- displayButton. heightAnchor. constraint ( equalToConstant: Constants . buttonHeight) ,
122+ displayButton. centerYAnchor. constraint ( equalTo: view. centerYAnchor, constant: Constants . padding) ,
95123 ] )
96124 }
97125
98126 @objc private func buttonTapped( ) {
99- present ( SampleViewController ( ) , animated: true )
127+ let attributes = [
128+ " email " : " j.smith@example.com " ,
129+ " firstname " : " Jenny " ,
130+ " lastname " : " Smith " ,
131+ " mobile " : " (555)867-5309 " ,
132+ " postcode " : " 90210 " ,
133+ " country " : " US "
134+ ]
135+
136+ MParticle
137+ . sharedInstance ( )
138+ . rokt
139+ . selectPlacements (
140+ " RoktEmbeddedExperience " ,
141+ attributes: attributes,
142+ placements: [ " RoktEmbedded1 " : roktEmbeddedView] ,
143+ onLoad: {
144+ // Optional callback for when the Rokt placement loads
145+ } ,
146+ onUnLoad: { [ weak self] in
147+ // Optional callback for when the Rokt placement unloads
148+ self ? . hideButton ( false )
149+ } ,
150+ onShouldShowLoadingIndicator: { [ weak self] in
151+ // Optional callback to show a loading indicator
152+ self ? . loadingIndicator. startAnimating ( )
153+ } ,
154+ onShouldHideLoadingIndicator: { [ weak self] in
155+ self ? . loadingIndicator. stopAnimating ( )
156+ } ,
157+ onEmbeddedSizeChange: { selectedPlacement, widgetHeight in
158+ // Optional callback to get selectedPlacement and height required by the placement every time the height of the placement changes
159+ }
160+ )
161+ hideButton ( true )
162+ }
163+
164+ private func hideButton( _ hide: Bool ) {
165+ roktEmbeddedView. isHidden = !hide
166+ displayButton. isHidden = hide
167+ spacer. isHidden = hide
100168 }
101169}
0 commit comments