RxSwift AdMob delegate wrapper

AdMobRxSwift is a very powerful framework and a lot of components are already available but often you’ll find yourself in a situation where you’d have to implement your own reactive extension in order for certain component to work seamlessly with the rest of your reactive codebase. This is the case when you’re using third party SDKs or libraries that you’re interacting with using different delegate methods. I created a sample project to wrap AdMob banner and interstitial view delegates.

In order to wrap a delegate you need to create an RxProxy for it. In my sample project, for the banner view delegate, I created a banner view delegate proxy RxGADBannerViewDelegateProxy. It needs to inherit from the base DelegateProxy class. It also needs to conform to the DelegateProxyType protocol. One last thing, it needs to conform to the actual protocol you want to wrap, so in this case it is GADBannerViewDelegate. There are three methods you need to implement, as shown below.

The next step is to create a Reactive extension with your delegate’s owner as a base. In this case it is the banner view class GADBannerView. Using methodInvoked you can observe selectors fired in the underlying delegate and map the arguments passed in the appropriate Observable. You can conveniently map different success and fail delegate methods into appropriate Observable object.

Sample project is located on my GitHub repo: https://github.com/lukagabric/RxSwiftDelegateWrapper

If you liked this blog post I’d appreciate if you followed me on Twitter

Related Post

Remember to share...Share on FacebookTweet about this on TwitterShare on Google+Share on LinkedInEmail this to someone

Leave a Reply

Your email address will not be published. Required fields are marked *