สิ่งที่ต้องดำเนินการก่อน
คู่มือการนําไปใช้นี้จะถือว่าคุณคุ้นเคยกับสิ่งต่อไปนี้
- การพัฒนาแอป iOS
- การเชื่อมโยง SDK โฆษณาในอุปกรณ์เคลื่อนที่ของ Google กับโปรเจ็กต์ iOS ดาวน์โหลด SDK เวอร์ชันล่าสุด
ภาพรวม
เอกสารนี้สรุปขั้นตอนการผสานรวมโฆษณา AFS สําหรับแอปบนอุปกรณ์เคลื่อนที่ (AFSMA) ในแอปบนอุปกรณ์เคลื่อนที่ iOS หรือในบางครั้งโฆษณา AFSMA เรียกอีกอย่างว่าโฆษณา Search ความสูงแบบไดนามิก หากต้องการขอและแสดงโฆษณา AFSMA ใน iOS คุณจะต้องใช้สิ่งต่อไปนี้
- คลาสนี้จะรับช่วงมาจากคลาส UIView ของ iOS และแสดงโฆษณา AFSMA
GADSearchBannerView
จะส่งคําขอสําหรับโฆษณาที่มีGADDynamicHeightSearchRequest
และแสดงโฆษณาที่แสดงผล ควรเพิ่มGADSearchBannerView
ในข้อมูลพร็อพเพอร์ตี้ที่มีอยู่ของแอปนี้ โดยปกติจะเป็นตัวควบคุมข้อมูลพร็อพเพอร์ตี้หลักที่จัดเก็บข้อมูลพร็อพเพอร์ตี้ที่GADSearchBannerView
เพิ่มเข้ามา ควรกําหนดผู้รับมอบสิทธิ์ที่เหมาะสมในGADSearchBannerView
- ต้องสร้างอินสแตนซ์
GADSearchBannerView
ด้วยinitWithAdSize:kGADAdSizeFluid
เพื่อขอโฆษณา AFSMA กําลังสร้างอินสแตนซ์GADSearchBannerView
ที่มีinitWithAdSize:kGADAdSizeBanner
ส่งคําขอโฆษณา AFSMA เดิม - ต้องตั้งค่าพร็อพเพอร์ตี้
adUnitID
ในออบเจ็กต์นี้เป็นโค้ดพร็อพเพอร์ตี้
- ออบเจ็กต์นี้สรุปพารามิเตอร์คําขอโฆษณา ซึ่งคล้ายกับการตั้งค่าพารามิเตอร์ในออบเจ็กต์คําขอโฆษณา 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"]);
}
หากคําขอโฆษณาไม่สําเร็จ คุณสามารถใช้การเรียกกลับเพื่อจัดการกับข้อผิดพลาดอย่างเหมาะสม และตรวจสอบข้อผิดพลาดผ่านออบเจ็กต์ข้อผิดพลาด