MobFlow SDK - SwiftUI
MobFlowiOS Integration in SwiftUI App
Overview
MobFlowiOS is a powerful framework for seamlessly integrating advertisements into your SwiftUI app. This documentation provides a step-by-step guide for implementing MobFlowiOS in your SwiftUI project, considering the variations in state objects that may exist based on your app's requirements.
Prerequisites
NOTE: minimum deployment target or supporting OS should be iOS 14.0
Open your project in Xcode and go to the "Podfile" file.
Add the following line to the file:
pod 'MobFlowiOS', :git => 'https://github.com/MASDKManager/MobFlowSwift.git', :tag => '3.3.0'
Save the file and run the command "pod install" in the terminal to install the pod.
Step 1: Initialize MobFlow
In the AppEnvironment
class, initialize MobFlow with the required parameters:
MobiFlowSwift(initDelegate: self,
unityGameId: "your_unity_gameid",
clarityProjectId: "your_clarity_ProjectId",
launchOptions: nil)
Step 2: Customize State Objects (Optional)
The AppEnvironment
class includes several state objects, such as coreDataFunctions
, currencyManager
, and coreManipulation
. You may customize or add additional state objects based on your app's specific needs. For example, you might include a state object for user authentication or app settings.
@StateObject var userAuthManager = UserAuthManager()
@StateObject var appSettings = AppSettings()
Step 3: Display Ads
Utilize the provided methods in AppEnvironment
to display different types of ads in your app:
Display Banner Ad
// Place it within the body property of a SwiftUI view,
// typically at the bottom of a VStack or any other container
if let bannerView = appEnvironment.mobflow?.createBannerView() {
bannerView
.frame(height: 50)
.padding(.top)
}
Display Interestial Ad
appEnvironment.showInterestialAd { success in
// Handle ad closure (e.g., reward the user)
}
Display Rewarded Ad
appEnvironment.showRewardedAd { success in
if success {
// Grant rewards to the user
} else {
// Handle unsuccessful ad view
}
}
Step 4: Handle MobFlow Delegate Methods
Conform to the MobiFlowDelegate
protocol to handle events triggered by the MobFlow SDK. For example, presenting content once the MobFlow SDK is loaded:
extension AppEnvironment: MobiFlowDelegate {
func present(dic: [String : Any]) {
// Handle presentation of content after MobFlow SDK is loaded
}
func unloadUnityOnNotificationClick() {
// Handle unloading Unity on notification click
}
}
Conclusion
By following these steps, you can seamlessly integrate MobFlowiOS into your SwiftUI app, customizing state objects as needed for your specific application. Find below final example of how your code will look:
import SwiftUI
import MobFlowiOS
@main
struct MobFlowSdk_SwiftUIApp: App {
@StateObject var appEnvironment = AppEnvironment.shared
var body: some Scene {
WindowGroup {
SplashView()
}
}
}
class AppEnvironment: ObservableObject {
var mobflow: MobiFlowSwift?
static let shared = AppEnvironment()
init() {
// Initialize MobFlow with necessary parameters
self.mobflow = MobiFlowSwift(initDelegate: self,
unityGameId: "your_unity_gameid",
clarityProjectId: "your_clarity_ProjectId",
launchOptions: nil)
// Note: launchOptions might need to be handled differently in SwiftUI
}
func showInterestialAd(onClose: @escaping (Bool) -> Void) {
mobflow?.showInterestialAd(onClose: onClose)
}
func showRewardedAd(onClose: @escaping (Bool) -> Void) {
mobflow?.showRewardedAd(onClose: onClose)
}
}
extension AppEnvironment: MobiFlowDelegate {
func present(dic: [String : Any]) {
DispatchQueue.main.async { [self] in
//Here once the MobFlow SDK is loaded, and present delegate is called, show here intro/main app content in main thread.
debugPrint("--------------- MobiFlowDelegate present called ---------------")
if let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene {
let mainWindow = windowScene.windows.first
// Use 'mainWindow' as needed
let contentView =
let vc = UIHostingController(rootView: ContentView().environmentObject(self))
mainWindow?.rootViewController = vc
mainWindow?.makeKeyAndVisible()
}
}
}
func unloadUnityOnNotificationClick() {
// Handle unloading Unity on notification click.
}
}
struct SplashView: View {
var body: some View {
ZStack{
Image("splash")
.resizable()
.scaledToFill()
.frame(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
.ignoresSafeArea(edges: .all)
}
}
}
Now continue from “Adding Google Plist” section in main documentation click here.
For a hands-on demonstration, check out our GitHub demo project: MobFlowiOS Demo