通用設定

GADMobileAds 類別提供全域設定,可用於控制 Mobile Ads SDK 收集的特定資訊。

影片廣告音量控制

如果您的應用程式有專屬的音量控制項 (例如自訂音樂或音效音量),向 Google Mobile Ads SDK 公開應用程式音量,即可讓影片廣告採用應用程式音量設定。這可確保使用者接收到影片廣告時,廣告的音量符合預期。

裝置音量可透過音量鍵或作業系統層級的音量滑桿控制,用於決定裝置音訊輸出的音量。不過,應用程式可以獨立調整相對於裝置音量的音量,以提供客製化的音訊體驗。

針對應用程式開啟、橫幅、插頁式、獎勵和獎勵插頁式廣告格式,您可以設定 applicationVolume 屬性,向 Google Mobile Ads 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 Mobile Ads SDK 應用程式音量已調低:

Swift

GADMobileAds.sharedInstance().applicationMuted = true

Objective-C

GADMobileAds.sharedInstance.applicationMuted = YES;

根據預設,applicationVolume 會設為 1 (目前裝置音量),applicationMuted 則設為 NO

原生廣告

如要瞭解如何控制靜音設定,請參閱 GADVideoOptions。原生廣告不支援自訂音量控制。

音訊工作階段

音訊工作階段可讓您向系統表達應用程式音訊行為的意圖。如要進一步瞭解音訊工作階段,請參閱 Apple 的音訊工作階段編程指南。您可以透過 audioVideoManager 屬性,使用可用的選項來管理 Google Mobile Ads SDK 音訊。

如果應用程式不使用音訊,就不需要使用這些 API。Google Mobile Ads SDK 會在播放音訊時自動管理音訊工作階段類別。如果您在應用程式中播放音訊,且想要更嚴密控管 Google Mobile Ads SDK 播放音訊的方式和時機,可以使用這些 API。

如果您想自行管理音訊工作階段類別,可以在音訊/視訊管理工具中將 audioSessionIsApplicationManaged 屬性設為 YES

如果您想管理音訊工作階段類別,可以實作 GADAudioVideoManagerDelegate,並在音訊/視訊管理工具上設定 delegate 屬性,以便接收廣告影片和音訊播放事件的通知。接著,請根據 Apple 的 Audio Session Programming Guide 將音訊工作階段類別變更為相關類別。

以下是簡化的程式碼範例,說明在應用程式播放音樂時,使用上述 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 Mobile Ads 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

如果應用程式有特殊需求,您可以設定選用的 NSUserDefaults gad_has_consent_for_cookies。如果 gad_has_consent_for_cookies 偏好設定為零,Google Mobile Ads 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");