{"id":1550,"date":"2018-01-13T11:17:08","date_gmt":"2018-01-13T02:17:08","guid":{"rendered":"http:\/\/blue-bear.jp\/kb\/?p=1550"},"modified":"2018-01-13T11:17:08","modified_gmt":"2018-01-13T02:17:08","slug":"swift4-admob%e3%81%ae%e3%83%90%e3%83%8a%e3%83%bc%e3%82%92tableview%e3%81%ae%e6%8c%87%e5%ae%9a%e8%a1%8c%e3%81%ab%e8%bf%bd%e5%8a%a0%e3%81%99%e3%82%8b","status":"publish","type":"post","link":"https:\/\/blue-bear.jp\/kb\/swift4-admob%e3%81%ae%e3%83%90%e3%83%8a%e3%83%bc%e3%82%92tableview%e3%81%ae%e6%8c%87%e5%ae%9a%e8%a1%8c%e3%81%ab%e8%bf%bd%e5%8a%a0%e3%81%99%e3%82%8b\/","title":{"rendered":"[swift4] Admob\u306e\u30d0\u30ca\u30fc\u3092TableView\u306e\u6307\u5b9a\u884c\u306b\u8ffd\u52a0\u3059\u308b"},"content":{"rendered":"<h2>Admob\u306e\u30e2\u30b8\u30e5\u30fc\u30eb\u3092Pod\u3067\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/h2>\n<p>pod\u30d5\u30a1\u30a4\u30eb\u306b\u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u3092\u8ffd\u52a0\u3057\u3001&#8221;pod install&#8221;<\/p>\n<pre class=\"lang:default decode:true\">pod 'Google-Mobile-Ads-SDK'<\/pre>\n<h2><\/h2>\n<h2>StoryBoard\u90e8\u5206<\/h2>\n<p>ViewContoller\u306bTableView\u3092\u914d\u7f6e\u3057\u3001TableView\u306bCell\u3092\u901a\u5e38\u7528\u30bb\u30eb\u3068\u5e83\u544a\u7528\u30bb\u30eb\u3092\u8ffd\u52a0\u3002<\/p>\n<p>\u901a\u5e38Cell\u306f\u300ccell\u300d\u3001\u5e83\u544a\u7528Cell\u306f\u300cADcell\u300d\u3068\u3044\u3046ID\u3092\u4ed8\u4e0e\u3057\u3066\u304a\u304f\u3002<\/p>\n<p>ADcell\u4e0a\u306b\u306f\u5e83\u544a\u30b5\u30a4\u30ba\u306b\u5408\u308f\u305b\u305f\u901a\u5e38View\u3092\u914d\u7f6e\u3057\u3001tag20\u3092\u8a2d\u5b9a\u3002<\/p>\n<p>\uff0a\u3082\u3063\u3068\u826f\u3044\u65b9\u6cd5\u304c\u3042\u308b\u306f\u305a\u3060\u304c\u3002\u3002\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1551\" src=\"http:\/\/blue-bear.jp\/kb\/wp-content\/uploads\/2018\/01\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2018-01-13-11.15.09.png\" alt=\"\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8 2018-01-13 11.15.09\" width=\"496\" height=\"698\" srcset=\"https:\/\/blue-bear.jp\/kb\/wp-content\/uploads\/2018\/01\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2018-01-13-11.15.09.png 496w, https:\/\/blue-bear.jp\/kb\/wp-content\/uploads\/2018\/01\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2018-01-13-11.15.09-213x300.png 213w\" sizes=\"(max-width: 496px) 100vw, 496px\" \/><\/p>\n<p>&nbsp;<\/p>\n<h2>ViewController.swift<\/h2>\n<pre class=\"lang:default decode:true \">\r\nimport UIKit\r\nimport CoreData\r\nimport GoogleMobileAds\r\n\r\nclass FViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {\r\n \r\n @IBOutlet weak var tableView: UITableView!\r\n \r\n \/\/Admob\r\n var bannerView: GADBannerView!\r\n let adUnitID = \"UnitID\u3092\u5165\u529b\"\r\n let AdInsertPositionArray = [3,15,25,35,45] \/\/3\u884c\u76ee\u300115\u884c\u76ee\u300125\u884c\u76ee\u30fb\u30fb\u306bGADBannerView\u3092\u633f\u5165\r\n \r\n let cellHeight = 90\r\n let cellHeightForAD = 250\r\n \r\n \/\/\u8981\u7d20\r\n var posts:[Dictionary&lt;String, String&gt;]! \/\/\u672c\u756a\u30c7\u30fc\u30bf\r\n var TempPosts:[Dictionary&lt;String, String&gt;]! \/\/\u672c\u756a\u30c7\u30fc\u30bf\u306bappend\u3059\u308b\u524d\u306e\u30c7\u30fc\u30bf\r\n\r\n \/\/MARK: didload\r\n override func viewDidLoad() {\r\n super.viewDidLoad()\r\n \r\n tableView.dataSource = self\r\n tableView.delegate = self\r\n\r\n \/\/\u8981\u7d20(Posts)\u3092\u8aad\u307f\u8fbc\u3080\u30b3\u30fc\u30c9\u3092\u4f55\u3089\u304b\u5b9f\u88c5\r\n\r\n TempPosts = [] \/\/\u521d\u671f\u5316\r\n\r\n insertAD() \r\n\r\n loadAd()\r\n \r\n }\r\n \r\n \r\n \/\/MARK: TableView Method\r\n func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {\r\n  return posts.count\r\n }\r\n \r\n \r\n func numberOfSections(in tableView: UITableView) -&gt; Int { \/\/ section\u306e\u6570\u3092\u6c7a\u3081\u308b\r\n return 1\r\n }\r\n \r\n func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -&gt; CGFloat {\r\n \r\n if posts[indexPath.row][\"id\"] == \"ad\" {\r\n  return CGFloat(cellHeightForAD) \/\/\u5e83\u544a\u7528\u306e\u30bb\u30eb\u306e\u9ad8\u3055\r\n }\r\n \r\n  return CGFloat(cellHeight)\r\n }\r\n \r\n \/\/MARK: cellSetting\r\n func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {\r\n \r\n let row = indexPath.row\r\n \r\n \/\/GoogleAd\r\n if posts[row][\"id\"] == \"ad\" {\r\n  let cell = tableView.dequeueReusableCell(withIdentifier: \"ADcell\", for: indexPath)\r\n  let baseView = cell.contentView.viewWithTag(20) as! UIView\r\n  baseView.addSubview(bannerView) \/\/baseView\u306b\u5e83\u544a\u3092\u8ffd\u52a0\r\n \r\n  return cell\r\n }\r\n \r\n \/\/\u901a\u5e38\u306e\u30bb\u30eb\r\n let cell = tableView.dequeueReusableCell(withIdentifier: \"cell\", for: indexPath)\r\n \r\n return cell\r\n }\r\n \r\n return cell\r\n \r\n }\r\n \r\n \r\n \/\/MARK: Selected Cell\r\n func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {\r\n  \r\n let cell = tableView.cellForRow(at: indexPath)\r\n if cell?.reuseIdentifier != \"cell\" { \/\/\u901a\u5e38\u30bb\u30eb\u304b\u3069\u3046\u304b\u5224\u5b9a\r\n  return\r\n }\r\n  \/\/\u901a\u5e38\u306e\u30bb\u30eb\u304c\u30bf\u30c3\u30d7\u3055\u308c\u305f\u6642\u306e\u51e6\u7406\r\n }\r\n\r\n func insertAD(){\r\n for pos in AdInsertPositionArray {\r\n  if TempPosts.count&gt;pos {\r\n   TempPosts.insert([\"id\":\"ad\"], at: pos) \/\/\u5e83\u544a\u30d1\u30fc\u30c8\u3092\u8981\u7d20\u306b\u633f\u5165\r\n  }\r\n }\r\n \r\n \r\n}<\/pre>\n<p>&nbsp;<\/p>\n<h2>GADBannerViewDelegate\u90e8\u5206<\/h2>\n<pre class=\"lang:default decode:true\">import Foundation\r\nimport GoogleMobileAds\r\n\r\nextension ViewController:GADBannerViewDelegate{\r\n \r\n func loadAd(){\r\n \r\n bannerView = GADBannerView(adSize: kGADAdSizeMediumRectangle) \/\/320x250\r\n \/\/bannerView = GADBannerView(adSize: kGADAdSizeBanner) \/\/320x50\r\n bannerView.adUnitID = adUnitID\r\n bannerView.rootViewController = self\r\n bannerView.load(GADRequest())\r\n bannerView.delegate = self\r\n }\r\n \r\n \/\/\/ Tells the delegate an ad request loaded an ad.\r\n func adViewDidReceiveAd(_ bannerView: GADBannerView) {\r\n print(\"adViewDidReceiveAd\")\r\n \/\/bannerBaseView.addSubview(bannerView)\r\n \/\/bannerBaseView.isHidden = false\r\n }\r\n \r\n \/\/\/ Tells the delegate an ad request failed.\r\n func adView(_ bannerView: GADBannerView,\r\n didFailToReceiveAdWithError error: GADRequestError) {\r\n print(\"adView:didFailToReceiveAdWithError: \\(error.localizedDescription)\")\r\n \/\/bannerBaseView.isHidden = true\r\n }\r\n \r\n \/\/\/ Tells the delegate that a full-screen view will be presented in response\r\n \/\/\/ to the user clicking on an ad.\r\n func adViewWillPresentScreen(_ bannerView: GADBannerView) {\r\n print(\"adViewWillPresentScreen\")\r\n }\r\n \r\n \/\/\/ Tells the delegate that the full-screen view will be dismissed.\r\n func adViewWillDismissScreen(_ bannerView: GADBannerView) {\r\n print(\"adViewWillDismissScreen\")\r\n \/\/bannerBaseView.isHidden = true\r\n }\r\n \r\n \/\/\/ Tells the delegate that the full-screen view has been dismissed.\r\n func adViewDidDismissScreen(_ bannerView: GADBannerView) {\r\n print(\"adViewDidDismissScreen\")\r\n \/\/bannerBaseView.isHidden = true\r\n }\r\n \r\n \/\/\/ Tells the delegate that a user click will open another app (such as\r\n \/\/\/ the App Store), backgrounding the current app.\r\n func adViewWillLeaveApplication(_ bannerView: GADBannerView) {\r\n print(\"adViewWillLeaveApplication\")\r\n \/\/bannerBaseView.isHidden = true\r\n }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Admob\u306e\u30e2\u30b8\u30e5\u30fc\u30eb\u3092Pod\u3067\u30a4\u30f3\u30b9\u30c8<\/p>\n","protected":false},"author":1,"featured_media":1551,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/posts\/1550"}],"collection":[{"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/comments?post=1550"}],"version-history":[{"count":1,"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/posts\/1550\/revisions"}],"predecessor-version":[{"id":1552,"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/posts\/1550\/revisions\/1552"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/media\/1551"}],"wp:attachment":[{"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/media?parent=1550"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/categories?post=1550"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/tags?post=1550"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}