全局设置

The GADMobileAds class provides global settings for controlling certain information collected by the Mobile Ads SDK.

Video ad volume control

If your app has its own volume controls, such as custom music or sound effect volumes, disclosing app volume to the Google Mobile Ads SDK enables video ads to respect app volume settings. This ensures users receive video ads with the expected audio volume.

The device volume, controlled through volume buttons or OS-level volume slider, determines the volume for device audio output. However, apps can independently adjust volume levels relative to the device volume to tailor the audio experience.

For App Open, Banner, Interstitial, Rewarded, and Rewarded Interstitial ad formats you can report the relative app volume to the Google Mobile Ads SDK by setting the applicationVolume property. Valid ad volume values range from 0.0 (silent) to 1.0 (current device volume). Here's an example of how to report the relative app volume to the 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;
  ...
}

For App Open, Banner, Interstitial, Rewarded, and Rewarded Interstitial ad formats, you can inform the Google Mobile Ads SDK that the app volume has been muted by setting the applicationMuted property:

Swift

GADMobileAds.sharedInstance().applicationMuted = true

Objective-C

GADMobileAds.sharedInstance.applicationMuted = YES;

By default, applicationVolume is set to 1 (the current device volume) and applicationMuted is set to NO.

Native ads

See GADVideoOptions for instructions on controlling the mute settings. For native ads, there isn't support for custom volume control.

Audio sessions

Audio sessions let you express to the system your intentions for your app's audio behavior. Additional information on audio sessions can be found in Apple's Audio Session Programming Guide. The available options for managing the Google Mobile Ads SDK audio is through the audioVideoManager property.

If you don't use audio in your app, you don't need to use these APIs. The Google Mobile Ads SDK automatically manages the audio session category when it plays audio. If you do play audio in your app and you want tighter control of how and when Google Mobile Ads SDK plays audio, you can make use of these APIs.

On the audio video manager, you can set the audioSessionIsApplicationManaged property to YES if you want to take responsibility for managing the audio session category yourself.

If you want to manage the audio session category, you can implement GADAudioVideoManagerDelegate and set the delegate property on the audio video manager to be notified of ads video and audio playback events. You should then change the audio session category to the relevant category according to Apple's Audio Session Programming Guide.

Here is a simplified code sample that shows the recommended approach if your app plays music, using the above APIs:

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];
}

Crash reporting

The Google Mobile Ads SDK inspects exceptions that occur in an iOS app and records them if they were caused by the SDK. These exceptions are then addressed in future SDK versions.

Crash reporting is enabled by default. If you don't want SDK-related exceptions to be recorded, you can disable this feature by calling the disableSDKCrashReporting method. The best time to call this method is when the app launches:

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

If your app has special requirements, you can set the optional NSUserDefaults gad_has_consent_for_cookies. The Google Mobile Ads SDK enables limited ads (LTD) if the gad_has_consent_for_cookies preference is set to zero.

Swift

UserDefaults.standard.set(0, forKey: "gad_has_consent_for_cookies")

Objective-C

NSUserDefaults.standardUserDefaults().setObject(Int(0),
    forKey: "gad_has_consent_for_cookies");