iAd在Tab Bar Controller中切换标签消失/失败

时间:2015-11-20 02:35:59

标签: ios swift uitabbarcontroller swift2 iad

我正在尝试将iAds应用到我的使用标签栏控制器的应用程序中。在测试广告时,似乎当我从一个标签移动到另一个标签时,iAd失败并切换到Admob(当iAds失败时,这是我的回填)。这种行为并不总是发生,但除非用户强制关闭应用程序然后重新启动应用程序,否则我无法取回iAd。我通过App Delegate使用共享的iAds来实现我的目的。我知道这需要查看很多代码,但似乎这个问题之前已经在StackOverflow上被提出过,但从未解决过。

的ViewController:

class ViewController {

override func viewDidLoad() {
        super.viewDidLoad()
        UIiAd.hidden = true
        gAds?.hidden = true
        adview.hidden = true
     }

override func viewWillAppear(animated: Bool) {
        if(!defaults.boolForKey("removeads") && trackAds != true && trackUIiAds == false) {
            let navigationBarHeight: CGFloat = self.navigationController!.navigationBar.frame.height
            UIiAd = self.appdelegate().UIiAd
            UIiAd.delegate = self
            adview = UIView(frame: CGRectMake(0, navigationBarHeight + 20, UIiAd.frame.width, UIiAd.frame.height))
            adview.addSubview(UIiAd)
            adview.backgroundColor = UIColor.clearColor()
            //UIiAd.frame = adview.frame
            self.view.insertSubview(adview, aboveSubview: mapView)
            print("my ad added")
        } else if defaults.boolForKey("removeads") {
            UIiAd.hidden = true
            gAds?.hidden = true
        }

    }

func bannerViewDidLoadAd(banner: ADBannerView!) {
        print("my ad loaded")
        UIiAd.hidden = false
        adview.hidden = false
        //gAds?.hidden = true
        trackUIiAds = true
        UIView.beginAnimations(nil, context: nil)
        UIView.setAnimationDuration(1)
        UIiAd.alpha = 1
        UIView.commitAnimations()
    }

    func bannerViewWillLoadAd(banner: ADBannerView!) {

    }

    func bannerViewActionDidFinish(banner: ADBannerView!) {

    }


    func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool {
        return true
    }

    func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
        UIiAd.hidden = true
        //NSLog("Error!")
        UIView.beginAnimations(nil, context: nil)
        UIView.setAnimationDuration(1)
        UIiAd.alpha = 0
        UIView.commitAnimations()
        UIiAd.removeFromSuperview()
        adview.removeFromSuperview()
        trackAds = true
        gAds = GADBannerView(adSize: kGADAdSizeBanner)
        //print("Google Mobile Ads SDK version: " + GADRequest.sdkVersion())
        gAds.adUnitID = "ca-app-pub-"
        gAds.rootViewController = self
        gAds.delegate = self
        let navigationBarHeight: CGFloat = self.navigationController!.navigationBar.frame.height
        gadview = UIView(frame: CGRectMake(0, navigationBarHeight + 20, self.view.frame.width, self.view.frame.height))
        gAds.frame = CGRectMake(self.view.frame.minX,
            navigationBarHeight + 20,
            self.view.frame.width,
            self.gAds.frame.size.height)
        let request = GADRequest()
        request.testDevices = [ kGADSimulatorID ]
        gAds.loadRequest(request)
        self.view.insertSubview(gAds, aboveSubview: mapView)
    }



    func adViewDidReceiveAd(bannerView: GADBannerView!) {
        gAds?.hidden = false
        //UIiAd.hidden = true
        UIView.beginAnimations(nil, context: nil)
        UIView.setAnimationDuration(1)
        gAds.alpha = 1
        UIView.commitAnimations()
    }

    func adView(bannerView: GADBannerView!, didFailToReceiveAdWithError error: GADRequestError!) {
        gAds?.hidden = true
        UIView.beginAnimations(nil, context: nil)
        UIView.setAnimationDuration(1)
        gAds.alpha = 0
        gAds.removeFromSuperview()
        UIView.commitAnimations()
    }

