通用設定

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

影片廣告音量控制項

如果應用程式有專屬的音量控制項 (例如自訂音樂或音效音量),只要向 Google Mobile Ads SDK 揭露應用程式音量,影片廣告就能按照應用程式音量設定運作。如此可確保使用者能收到預期音訊音量的影片廣告。

裝置音量 (透過音量按鈕或 OS 等級的音量滑桿控制) 會決定裝置音訊輸出的音量。不過,應用程式可以根據裝置音量,單獨調整音量大小,量身打造音訊體驗。

對於應用程式開啟頁面、橫幅、插頁式、獎勵和插頁式獎勵廣告格式,您可以設定 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 的音訊工作階段程式設計指南。管理 Google Mobile Ads SDK 音訊的方法可以使用 audioVideoManager 屬性。

如果您不會在應用程式中使用音訊,則不需要使用這些 API。Google Mobile Ads SDK 在播放音訊時會自動管理音訊工作階段類別。如果您在應用程式中播放音訊,並想更進一步控制 Google Mobile Ads 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 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 的偏好設定設為零,就會啟用 受限制的廣告 (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");