Triển khai gốc AFS dành cho iOS

SDK quảng cáo trên thiết bị di động của Google cũng hỗ trợ các kiểu quảng cáo đi kèm kết quả tìm kiếm tùy chỉnh. Nếu ứng dụng của bạn đã sử dụng SDK quảng cáo trên thiết bị di động của Google, chúng tôi khuyên bạn nên sử dụng Phiên bản AFSMA SDK thay thế.

Điều kiện tiên quyết

  • Sử dụng Xcode 8.0 trở lên
  • Nhắm mục tiêu phiên bản iOS 8.0 trở lên
  • Mã thuộc tính web của nhà xuất bản hợp lệ (ví dụ: vert-ppa-test1-srp)
  • CocoaPods

Nhập SDK gốc AFS

CocoaPods

Nhập SDK Google-AFSNative vào một dự án iOS bằng cách sử dụng CocoaPods Mở tệp Podfile và thêm dòng này vào mục tiêu của ứng dụng:

pod 'Google-AFSNative'

Sau đó, từ dòng lệnh, hãy chạy:

pod install --repo-update

Nếu bạn mới sử dụng CocoaPods, hãy xem tài liệu chính thức để biết thông tin về cách tạo và sử dụng Podfile.

Tổng quan

Nếu bạn đang nâng cấp lên phiên bản 4.0 hoặc cao hơn từ 2.0.8 trở xuống, vui lòng xem hướng dẫn di chuyển của chúng tôi.

Tài liệu này trình bày quy trình tích hợp Quảng cáo gốc AFS trong ứng dụng di động iOS của bạn.

GANSearchAdController

  • Hàm khởi tạo GANSearchAdController cần được cung cấp mã thuộc tính web của nhà xuất bản, mã chế độ cài đặt mong muốn và mã Đối tượng GANSearchAdControllerOptions.
  • Mỗi lệnh gọi đến loadAds() cho biết một lượt tìm kiếm mới và lệnh gọi này sẽ dẫn đến lệnh gọi hiện tại nhóm quảng cáo bị loại bỏ và không hợp lệ.
  • Mẫu quảng cáo được lưu trữ trong GANAdView.
  • Quảng cáo được chèn vào quảng cáo GANAdView bằng phương thức populateAdView. Trong ngoài GANAdView được điền sẵn, phương thức gọi cung cấp một adIdentifier – là một chuỗi tuỳ ý sẽ dùng để nhận dạng duy nhất quảng cáo. Bên trong API, một quảng cáo cụ thể được chỉ định cho mỗi adIdentifier được truyền trong năm Sau đó, bất cứ khi nào adKey đó được chuyển lại trong tương lai, quảng cáo đó sẽ bị trả lại. Ví dụ: nếu populateAdView được gọi lần đầu tiên bằng adIdentifier "keyA", mỗi lệnh gọi tiếp theo đến populateAdView trong khi truyền "keyA" vì adIdentifier sẽ dẫn đến việc hiển thị cùng một quảng cáo.

GANAdView

  • Đây là UIView chứa mẫu quảng cáo đó.
  • Điền một quảng cáo vào chế độ xem này bằng cách sử dụng phương thức populateAdView trên GANSearchAdController

GANSearchAdControllerOptions

  • Truyền đối tượng này đến hàm khởi tạo GANSearchAdController để chỉ định về cách quảng cáo được yêu cầu và hiển thị.

GANSearchAdRequest

  • Gọi phương thức loadAds trên thực thể GANSearchAdController bằng đoạn mã này để tạo yêu cầu quảng cáo.

GANSearchAdControllerDelegate

  • Triển khai giao diện này và cung cấp cho GANSearchAdController để đăng ký lệnh gọi lại cho một số trạng thái.

Cách triển khai mẫu

Ví dụ bên dưới minh hoạ việc tạo GANSearchAdControllerGANView để hiển thị quảng cáo trong ViewController mẫu.

// SampleAppViewController.m implementation

#import <AFSNative/AFSNative.h>

@interface GBannerViewController () {
  // The Ad Controller used by the sample application.
  GANSearchAdController *_adController;

  // The Ad View to display the loaded ad.
  GANAdView *_adView;
}
// scrollView will be where we place our ads in this example.
@property(nonatomic, strong) UIScrollView *scrollView;
@end
...

- (void)viewDidLoad {
  [super viewDidLoad];

  // Create the scroll view.
  ...
  [self.view addSubview:scrollView];

  // Create a test button and link the ad request to its action.
  UIButton *loadBannerButton = [UIButton buttonWithType:UIButtonTypeCustom];
  ...
  [loadBannerButton addTarget:self
                       action:@selector(loadAd:)
             forControlEvents:UIControlEventTouchUpInside];
  [self.scrollView addSubview:loadBannerButton];

  // Construct the Ad Controller.
  GANSearchAdControllerOptions *options = [[GANSearchAdControllerOptions alloc] init];
  options.prefetchEnabled = YES;
  options.adType = GANSearchAdTypeSPA;
  options.adFetchCount = 3;

  _adController = [[GANSearchAdController alloc]
                     initWithPublisherID: @"your-client-id"
                                 styleID: @"your-settings-id"
                                 options: options
                                delegate: self];

  _adView = [_adController adView];
  [self.scrollView addSubview:_adView];
}

// Request ads when the test button is pressed.
- (void)loadAd:(id)sender {
  // Construct the Ad Request.
  GANSearchAdRequest *adRequest = [[GANSearchAdRequest alloc] init];
  adRequest.query =  @"some-query";
  // Start loading ads. Note that the loading is asynchronous.
  [_adController loadAds: adRequest];
}

// Insert ads into GANAdView if the request returns successfully.
- (void)searchAdController:(GANSearchAdController *)adController  
                didLoadAds:(NSInteger)numberOfAds {
  if (numberOfAds <= 0) {
    NSLog(@"No ads found on the server");
  } else {
    [_adController populateAdView:_adView identifier:@"demoAd"];
  }
}
...