Это мой первый раз, когда я работаю с нативной рекламой Google AdMob, я думаю, что я следовал инструкции по внедрению. Все, что осталось, - это отображение рекламы в представлении коллекции, и вот где я застрял. Я не знаю, как правильно показывать объявления между пользователями, загруженными постом. В основном мне нужна помощь в добавлении xib в представление коллекции. задача: объявления должны быть заполнены при прокрутке сообщений ..
Я использую
Коллекция Просмотр Xib Google AdMob Native advanced
Я также не получаю никаких ошибок или сбоев, и консоль печатает, так что я, очевидно, что-то делаю не так .. Получено нативное объявление:
Консоль также - print
(«Реклама не показывается») и print
(«Не то, что я хочу»)
Вот мой код
import UIKit
import Firebase
class FollowingFeedViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UISearchBarDelegate, FeedCellDelegate, PeopleToFollowDelegate, GADUnifiedNativeAdLoaderDelegate {
// MARK: - Google ADMob
/// The ad unit ID from the AdMob UI.
let adUnitID = "ca-app-pub-3940256099942544/3986624511"
/// The number of native ads to load (between 1 and 5 for this example).
let numAdsToLoad = 5
/// The native ads.
var nativeAds = [GADUnifiedNativeAd]()
/// The ad loader that loads the native ads.
var adLoader: GADAdLoader!
func adLoaderDidFinishLoading(_ adLoader: GADAdLoader) {
addNativeAds()
}
func adLoader(_ adLoader: GADAdLoader, didReceive nativeAd: GADUnifiedNativeAd) {
print("Received native ad: \(nativeAd)")
// Add the native ad to the list of native ads.
nativeAds.append(nativeAd)
}
func adLoader(_ adLoader: GADAdLoader, didFailToReceiveAdWithError error: GADRequestError) {
print("\(adLoader) failed with error: \(error.localizedDescription)")
}
/// Add native ads to the list.
func addNativeAds() {
if nativeAds.count <= 0 {
print("Ads not dispalying ")
return
}
let adInterval = (posts.count / nativeAds.count) + 1
var index = 0
for nativeAd in nativeAds {
if index < collectionObject.count {
collectionObject.insert(nativeAd, at: index)
index += adInterval
} else {
print("Not what I want")
break
}
}
}
// MARK: - Properties
var posts = [Post]()
var collectionObject = [AnyObject]()
var viewSinglePost = false
var post: Post?
var currentKey: String?
var userProfileController: ProfileViewController?
var header: FeedReusableView?
@IBOutlet weak var collectionView: UICollectionView!
// MARK: - UICollectionViewDataSource
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
if posts.count > 4 {
if indexPath.item == posts.count - 1 {
fetchPosts()
}
}
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return posts.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PostsCell", for: indexPath) as? FollowingCell {
cell.delegate = self
cell.post = posts[indexPath.item] as Post
handleUsernameLabelTapped(forCell: cell)
handleMentionTapped(forCell: cell)
handleHashtagTapped(forCell: cell)
return cell
} else {
let nativeAd = collectionObject[indexPath.row] as! GADUnifiedNativeAd
nativeAd.rootViewController = self
let nativeAdCell = collectionView.dequeueReusableCell(withReuseIdentifier: "UnifiedNativeAdCell", for: indexPath)
// Get the ad view from the Cell. The view hierarchy for this cell is defined in
let adView : GADUnifiedNativeAdView = nativeAdCell.contentView.subviews
.first as! GADUnifiedNativeAdView
// Associate the ad view with the ad object.
// This is required to make the ad clickable.
adView.nativeAd = nativeAd
adView.mediaView?.mediaContent = nativeAd.mediaContent
// Populate the ad view with the ad assets.
(adView.headlineView as! UILabel).text = nativeAd.headline
(adView.advertiserView as! UILabel).text = nativeAd.advertiser
(adView.bodyView as! UILabel).text = nativeAd.body
adView.bodyView?.isHidden = nativeAd.body == nil
(adView.iconView as? UIImageView)?.image = nativeAd.icon?.image
adView.iconView?.isHidden = nativeAd.icon == nil
// In order for the SDK to process touch events properly, user interaction
// should be disabled.
adView.callToActionView?.isUserInteractionEnabled = false
return nativeAdCell
}
}
// MARK: - ViewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
// // Google Admob
let options = GADMultipleAdsAdLoaderOptions()
options.numberOfAds = numAdsToLoad
// Prepare the ad loader and start loading ads.
adLoader = GADAdLoader(adUnitID: adUnitID,
rootViewController: self,
adTypes: [.unifiedNative],
options: [options])
collectionView.dataSource = self
collectionView.delegate = self
adLoader.delegate = self
adLoader.load(GADRequest())
self.collectionView.register(UINib(nibName: "NativeAdCell", bundle: nil), forCellWithReuseIdentifier: "UnifiedNativeAdCell")
addNativeAds()
}
@objc func handleRefresh() {
posts.removeAll(keepingCapacity: false)
self.currentKey = nil
fetchPosts()
collectionView?.reloadData()
header?.profilesCollectionView.reloadData()
}
}
Получить сообщение
func fetchPosts() {
guard let currentUid = Auth.auth().currentUser?.uid else { return }
if currentKey == nil {
USER_FEED_REF.child(currentUid).queryLimited(toLast: 5).observeSingleEvent(of: .value, with: { (snapshot) in
self.collectionView?.refreshControl?.endRefreshing()
guard let first = snapshot.children.allObjects.first as? DataSnapshot else { return }
guard let allObjects = snapshot.children.allObjects as? [DataSnapshot] else { return }
allObjects.forEach({ (snapshot) in
let postId = snapshot.key
self.fetchPost(withPostId: postId)
})
self.currentKey = first.key
})
} else {
USER_FEED_REF.child(currentUid).queryOrderedByKey().queryEnding(atValue: self.currentKey).queryLimited(toLast: 6).observeSingleEvent(of: .value, with: { (snapshot) in
guard let first = snapshot.children.allObjects.first as? DataSnapshot else { return }
guard let allObjects = snapshot.children.allObjects as? [DataSnapshot] else { return }
allObjects.forEach({ (snapshot) in
let postId = snapshot.key
if postId != self.currentKey {
self.fetchPost(withPostId: postId)
}
})
self.currentKey = first.key
})
}
}
func fetchPost(withPostId postId: String) {
Database.fetchPost(with: postId) { (post) in
self.posts.append(post)
self.posts.sort(by: { (post1, post2) -> Bool in
return post1.creationDate > post2.creationDate
})
self.collectionView?.reloadData()
}
}
}