ios - Monotouch.Dialog 和 iAds

标签 ios xamarin xamarin.ios iad monotouch.dialog

我正在玩 iAds。我想将它添加到基本上是一堆 Monotouch.Dialog View 的应用程序。

最好的做法是添加一个UIViewController,然后添加一个ADBannerView 和一个Monotouch.Dialog,或者我应该添加iAds View 到 Monotouch.Dialog ViewController?

最佳答案

我创建了一个很好用的包装器 ViewController,只需传入主应用程序的 View ,如 TabBar 或 SplitView 等,并使用 IADViewController 作为 RootViewCONtroller:

     public partial class IADViewController : UIViewController
{
    private UIViewController _anyVC;
    private MonoTouch.iAd.ADBannerView _ad;

    public IADViewController (UIViewController anyVC)
    {
        _anyVC = anyVC;
    }

    public override void ViewDidLoad ()
    {
        base.ViewDidLoad ();

        View.AddSubview (_anyVC.View);


        if (Common.Device.Is6AtLeast && Social.IsiAdCountry) {

            try {
                _ad = new MonoTouch.iAd.ADBannerView (MonoTouch.iAd.ADAdType.Banner);
                _ad.Hidden = true;
                _ad.FailedToReceiveAd += HandleFailedToReceiveAd;
                _ad.AdLoaded += HandleAdLoaded;
                View.BackgroundColor = UIColor.Clear;
                _anyVC.View.Frame = View.Bounds;
                View.AddSubview (_ad);
            } catch {
            }
        } else {
            Resize ();
        }
    }

    public override void DidRotate (UIInterfaceOrientation fromInterfaceOrientation)
    {
        base.DidRotate (fromInterfaceOrientation);
        Resize ();
    }

    public override void ViewDidAppear (bool animated)
    {
        base.ViewDidAppear (animated);
        Resize ();
    }

    void Resize ()
    {

        UIView.Animate (.25,
            () => {
                if (_ad !=null && _ad.Hidden == false) {
                    _anyVC.View.Frame = new RectangleF (0, 0, this.View.Bounds.Width, this.View.Bounds.Height - _ad.Frame.Height);
                } else {
                    _anyVC.View.Frame = View.Bounds;
                }
            });
        if(_ad!=null)
           _ad.Frame = new RectangleF (0, _anyVC.View.Bounds.Height, this.View.Bounds.Width, _ad.Frame.Height);
    }

    void HandleAdLoaded (object sender, EventArgs e)
    {
        if (_ad == null)
            return;
        _ad.Hidden = false;
        Resize ();
    }

    void HandleFailedToReceiveAd (object sender, AdErrorEventArgs e)
    {
        if (_ad == null)
            return;
        _ad.Hidden = true;
        Resize ();
    }
}

关于ios - Monotouch.Dialog 和 iAds,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20036725/

相关文章:

ios - 如何使用 Xamarin 表单获取 iOS 的设备序列号

c# - 通过 linq 在 iphone 中的 Monotouch/Xamarin 上获取 IP 地址

ios - 在同一 View Controller 中的另一个 UIView 后面进行 UIView 更新

ios - 如何以编程方式将图像保存在自定义位置?

xamarin.ios - 可移植类库 System.Object 错误

c# - Monotouch.Dialog 两个表

iphone - 如何在 AVPlayer 中播放完第一首歌曲后继续播放第二首歌曲

ios - 如何为apple iap 服务器到服务器通知设置测试和生产环境

ios - MvvmCross iOS : How to bind MapView Annotation to jump to another view?

c# - TextBlob 为空