GADMobileAds
类提供了全局设置,用于控制移动广告 SDK 收集的特定信息。
同应用标识键
Google 移动广告 SDK 8.3.0 版引入了 同应用标识键 ,帮助您通过使用从应用收集的数据投放相关性更高的个性化广告。
同应用标识键默认处于启用状态,但您可以通过以下 API 将其停用。
Swift
@import GoogleMobileAds // Disables the same app key. GADMobileAds.sharedInstance().requestConfiguration.setSameAppKeyEnabled(false)
Objective-C
@import GoogleMobileAds; // Disables the same app key. [GADMobileAds.sharedInstance.requestConfiguration setSameAppKeyEnabled:NO];
视频广告音量控制
如果您的应用有自己的音量控制,例如自定义的音乐或音效音量,那么向 Google 移动广告 SDK 报告应用的音量后,视频广告会遵循相关的应用音量设置。这样可确保用户收到符合预期的视频广告音量。
设备音量(通过音量按钮或操作系统级音量滑块控制)决定了设备音频输出的音量。不过,应用可以相对于设备音量单独调整音量,以定制音频体验。
对于开屏广告、横幅广告、插页式广告、插页式激励广告和插页式激励广告,您可以通过设置 applicationVolume
属性,向 Google 移动广告 SDK 报告相对的应用音量。广告音量的有效值范围为 0.0
(静音)至 1.0
(当前设备音量)。以下示例展示了如何向 SDK 报告相对的应用音量:
Swift
func viewDidLoad() { super.viewDidLoad() // Set app volume to be half of the current device volume. GADMobileAds.sharedInstance().applicationVolume = 0.5 ... }
Objective-C
- (void)viewDidLoad { [super viewDidLoad]; // Set app volume to be half of the current device volume. GADMobileAds.sharedInstance.applicationVolume = 0.5; ... }
对于开屏广告、横幅广告、插页式广告、激励广告和插页式激励广告格式,您可以通过设置 applicationMuted
属性来告知 Google 移动广告 SDK:应用已静音:
Swift
GADMobileAds.sharedInstance().applicationMuted = true
Objective-C
GADMobileAds.sharedInstance.applicationMuted = YES;
默认情况下,applicationVolume
设置为 1
(当前设备音量),applicationMuted
设置为 NO
。
原生广告
如需了解如何控制静音设置,请参阅 GADVideoOptions
。目前,原生广告不支持自定义音量控制。
音频会话
通过音频会话,您可以向系统传达您对应用音频行为的意图。如需详细了解音频会话,请参阅 Apple 的音频会话编程指南。您可以通过 audioVideoManager
属性管理 Google 移动广告 SDK 音频。
如果您不在应用中使用音频,则无需使用这些 API。Google 移动广告 SDK 会在播放音频时自动管理音频会话类别。如果您在应用中播放音频,并且想要更严格地控制 Google 移动广告 SDK 播放音频的方式和时间,则可以使用这些 API。
如果您想自己负责管理音频会话的类别,则可以在音视频管理器上将 audioSessionIsApplicationManaged
属性设置为 YES
。
如果您想管理音频会话类别,可以实现 GADAudioVideoManagerDelegate
并在音视频管理器上设置 delegate
属性,以便接收广告视频和音频播放事件的通知。然后,您应该根据 Apple 的音频会话编程指南,将音频会话类别更改为相关类别。
以下是一个简化的代码示例,显示了上述 API 在应用播放音乐时使用的推荐方法:
Swift
func setUp() { GADMobileAds.sharedInstance().audioVideoManager.delegate = self GADMobileAds.sharedInstance().audioVideoManager.audioSessionIsApplicationManaged = false } // MARK: - GADAudioVideoManagerDelegate func audioVideoManagerWillPlayAudio(_ audioVideoManager: GADAudioVideoManager) { // The Mobile Ads SDK is notifying your app that it will play audio. You // could optionally pause music depending on your apps design. MyAppObject.sharedInstance().pauseAllMusic() } func audioVideoManagerDidStopPlayingAudio(_ audioVideoManager: GADAudioVideoManager) { // The Mobile Ads SDK is notifying your app that it has stopped playing // audio. Depending on your design, you could resume music here. MyAppObject.sharedInstance().resumeAllMusic() }
Objective-C
- (void)setUp { GADMobileAds.sharedInstance.audioVideoManager.delegate = self; GADMobileAds.sharedInstance.audioVideoManager.audioSessionIsApplicationManaged = NO; } #pragma mark - GADAudioVideoManagerDelegate - (void)audioVideoManagerWillPlayAudio:(GADAudioVideoManager *)audioVideoManager { // The Mobile Ads SDK is notifying your app that it will play audio. You // could optionally pause music depending on your apps design. [MyAppObject.sharedInstance pauseAllMusic]; } - (void)audioVideoManagerDidStopPlayingAudio:(GADAudioVideoManager *)audioVideoManager { // The Mobile Ads SDK is notifying your app that it has stopped playing // audio. Depending on your design, you could resume music here. [MyAppObject.sharedInstance resumeAllMusic]; }
崩溃报告
Google 移动广告 SDK 会检查 iOS 应用中发生的异常,如果异常是由 SDK 引起的,则会加以记录。这些异常会在未来的 SDK 版本中得到解决。
崩溃报告默认处于启用状态。如果您不想记录与 SDK 相关的异常,则可以通过调用 disableSDKCrashReporting
方法来停用此功能。此方法的最佳调用时机是在应用启动时:
Swift
import GoogleMobileAds
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
GADMobileAds.disableSDKCrashReporting()
return true
}
}
Objective-C
@import GoogleMobileAds;
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[GADMobileAds disableSDKCrashReporting];
return YES;
}
@end
关于是否同意使用 Cookie 的设置
如果您的应用有特殊要求,您可以设置可选的 NSUserDefaults
gad_has_consent_for_cookies
。当 gad_has_consent_for_cookies
偏好设置设为 0 时,移动广告 SDK 会启用受限广告 (LTD)。
Swift
UserDefaults.standard.set(0, forKey: "gad_has_consent_for_cookies")
Objective-C
NSUserDefaults.standardUserDefaults().setObject(Int(0), forKey: "gad_has_consent_for_cookies");