TableViewController:

class SettingsTableViewController {

    override func viewWillAppear(animated: Bool) {
    let tabBarHeight = self.tabBarController?.tabBar.bounds.height
        if (settingsDefault.boolForKey("removeads")) {
            self.tableView.contentInset = UIEdgeInsets(top: 64.0, left: 0.0, bottom: tabBarHeight!, right: 0.0)
            cellsArray = [.tempConversion,.ads, .radarType, .mapType, .radarSpeed, .numFrames, .otherSettings]
        } else {
            UIiAd = self.appdelegate().UIiAd
            UIiAd.delegate = self
            self.UIiAd.frame = CGRectMake(0, 0, self.UIiAd.frame.size.width, self.UIiAd.frame.size.height)
            self.tableView.contentInset = UIEdgeInsets(top: 64.0, left: 0.0, bottom: tabBarHeight!, right: 0.0)
        }
        let barViewControllers = self.tabBarController?.viewControllers
        let navCon = barViewControllers![0] as! UINavigationController
        if let WTable = navCon.visibleViewController as? WeatherTableViewController {
            locationTime = WTable.trackTimeForPlace
            self.setTableViewBackgroundGradient(self, time: locationTime!)
        }

        self.tableView.reloadData()

    }

   func bannerViewDidLoadAd(banner: ADBannerView!) {
        UIiAd.hidden = false
        UIView.beginAnimations(nil, context: nil)
        UIView.setAnimationDuration(1)
        UIiAd.alpha = 1
        UIView.commitAnimations()
    }

    func bannerViewWillLoadAd(banner: ADBannerView!) {

    }


    func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool {
        return true
    }

    func bannerViewActionDidFinish(banner: ADBannerView!){

    }

    func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
        UIiAd.hidden = true
        //NSLog("Error!")
        UIView.beginAnimations(nil, context: nil)
        UIView.setAnimationDuration(1)
        UIiAd.alpha = 0
        UIView.commitAnimations()
        UIiAd.removeFromSuperview()
        trackAds = "googleAds"
        //dispatch_async(dispatch_get_main_queue(), {
            let path = NSIndexPath(forRow: 0, inSection: 0)
            self.tableView.reloadRowsAtIndexPaths([path], withRowAnimation: .None)
        //})

    }

    func adViewDidReceiveAd(bannerView: GADBannerView!) {
        gAds?.hidden = false
        UIView.beginAnimations(nil, context: nil)
        UIView.setAnimationDuration(1)
        gAds.alpha = 1
        UIView.commitAnimations()
    }

    func adView(bannerView: GADBannerView!, didFailToReceiveAdWithError error: GADRequestError!) {
        UIView.beginAnimations(nil, context: nil)
        UIView.setAnimationDuration(1)
        gAds.alpha = 0
        UIView.commitAnimations()
        trackAds = "appleAds"
        //dispatch_async(dispatch_get_main_queue(), {
            let path = NSIndexPath(forRow: 0, inSection: 0)
            self.tableView.reloadRowsAtIndexPaths([path], withRowAnimation: .None)
        //})
        UIiAd.hidden = false

    }

   override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

       case .adBanner:
            let cell = self.tableView.dequeueReusableCellWithIdentifier(cellInfo.cellIdentifier, forIndexPath: indexPath) as! WeatherSettingsTableViewCell
            print("my ad added")
            switch(trackAds) {
            case "appleAds":
                cell.contentView.addSubview(UIiAd)
            case "googleAds":
                gAds = GADBannerView(adSize: kGADAdSizeBanner)
                gAds.adUnitID = "ca-app-pub-"
                gAds.delegate = self
                gAds.frame = CGRectMake(0, 0, cell.contentView.frame.width, cell.contentView.frame.height)
                gAds.rootViewController = self
                let request = GADRequest()
                request.testDevices = [ "" ]
                gAds.loadRequest(request)
                cell.contentView.addSubview(gAds)
            default:
                cell.contentView.addSubview(UIiAd)
            }

            cell.backgroundColor = UIColor.clearColor()
            return cell
    }

0 个答案:

没有答案