การติดตั้งใช้งาน AFS สําหรับแอปบนอุปกรณ์เคลื่อนที่ (AFSMA) สําหรับ iOS

ข้อกำหนดเบื้องต้น

คู่มือการติดตั้งใช้งานนี้จะถือว่าคุณคุ้นเคยกับสิ่งต่อไปนี้ดี

ภาพรวม

เอกสารนี้ระบุกระบวนการผสานรวมโฆษณา AFS สำหรับแอปบนอุปกรณ์เคลื่อนที่ (AFSMA) ในแอปบนอุปกรณ์เคลื่อนที่ iOS ในบางครั้ง โฆษณา AFSMA อาจเรียกว่าโฆษณา Search ที่มีความสูงแบบไดนามิกเช่นกัน หากต้องการขอและแสดงผลโฆษณา AFSMA บน iOS คุณจะต้องใช้รายการต่อไปนี้

GADSearchBannerView

  • คลาสนี้รับค่ามาจากคลาส UIView ของ iOS และแสดงโฆษณา AFSMA GADSearchBannerView ส่งคำขอโฆษณาที่มี GADDynamicHeightSearchRequest และแสดงโฆษณาที่ส่งกลับมา ควรเพิ่ม GADSearchBannerView ลงในข้อมูลพร็อพเพอร์ตี้ใดก็ได้ที่มีอยู่ของแอป โดยทั่วไปแล้วจะเป็นตัวควบคุมมุมมองระดับบนสุดที่เก็บการแสดงผลที่มีการเพิ่ม GADSearchBannerView ไว้ ควรตั้งค่าผู้รับมอบสิทธิ์ที่เหมาะสมใน GADSearchBannerView
  • ต้องสร้างอินสแตนซ์ GADSearchBannerView ด้วย initWithAdSize:kGADAdSizeFluid เพื่อขอโฆษณา AFSMA การสร้างอินสแตนซ์ GADSearchBannerView ด้วย initWithAdSize:kGADAdSizeBanner จะส่งคำขอโฆษณา AFSMA เดิม
  • ต้องตั้งค่าพร็อพเพอร์ตี้ adUnitID ในออบเจ็กต์นี้เป็นรหัสพร็อพเพอร์ตี้

GADDynamicHeightSearchRequest

  • ออบเจ็กต์นี้สรุปพารามิเตอร์คำขอโฆษณา ซึ่งเทียบได้กับการตั้งค่าพารามิเตอร์ในออบเจ็กต์คำขอโฆษณา JavaScript (ตัวเลือกหน้าเว็บ ตัวเลือกหน่วย) สำหรับ AFS บนเดสก์ท็อปและเว็บบนอุปกรณ์เคลื่อนที่

(void)adView:(GADBannerView *)bannerView willChangeAdSizeTo:(GADAdSize)size

  • ระบบจะเรียกโค้ดเรียกกลับนี้เมื่อคำขอโฆษณากลับมา เนื่องจากหน่วยโฆษณาที่ส่งกลับมาอาจมีโฆษณาจำนวนหนึ่งที่มีส่วนขยายต่างกัน ระบบไม่ทราบขนาดที่แน่นอนของหน่วยโฆษณาเมื่อมีการสร้างคำขอโฆษณา เมื่อส่งโฆษณากลับมาแล้ว ต้องอัปเดตมุมมองแบนเนอร์เพื่อให้รองรับขนาดใหม่ของหน่วยโฆษณา ควรติดตั้งโค้ดสำหรับปรับขนาด GADSearchBannerView ในมุมมองระดับบนสุดที่นี่

ตัวอย่างการใช้งาน

ตัวอย่างด้านล่างสาธิตการใช้ GBannerViewController เพื่อสร้าง GADSearchBannerView เป็นมุมมองย่อยของ UIScrollView หากต้องการขอโฆษณา AFSMA ได้อย่างถูกต้อง ต้องสร้างอินสแตนซ์ GADSearchBannerView ด้วย initWithAdSize:kGADAdSizeFluid

// GBannerViewController.m implementation

@interface GBannerViewController () <GADAdSizeDelegate,
                                     GADBannerViewDelegate>

@property(nonatomic, strong) GADSearchBannerView *searchBannerView;

@property(nonatomic, strong) UIScrollView *scrollView;

@end

@implementation GBannerViewController

