Untitled

 avatar
unknown
swift
2 years ago
4.2 kB
5
Indexable

class ViewController: UIViewController {
    
    var adView: YMANativeAdView = {
        let view = YMANativeAdView()
        
        view.titleLabel = UILabel()
        view.titleLabel?.backgroundColor = .red
        
        view.addSubview(view.titleLabel!)
        view.titleLabel?.translatesAutoresizingMaskIntoConstraints = false
        view.titleLabel?.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        view.titleLabel?.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
        view.titleLabel?.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
        
        view.mediaView = .init()
        view.addSubview(view.mediaView!)
        view.mediaView?.translatesAutoresizingMaskIntoConstraints = false
        view.mediaView?.topAnchor.constraint(equalTo: view.titleLabel!.bottomAnchor, constant: 10).isActive = true
        view.mediaView?.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
        view.mediaView?.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
        view.mediaView?.heightAnchor.constraint(equalToConstant: 200).isActive = true
        
        
        return view
    }()
    
    var adLoader: YMANativeAdLoader!


    override func viewDidLoad() {
        super.viewDidLoad()
        setup()
    }
    
    
    func setup() {
        adLoader = YMANativeAdLoader()
        adLoader.delegate = self
        
        
        view.addSubview(adView)
        adView.translatesAutoresizingMaskIntoConstraints = false
        adView.topAnchor.constraint(equalTo: view.topAnchor, constant: 100).isActive = true
        adView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
        adView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
        adView.heightAnchor.constraint(equalToConstant: 400).isActive = true
        
        
        // Код из интерфейса Adfox для работы с прямыми кампаниями.
        var parameters = [String: String]()
        parameters["adf_ownerid"] = "270901"
        parameters["adf_p1"] = "caboj"
        parameters["adf_p2"] = "fksh"
        parameters["adf_pfc"] = "bskug"
        parameters["adf_pfb"] = "fkjas"
        parameters["adf_pt"] = "b"
        
        let requestConfiguration = YMAMutableNativeAdRequestConfiguration(adUnitID: "R-M-243655-10")
        requestConfiguration.parameters = parameters

        adLoader.loadAd(with: requestConfiguration)
    }
}


extension ViewController: YMANativeAdLoaderDelegate {
    
//    func interstitialAdDidLoad(_ interstitialAd: YMAInterstitialAd) {
//        interstitialAd.present(from: self)
//    }
//
//    func interstitialAdDidFail(toLoad interstitialAd: YMAInterstitialAd, error: Error) {
//        print("toLoad error = ", error)
//    }
//
//    func interstitialAdDidFail(toPresent interstitialAd: YMAInterstitialAd, error: Error) {
//        print("toPresent error = ", error)
//    }
    
    func nativeAdLoader(_ loader: YMANativeAdLoader, didLoad ad: YMANativeAd) {
        print("loader didLoad")
        
        ad.delegate = self
                
                do {
                    try ad.bind(with: adView)
                    adView.ad?.adAssets()
                    
//                    addAdView()
                } catch {
                    print("Binding error: \(error)")
                }
        
    }
    
    func nativeAdLoader(_ loader: YMANativeAdLoader, didFailLoadingWithError error: Error) {
        print("didFailLoadingWithError error = ", error)
    }
}

extension ViewController: YMANativeAdDelegate {

    // Uncomment to open web links in in-app browser
//    func viewControllerForPresentingModalView() -> UIViewController? {
//        return self
//    }
    func nativeAdWillLeaveApplication(_ ad: YMANativeAd) {
        print("Will leave application")
    }

    func nativeAd(_ ad: YMANativeAd, willPresentScreen viewController: UIViewController?) {
        print("Will present screen")
    }

    func nativeAd(_ ad: YMANativeAd, didDismissScreen viewController: UIViewController?) {
        print("Did dismiss screen")
    }

    func close(_ ad: YMANativeAd) {
        print("close")
    }
}
Editor is loading...