1、新建一个OpenAd类:(记得导入GoogleMobileAds) final class OpenAd: NSObject, GADFullScreenContentDelegate { var appOpenAd: GADAppOpenAd? var loadTime = Date() func requestAppOpenAd() { let request = GADRequest() GADAppOpenAd.load(withAdUnitID: "ca-app-pub-3940256099942544/5662855259", request: request, orientation: UIInterfaceOrientation.portrait, completionHandler: { (appOpenAdIn, _) in self.appOpenAd = appOpenAdIn self.appOpenAd?.fullScreenContentDelegate = self self.loadTime = Date() print("[OPEN AD] Ad is ready") }) } func tryToPresentAd() { if let gOpenAd = self.appOpenAd, wasLoadTimeLessThanNHoursAgo(thresholdN: 4) { gOpenAd.present(fromRootViewController: (UIApplication.shared.windows.first?.rootViewController)!) } else { self.requestAppOpenAd() } } func wasLoadTimeLessThanNHoursAgo(thresholdN: Int) -> Bool { let now = Date() let timeIntervalBetweenNowAndLoadTime = now.timeIntervalSince(self.loadTime) let secondsPerHour = 3600.0 let intervalInHours = timeIntervalBetweenNowAndLoadTime / secondsPerHour return intervalInHours < Double(thresholdN) } func ad(_ ad: GADFullScreenPresentingAd, …
书接上文 ~~~~ 激励广告 创建激励广告几乎与插页式广告相同。唯一的区别是,对于奖励广告,我们需要创建一个函数,该函数将在授予奖励时运行。 就像插页式广告一样,我们需要创建一个RewardedAdObject来处理广告的加载。 class RewardedAd: NSObject { var rewardedAd: GADRewardedAd? static let shared = RewardedAd() func loadAd(withAdUnitId id: String) { let req = GADRequest() GADRewardedAd.load(withAdUnitID: id, request: req) { rewardedAd, err in if let err = err { print("Failed to load ad with error: \(err)") return } self.rewardedAd = rewardedAd } } } 现在,我们将使用这个对象来创建RewardedAdView。 class RewardedAdView: NSObject, UIViewControllerRepresentable, GADFullScreenContentDelegate { let rewardedAd = RewardedAd.shared.rewardedAd @Binding var isPresented: Bool let adUnitId: String //This is the variable for our reward function let rewardFunc: (() -> Void) init(isPresented: Binding<Bool>, adUnitId: String, rewardFunc: @escaping (() -> Void)) { self._isPresented = isPresented self.adUnitId …
简介: 随着越来越多的应用程序使用 SwiftUI 构建,我想提供一种简单的方法将 Admob 直接集成到您的应用程序中。为了让大家集成的更容易,我计划将它作为 Cocoapod 发布。 先看一下集成后的效果: 设置 Google 移动广告 (Admob) SDK 申请应用,请参考 Google 的官方文档地址为:https://developers.google.com/admob/ios/quick-start。 使用 Cocoapods 导入 SDK pod ‘Google-Mobile-Ads-SDK’ 然后使用命令行运行 pod install –repo-update 更新 Info.plist 请更新应用的 Info.plist 文件以添加以下两个键: 一个字符串值为您的 AdMob 应用 ID 的 GADApplicationIdentifier 键(在 AdMob 界面中标识)。 一个 SKAdNetworkIdentifier 值为 Google(cstr6suwn9.skadnetwork),并选择向 Google 提供了这些值的其他买家的 SKAdNetworkItems 键。 <key>GADApplicationIdentifier</key> <string>ca-app-pub-3940256099942544~2435281174</string> 初始化 Google 移动广告 (Admob) SDK import SwiftUI import AppTrackingTransparency import GoogleMobileAds @main struct ExampleApp: App { //在 App Delegate 中使用 init() 代替 ApplicationDidFinishLaunchWithOptions init() { if ATTrackingManager.trackingAuthorizationStatus == .notDetermined { // TODO 这里可以弹出隐私协议 } else { ATTrackingManager.requestTrackingAuthorization { status in GADMobileAds.sharedInstance().start(completionHandler: nil) } } } var body: some Scene …
开发环境: xcode版本:13.1 swift版本:5.5.1 swift-driver version: 1.26.9 Apple Swift version 5.5.1 一、缺少文件: 问题: 创建新项目后缺少了 AppDelegate.swift、SceneDelegate.swift 文件,那么在初始化一些SDK时,无法初始化 解决方案: 在项目App.swift文件中加入init()方法: @main struct SwiftUIExampleApp: App { init() { // 可以在这里做初始化的操作 } var body: some Scene { WindowGroup { ContentView() } } } 二、找不到Info.plist文件 问题: 在需要往info.plist文件中添加值时,找不到info.plist文件 解决方案: info.plist 文件改为了xcode的配置,位置向导为;Project → Targets → Info → Custom iOS Target Properties 三、报错信息:Non-constant range: not an integer range 解决方案: 传递一个id参数 原因Apple官方解释: /// Creates an instance that generates Rotor content by combining, in order, /// individual Rotor content for each element in the data given to this /// `ForEach`. /// /// It’s important that the `id` of a data element …