This guide provides a comprehensive walkthrough for integrating the MyChips SDK into your iOS application, enabling the display of an engaging offerwall.
Replace "YOUR_USER_ID" with your actual user ID variable or value.
If you do not provide a specific user ID, one will be automatically generated.
4. Displaying the Offerwall
To display the offerwall we provide you with a UIViewController which you can integrate into UIKit or SwiftUI project.
To show the offerwall you need to provide your AdUnitId and a closure which closes the offerwall.
For SwiftUI first we need to wrap the UIViewController:
// Definition of the WebViewWrapper struct, which conforms to UIViewControllerRepresentable.
// UIViewControllerRepresentable allows integrating a UIViewController into a SwiftUI interface.
struct WebViewWrapper: UIViewControllerRepresentable {
// Property for the ad unit identifier, passed during initialization.
let adunitId: String
// Access to the SwiftUI environment to manage presentation mode.
// @Environment provides access to shared values throughout the app.
@Environment(\.presentationMode) var presentationMode
// Method to create and configure the UIViewController (required by UIViewControllerRepresentable).
func makeUIViewController(context: Context) -> UINavigationController {
// Initialize the custom MCWebViewController with the adunitId.
let webViewController = MCWebViewController(
adunitId: adunitId,
// Closure defining the behavior when the webViewController is closed.
// This dismisses the view controller using presentationMode.
onClose: {
presentationMode.wrappedValue.dismiss()
}
)
// Wrap the webViewController inside a UINavigationController.
// This allows navigation functionality if required.
return UINavigationController(rootViewController: webViewController)
}
// Method to update the existing UIViewController with new data (required by UIViewControllerRepresentable).
// Not implemented here as no updates are needed for this functionality.
func updateUIViewController(_ uiViewController: UINavigationController, context: Context) {
}
}
Now we use the wrapper in our view. Since in this example we will use it in a NavigationView we hide the default back button since the offerwall provides its own:
// Definition of the WebViewPage struct, which conforms to the View protocol.
// This struct represents a SwiftUI view that embeds the WebViewWrapper.
struct WebViewPage: View {
// The body property defines the content and layout of the view.
var body: some View {
// Embedding the WebViewWrapper inside the view.
// The WebViewWrapper takes an adunitId as a parameter.
WebViewWrapper(
adunitId: "YOUR_AD_UNIT_ID" // Placeholder for the ad unit identifier.
)
// Hides the back button in the navigation bar for this view.
.navigationBarBackButtonHidden()
}
}
Now we can add this page to our NavigationView and show the offerwall on a button press:
// Definition of the ContentView struct, which conforms to the View protocol.
// This struct represents the main entry point for the SwiftUI interface.
struct ContentView: View {
// The body property defines the layout and content of the view.
var body: some View {
// Embedding the view inside a NavigationView.
// NavigationView provides a navigation interface and allows transitioning between views.
NavigationView {
// Using a VStack to arrange elements vertically.
VStack {
// Adding a NavigationLink that navigates to WebViewPage when tapped.
NavigationLink(destination: WebViewPage()) {
// The label for the NavigationLink, displayed as a button.
Text("Open Offerwall")
}
}
}
}
}
// below is an example of a view controller which you
// can load into your scene and open the offerwall
// on a button press
class FirstViewController: UIViewController {
override func loadView() {
// Create a new UIView instance
let view = UIView()
view.backgroundColor = .white // Set background color
// Create and configure the button
let nextButton = UIButton(type: .system)
nextButton.setTitle("Show Offerwall", for: .normal)
nextButton.addTarget(self, action: #selector(showOfferwall), for: .touchUpInside)
nextButton.translatesAutoresizingMaskIntoConstraints = false // Disable default autoresizing mask
// Add the button to the view
view.addSubview(nextButton)
// Add constraints for button
NSLayoutConstraint.activate([
nextButton.centerXAnchor.constraint(equalTo: view.centerXAnchor),
nextButton.centerYAnchor.constraint(equalTo: view.centerYAnchor)
])
// Set the view of the view controller
self.view = view
}
@objc func showOfferwall() {
let mcWebView = MCWebViewController(
adunitId: "YOUR_AD_UNIT_ID",
onClose: {
self.navigationController?.popViewController(animated: true)
}
)
self.navigationController?.pushViewController(mcWebView, animated: true)
}
}