Giter Site home page Giter Site logo

admobswiftui's Introduction

AdmobSwiftUI

AdmobSwiftUI is a Swift package that integrates Admob ads into SwiftUI. This package implements several ways to use Admob in SwiftUI:

  • Banner
  • Interstitial
  • App Open
  • Reward
  • Reward Interstitial
  • Native

Requirements

  • iOS 14.0+
  • Google Mobile Ads SDK 10.6.0+

Installation

You can install AdmobSwiftUI using Swift Package Manager by adding the following URL to your project:

https://github.com/dearhui/AdmobSwiftUI.git

Before building the project, make sure you've added the -ObjC flag to the "Other Linker Flags" in the "Build Settings".

Configuration

To use AdmobSwiftUI, you need to add some key values to your Info.plist as required by the Google Mobile Ads SDK. Please refer to the SDK documentation for more details.

Additionally, you need to start the Google Mobile Ads SDK at the start of your app. Add the following code to your @main struct:

@main
struct AdmobSwitUIDemoApp: App {
    
    init() {
        GADMobileAds.sharedInstance().start(completionHandler: nil)
    }
    
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

Usage

First, you need to import AdmobSwiftUI in your SwiftUI file and initialize all the ad components you need.

import SwiftUI
import AdmobSwiftUI

struct ContentView: View {
    @StateObject private var nativeViewModel = NativeAdViewModel()
    private let adViewControllerRepresentable = AdViewControllerRepresentable()
    private let adCoordinator = InterstitialAdCoordinator()
    private let rewardCoordinator = RewardedAdCoordinator()

Then, you can include Banner ads in your view, or show Interstitial or Reward ads when the user performs a certain action.

    var body: some View {
        ScrollView {
            VStack (spacing: 20) {
            
                Button("Show App Open") {
                    Task {
                        do {
                            let ad = try await adCoordinator.loadAppOpenAd()
                            ad.present(fromRootViewController: adViewControllerRepresentable.viewController)
                        } catch {
                            print(error.localizedDescription)
                        }
                    }
                }
                
                Button("Show reward InterstitialAd") {
                    Task {
                        do {
                            let reward = try await rewardCoordinator.loadInterstitialAd()
                            reward.present(fromRootViewController: adViewControllerRepresentable.viewController) {
                                print("Reward amount: \(reward.adReward.amount)")
                            }
                        } catch {
                            print(error.localizedDescription)
                        }
                    }
                }
                
                BannerView()
                    .frame(height: 50)
                
                NativeAdView(nativeViewModel: nativeViewModel)
                    .frame(height: 300) // 250 ~ 300
                    .onAppear {
                        nativeViewModel.refreshAd()
                    }
            }
        }
        .padding()
        .background {
            // Add the adViewControllerRepresentable to the background so it
            // doesn't influence the placement of other views in the view hierarchy.
            adViewControllerRepresentable
                .frame(width: .zero, height: .zero)
        }
    }
}

Note

This package uses Google AdMob, make sure your project has imported and configured the Google Mobile Ads SDK properly.

Contribution

Any form of contribution is welcome, including feature requests, bug reports, or pull requests.

License

AdmobSwiftUI is released under the MIT license. See LICENSE for details.

admobswiftui's People

Contributors

dearhui avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.