SwiftyAds 使用教程

SwiftyAds 使用教程

swifty-adsA Swift library to display Google AdMob ads. GDPR, COPPA and App Tracking Transparency compliant.项目地址:https://gitcode.com/gh_mirrors/sw/swifty-ads

项目介绍

SwiftyAds 是一个 Swift 库,用于在 iOS 应用中展示 Google AdMob 广告,同时遵守 GDPR、COPPA 和 App Tracking Transparency 规定。该项目旨在简化在应用中集成广告的过程,并确保合规性。

项目快速启动

安装

首先,将 SwiftyAds 添加到你的项目中。你可以通过 CocoaPods 或 Swift Package Manager 进行安装。

使用 CocoaPods

在你的 Podfile 中添加以下内容:

pod 'SwiftyAds'

然后运行 pod install

使用 Swift Package Manager

在你的 Xcode 项目中,选择 File > Swift Packages > Add Package Dependency,然后输入以下 URL:

https://github.com/crashoverride777/swifty-ads.git

配置

在你的应用中配置 SwiftyAds。首先,在 AppDelegate.swift 中导入 SwiftyAds:

import SwiftyAds

然后在 application(_:didFinishLaunchingWithOptions:) 方法中进行配置:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // 配置 SwiftyAds
    SwiftyAds.shared.configure(
        with: self,
        adUnitIdForBanner: "YOUR_BANNER_AD_UNIT_ID",
        adUnitIdForInterstitial: "YOUR_INTERSTITIAL_AD_UNIT_ID",
        adUnitIdForRewarded: "YOUR_REWARDED_AD_UNIT_ID",
        adUnitIdForRewardedInterstitial: "YOUR_REWARDED_INTERSTITIAL_AD_UNIT_ID",
        request: .init(
            nonPersonalizedAdsOnly: false,
            childDirectedTreatment: false
        ),
        consentConfiguration: .default,
        mediationConfig: nil,
        environment: .production
    )
    return true
}

展示广告

在你的视图控制器中展示广告:

import SwiftyAds

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        // 展示横幅广告
        SwiftyAds.shared.showBannerAd(from: self, at: .bottom)
    }
    
    @IBAction func showInterstitialAd(_ sender: Any) {
        // 展示插页广告
        SwiftyAds.shared.showInterstitialAd(from: self)
    }
    
    @IBAction func showRewardedAd(_ sender: Any) {
        // 展示奖励广告
        SwiftyAds.shared.showRewardedAd(from: self) { [weak self] in
            // 奖励回调
            print("用户获得了奖励")
        }
    }
}

应用案例和最佳实践

应用案例

SwiftyAds 可以用于各种类型的应用,包括游戏、工具、社交应用等。例如,在一个游戏中,你可以在关卡之间展示插页广告,或者在用户观看奖励广告后给予额外的游戏货币。

最佳实践

  1. 合规性:确保你的广告展示遵守 GDPR、COPPA 和 App Tracking Transparency 规定。
  2. 用户体验:不要过度展示广告,以免影响用户体验。
  3. 测试:在发布前,确保在不同设备和网络环境下测试广告展示。

典型生态项目

SwiftyAds 可以与其他广告和分析库集成,例如:

  1. Google Analytics:用于跟踪广告效果和用户行为。
  2. Firebase:用于实时数据分析和应用性能监控。
  3. Adjust:用于移动应用的归因和分析。

通过这些集成,你可以更全面地了解你的应用和广告表现,从而做出更好的决策。

swifty-adsA Swift library to display Google AdMob ads. GDPR, COPPA and App Tracking Transparency compliant.项目地址:https://gitcode.com/gh_mirrors/sw/swifty-ads

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

农爱宜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值