- (void)viewDidLoad {
  [super viewDidLoad];

  // Create the scroll view.
  ....
  ....

  // Create the banner.
  self.searchBannerView = [[GADSearchBannerView alloc] initWithAdSize:kGADAdSizeFluid];

  // Replace with your pub ID (e.g. ms-app-pub-9616389000213823).
  self.searchBannerView.adUnitID = @"ms-app-pub-################";

  // Set the initial location and size of the banner. The initial height
  // is set to 0 since we might not get an ad back.
  self.searchBannerView.frame = CGRectMake(0,
                                           0,
                                           CGRectGetWidth(self.view.bounds),
                                           0);
  self.searchBannerView.autoresizingMask = UIViewAutoresizingFlexibleWidth;

  // Set the delegate properties.
  self.searchBannerView.adSizeDelegate = self;
  self.searchBannerView.delegate = self;

  // Add the new search banner into the parent scrollView.
  [self.scrollView addSubview:self.searchBannerView];
  }

ภายใน GBannerViewController เดียวกัน ให้สร้าง GADDynamicHeightSearchRequest ที่กำหนดพารามิเตอร์ของโฆษณาที่จะแสดงใน GADSearchView

// Create a search request and load the banner.
GADDynamicHeightSearchRequest *searchRequest = [[GADDynamicHeightSearchRequest alloc] init];

// Ad request options (set using GADDynamicHeightSearchRequest properties).
searchRequest.query = @"flowers";
searchRequest.numberOfAds = 2;

// Replace with the ID of a style from your custom search styles
[searchRequest setAdvancedOptionValue:@"0000000001"
                               forKey:@"styleId"];

ตัวเลือกการปรับแต่งอื่นๆ จะทำได้โดยตั้งค่าพร็อพเพอร์ตี้เพิ่มเติมในออบเจ็กต์ GADDynamicHeightSearchRequest

หากต้องการสร้างคำขอโฆษณา ให้เรียก loadRequest ด้วยออบเจ็กต์ GADDynamicHeightSearchRequest จากออบเจ็กต์ GADSearchBannerView ดังนี้

[self.searchBannerView loadRequest:searchRequest];

คุณต้องใช้โค้ดเรียกกลับต่อไปนี้เพื่อให้มุมมองระดับบนสุดรองรับ GADSearchBannerView ได้อย่างเหมาะสมเมื่อแสดงโฆษณา

// Callback to update the parent view height.
- (void)adView:(GADBannerView *)bannerView willChangeAdSizeTo:(GADAdSize)size {
  // Update the banner view based on the ad size.
  CGRect newFrame = self.searchBannerView.frame;
  newFrame.size.height = size.size.height;
  self.searchBannerView.frame = newFrame;

  // Perform any additional logic needed due to banner view size change.
  ...
}

ตัวเลือกขั้นสูง

พารามิเตอร์คำขอโฆษณาส่วนใหญ่ตั้งค่าผ่านพร็อพเพอร์ตี้ในออบเจ็กต์ GADDynamicHeightSearchRequest (searchRequest ด้านบน) ได้ คุณจะต้องตั้งค่าพารามิเตอร์อื่นๆ โดยใช้คู่คีย์-ค่าด้วยเมธอด setAdvancedOptionValue ดังนี้

// Advanced customization options (set using key-value pair).

// Set a parameter (parameter_name) and its value (parameter_value).
[searchRequest setAdvancedOptionValue:@"parameter_value"
                               forKey:@"parameter_name"];

// Example: Show visible URL below description (domainLinkAboveDescription: false).
[searchRequest setAdvancedOptionValue:@"false"
                               forKey:@"domainLinkAboveDescription"];

ดูรายการพารามิเตอร์ทั้งหมดที่ใช้ได้

การตรวจสอบข้อผิดพลาด

GADBannerViewDelegate มีโค้ดเรียกกลับเพื่อช่วยคุณตรวจสอบข้อผิดพลาด

- (void)adView:(GADBannerView *)bannerView didFailToReceiveAdWithError:(GADRequestError *)error {

  // This callback is triggered when the ad request fails.
  // Add code here to debug the error object to discover causes of failure
  NSLog(@"Ad call failed due to %@", error.userInfo[@"NSUnderlyingError"]);
}

หากคำขอโฆษณาล้มเหลว คุณสามารถใช้โค้ดเรียกกลับนี้เพื่อจัดการข้อผิดพลาดได้อย่างเหมาะสมและตรวจสอบข้อผิดพลาดผ่านออบเจ็กต์ข้อผิดพลาด