通用設定

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.
  MobileAds.shared.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

MobileAds.shared.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() {
  MobileAds.shared.audioVideoManager.delegate = self
  MobileAds.shared.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.shared.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.shared.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 {

    MobileAds.shared.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");