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 talaviram avatar

Stargazers

Arpit Williams avatar  avatar Pyae Phyo Aung (Austin) avatar Kuba Tomiczek avatar  avatar Adriano Cosme avatar Lee Ye Joo avatar TheOne avatar  avatar  avatar Celil Bozkurt avatar  avatar Halil Ibrahim Oztekin avatar Awais avatar  avatar Ethan Parker avatar  avatar  avatar Diego Rodriguez avatar Çağatay ÇÜRÜK avatar Laurent Delorme avatar Hong avatar  avatar Abdulkadir Aktas avatar 539 avatar  avatar Baby King avatar Mojo Dojo Sen Sen avatar buzai avatar Jens Lee avatar Webserveis avatar Indrajit Chakrabarty avatar Rami avatar Fatih Can avatar Berkay Oruç avatar  avatar Dmitriy avatar ZiBY avatar SamuelShu avatar  avatar jinhu avatar

Watchers

 avatar  avatar

admobswiftui's Issues

AdChoices (or Ads by Google) Icon

I was testing a native banner ad and I noticed there is no AdChoices Icon, which is required.

image image

Native ads policies & guidelines

import SwiftUI
import AdmobSwiftUI

struct NativeAd: View {
    @StateObject private var nativeViewModel = NativeAdViewModel(requestInterval: 0)
    
    var isLoaded: Bool {
        nativeViewModel.nativeAd != nil
    }
    
    var isLoading: Bool {
        nativeViewModel.isLoading
    }
    
    var noErrors: Bool {
        isLoaded || isLoading
    }
    
    var body: some View {
        ZStack {
            if isLoading {
                ProgressView()
                    .frame(maxWidth: .infinity, alignment: .center)
            } else if isLoaded {
                NativeAdView(nativeViewModel: nativeViewModel, style: .banner)
                    .contextMenu {
                        Button("Refresh"){
                            nativeViewModel.refreshAd()
                        }
                    }
            }
            // how to hide empty ZStack in a List?
        }
        .frame(height: 60)
        .listRowInsets(EdgeInsets())
        .onAppear {
            if !isLoaded {
                nativeViewModel.refreshAd()
            }
        }
    }
}

Thanks for the library!

AdmobSwiftUI broken with 11.1.0 or newer / Xcode 15.3

When trying to build under Xcode 15.3 and running on the simulator I get error ending up with:

GoogleMobileAds.framework/Info.plist: No such file or directory

I can enforce to add 11.0.1 (4668daa078c7e104df9f5b8a80a7638cab19bb4e) as additional package.
But then the app won't validate with AppStore, I suspect due to "older" binary not permitted?

image

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.