Upgrade to Pro — share decks privately, control downloads, hide ads and more …

アドフリくんSDKをSwift4で組み込む

 アドフリくんSDKをSwift4で組み込む

studioshin

June 25, 2018
Tweet

More Decks by studioshin

Other Decks in Programming

Transcript

  1. func makeBannerAd() { let adfurikunView = AdfurikunView(frame: CGRect(x: 0, y:

    0, width: 320, height: 50)) self.view.addSubview(adfurikunView) adfurikunView.appID = “޿ࠂID” adfurikunView.transitionDulation = 0.5 adfurikunView.testModeEnable() // ςετϞʔυ adfurikunView.startShowAd() adfurikunView.center = CGPoint(x: self.view.frame.size.width/2, y: self.view.frame.size.height - 25) } όφʔ޿ࠂͷදࣔ 6*7JFX$POUSPMMFSʹදࣔ͢Δ૝ఆ
  2. ΠϯλʔεςΠγϟϧ޿ࠂͷදࣔ 6*7JFX$POUSPMMFSʹදࣔ͢Δ૝ఆ var adfurikunInter: AdfurikunPopupView! func makeInterstisialAd() { self.adfurikunInter =

    AdfurikunPopupView() self.adfurikunInter.appID = “޿ࠂID” self.adfurikunInter.testModeEnable() // ςετϞʔυ self.adfurikunInter.setSchedule(1) self.adfurikunInter.preloadAd() } @IBAction func showInterstisialAd(_ sender: Any) { self.view.addSubview(self.adfurikunInter) self.adfurikunInter.center = CGPoint(x: self.view.frame.size.width/2, y: self.view.frame.size.height/2) self.adfurikunInter.preloadShowAd() }
  3. ωΠςΟϒ޿ࠂͷදࣔ var adfurikunNative: AdfurikunNativeAd! var nativeLinkURL: URL? //޿ࠂϦϯΫ var imageView:

    UIImageView! //޿ࠂը૾ var titleLabel: UILabel! //޿ࠂλΠτϧ var textLabel: UILabel! //޿ࠂςΩετ var nativeAdView: UIView! //޿ࠂදࣔϏϡʔ ޿ࠂදࣔ༻ͷϓϩύςΟΛఆٛ
  4. ωΠςΟϒ޿ࠂͷදࣔ func nativeAd(size: CGSize) -> UIView { let adView =

    UIView(frame: CGRect(x: 0, y: 0, width: size.width, height: size.height)) self.nativeAdView = adView adView.backgroundColor = UIColor.white //ը૾ self.imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: size.height, height: size.height)) imageView.contentMode = .scaleAspectFit adView.addSubview(imageView) //λΠτϧ self.titleLabel = UILabel(frame: CGRect(x: size.height + 5.0, y: 0, width: size.width - (size.height + 5.0), height: size.height * 0.3)) titleLabel.numberOfLines = 2 titleLabel.minimumScaleFactor = 0.5 adView.addSubview(titleLabel) //ςΩετ self.textLabel = UILabel(frame: CGRect(x: size.height + 5.0, y: size.height * 0.3, width: size.width - (size.height + 5.0), height: size.height * 0.7)) textLabel.numberOfLines = 4 textLabel.minimumScaleFactor = 0.5 adView.addSubview(textLabel) ޿ࠂදࣔ༻ͷϏϡʔΛ࡞Δ
  5. ωΠςΟϒ޿ࠂͷදࣔ func apiDidFinishLoading(_ nativeAdInfo: AdfurikunNativeAdInfo, adnetworkKey networkKey: String!) { if

    let url = URL(string: nativeAdInfo.img_url) { let task = URLSession.shared.dataTask(with: url) { data, response, error in if let d = data, error == nil{ DispatchQueue.main.async { self.imageView.image = UIImage(data: d) //ը૾ self.titleLabel.text = nativeAdInfo.title //λΠτϧ self.textLabel.text = nativeAdInfo.text //ςΩετ //ϦϯΫ if let url = nativeAdInfo.link_url { self.nativeLinkURL = URL(string: url) } //λοϓδΣενϟʔ let tapGesture = UITapGestureRecognizer(target: self.nativeAdView, action: #selector(self.naitiveAdTap(_:))) self.nativeAdView.addGestureRecognizer(tapGesture) } } } task.resume() } } func apiDidFailWithError(_ err: Int32, adnetworkKey: String!) { print("Native Ad error: \(adnetworkKey)=\(err)") } σϦήʔτϝιουͰϦιʔεΛऔಘ ϝΠϯεϨουͰઃఆ AdfurikunNativeAdDelegate λοϓΛݕ஌Ͱ͖ΔΑ͏ʹ ͢Δɻ
  6. ωΠςΟϒ޿ࠂͷදࣔ @objc func naitiveAdTap(_ tap: UITapGestureRecognizer) { if let url

    = self.nativeLinkURL, UIApplication.shared.canOpenURL(url) { UIApplication.shared.open(url, options: [:], completionHandler: { (ok) in AdfurikunNativeAdInfo.makeInstance().recClick() }) } } ޿ࠂΫϦοΫͷ௨஌
  7. ಈըωΠςΟϒ޿ࠂͷදࣔ func makeVideoNativeView(size: CGSize) -> UIView { let rect =

    CGRect(x: 0, y: 0, width: size.width, height: size.height) let adView: UIView = UIView(frame: rect) if ADFmyMovieNativeAdView.isSupportedOSVersion() { self.adfurikunMovieNativeView = ADFmyMovieNativeAdView(appID: “޿ࠂID”, layoutPattern: .default) self.adfurikunMovieNativeView.autoLoad = true self.adfurikunMovieNativeView.loadAndNotify(to: self) } self.videoNativeAdView = adView return adView } ޿ࠂදࣔ༻ͷϏϡʔΛ࡞Δ σϦήʔτΛઃఆ ADFmyMovieNativeAdViewDelegate
  8. ಈըωΠςΟϒ޿ࠂͷදࣔ //޿ࠂͷऔಘ੒ޭ func onNativeMovieAdViewLoadFinish(_ appID: String) { if let adv

    = self.adfurikunMovieNativeView.getAdView() { adv.bounds = CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height) self.addSubview(adv) adv.center = CGPoint(x: self.frame.size.width / 2, y: self.frame.size.height / 2) self.adfurikunMovieNativeView.setVideoDelegate(self) self.adfurikunMovieNativeView.playVideo() } } //޿ࠂͷऔಘࣦഊ func onNativeMovieAdViewLoadError(_ error: ADFMovieError, appID: String) { print("ωΠςΟϰ޿ࠂͷऔಘࣦഊ:\(error.errorMessage)") } ಈըͷऔಘ σϦήʔτΛઃఆ ADFmyNativeAdViewVideoDelegate ADFmyMovieNativeAdViewDelegate
  9. ಈըωΠςΟϒ޿ࠂͷදࣔ ಈըͷ࠶ੜ֬ೝσϦήʔτ //޿ࠂͷ࠶ੜ։࢝ func onNativeMovieAdViewPlayStart(_ appID: String) { print("޿ࠂͷ࠶ੜ։࢝:\(appID)") }

    //޿ࠂͷ࠶ੜ׬ྃ func onNativeMovieAdViewPlayFinish(_ appID: String, isVideo: Bool) { print("޿ࠂͷ࠶ੜ׬ྃ:\(appID) isVideo=\(isVideo)") } //޿ࠂͷ࠶ੜࣦഊ func onNativeMovieAdViewPlayFail(_ appID: String, error: ADFMovieError) { print("޿ࠂͷ࠶ੜࣦഊ:\(appID) \(error.errorMessage)") } ADFmyNativeAdViewVideoDelegate
  10. ಈըΠϯλʔεςΠγϟϧ޿ࠂͷදࣔ if ADFmyMovieInterstitial.isSupportedOSVersion() { ADFmyMovieInterstitial.initWithAppID(“޿ࠂID”, viewController: self) } if let

    ad = ADFmyMovieInterstitial.getInstance(“޿ࠂID”, delegate: self) { if ad.isPrepared() { ad.play(withPresenting: self) } } ॳظԽ දࣔ ىಈ௚ޙʹ࣮ߦ σϦήʔτΛઃఆ ADFmyMovieRewardDelegate
  11. // ޿ࠂͷදࣔ४උ͕ऴΘͬͨ func adsFetchCompleted(_ isTestMode_inApp: Bool) { } // ޿ࠂͷද͕ࣔ։͔࢝ͨ͠

    func adsDidShow(_ adnetworkKey: String) { } // ޿ࠂͷදࣔΛ࠷ޙ·ͰऴΘ͔ͬͨ func adsDidCompleteShow() { } // ಈը޿ࠂ࠶ੜΤϥʔ࣌ͷΠϕϯτ func adsPlayFailed() { } // ޿ࠂΛดͨ࣌͡ͷΠϕϯτ func adsDidHide() { } ಈըΠϯλʔεςΠγϟϧ޿ࠂͷදࣔ σϦήʔτϝιου ADFmyMovieRewardDelegate σϦήʔτͷϓϩτίϧ͸ɺ ಈըϦϫʔυͱಉ͡ʂ ಈըϦϫʔυͷ৔߹ɺ͜ͷσϦήʔτϝιο υ͕ݺ͹Εͨ࣌఺ͰɺใुΛ༩͑Δɻ
  12. ॳظԽ දࣔ ಈըϦϫʔυ޿ࠂͷදࣔ if ADFmyMovieReward.isSupportedOSVersion() { ADFmyMovieReward.initWithAppID(“޿ࠂID”, viewController: self) self.adfurikunMovieReward

    = ADFmyMovieReward.getInstance(“޿ࠂID”, delegate: self) } ىಈ௚ޙʹ࣮ߦ if self.adfurikunMovieReward.isPrepared() == true { self.adfurikunMovieReward.play() } else { print("ADF ޿ࠂͷऔಘ͕׬͍ྃͯ͠·ͤΜ") } σϦήʔτΛઃఆ ADFmyMovieRewardDelegate
  13. // ޿ࠂͷදࣔ४උ͕ऴΘͬͨ func adsFetchCompleted(_ isTestMode_inApp: Bool) { } // ޿ࠂͷද͕ࣔ։͔࢝ͨ͠

    func adsDidShow(_ adnetworkKey: String) { } // ޿ࠂͷදࣔΛ࠷ޙ·ͰऴΘ͔ͬͨ func adsDidCompleteShow() { } // ಈը޿ࠂ࠶ੜΤϥʔ࣌ͷΠϕϯτ func adsPlayFailed() { } // ޿ࠂΛดͨ࣌͡ͷΠϕϯτ func adsDidHide() { } ಈըϦϫʔυ޿ࠂͷදࣔ σϦήʔτϝιου ADFmyMovieRewardDelegate ͜ͷσϦήʔτϝιου͕ݺ͹Εͨ࣌఺Ͱɺ ใुΛ༩͑Δɻ