libgdx robovm admob IOS SDK的绑定

IOS里面主要的广告平台就是用的是ADMOB, 在最新的ADMOB采用的是framework的方式,在绑定项目https://github.com/BlueRiverInteractive/robovm-ios-bindings/tree/master/admob已经不能正常使用了. 看到大家都有需求, 加班到凌晨1点钟,总算把它搞定了. 遇到了有些问题始终解决不了. 下面来介绍下怎么使用.

1.遇到的困难,

因为admob中有几处奇葩的写法,导致无法使用隐藏的symbol. 解决方案是自定义了一个.a然后写绑定 搞定

2.使用方法

在core项目中定义接口

public interface ITools {
	public void showInterAd();
	public void showBanner();
}

然后在Game里面传入它

public class GdxGame extends ApplicationAdapter {
	SpriteBatch batch;
	Texture img;
	ITools tools;
	
	public GdxGame(ITools tools) {
		this.tools = tools;
	}
	
	@Override
	public void create () {
		batch = new SpriteBatch();
		img = new Texture("badlogic.jpg");
		
		tools.showInterAd();
		tools.showBanner();
	}
在IOS项目中实现该接口并写入传入

public class IOSLauncher extends IOSApplication.Delegate implements ITools{
	private static final String AD_UNIT_ID = "ca-app-pub-1743332321423234/7283499900";
	static IOSLauncher instance;
	
	@Override
	protected IOSApplication createApplication() {
		IOSApplicationConfiguration config = new IOSApplicationConfiguration();
		return new IOSApplication(new GdxGame(instance), config);
	}

	public static void main(String[] argv) {
		instance = new IOSLauncher();
		NSAutoreleasePool pool = new NSAutoreleasePool();
		UIApplication.main(argv, null, IOSLauncher.class);
		pool.close();
	}

	@Override
	public void showInterAd() {
		final GADInterstitial interstitial = new GADInterstitial(AD_UNIT_ID);
        interstitial.setDelegate(new GADInterstitialDelegateAdapter(){
        	@Override
        	public void didReceiveAd(GADInterstitial ad) {
        		super.didReceiveAd(ad);
        		interstitial.present(((IOSApplication) Gdx.app).getUIViewController());
        	}
        	
        	@Override
        	public void didFailToReceiveAd(GADInterstitial ad,
        			GADRequestError error) {
        		Gdx.app.log("xxx", error.getCode() + "");
        		super.didFailToReceiveAd(ad, error);
        	}
        });
        
        GADRequest request = GADRequest.create();
        List<String> devices = new ArrayList<String>();
        devices.add(GADRequest.GAD_SIMULATOR_ID);
        
        request.setTestDevices(devices);
        interstitial.loadRequest(request);
	}

	@Override
	public void showBanner() {
		 final GADBannerView mBannerView = new GADBannerView(GADAdSize.smartBannerPortrait());
	        mBannerView.setAdUnitID("ca-app-pub-1743332321423234/2574098708");

	        final UIViewController controller = ((IOSApplication) Gdx.app).getUIViewController();
	        
	        GADRequest request = GADRequest.create();
	        List<String> devices = new ArrayList<String>();
	        devices.add(GADRequest.GAD_SIMULATOR_ID);
	        mBannerView.setRootViewController(controller);	        
	        controller.getView().addSubview(mBannerView);
	        
	        mBannerView.loadRequest(request);
	}
}

应该能看懂吧? 不懂请留言


3.最重要的是环境搭建

因为自定义了一个.a文件, 所以把libGADAdSizeWrapped.a文件放入iOS项目的build/libs下面,并写入xml

<libs>
      <lib>z</lib>   
     <lib>build/libs/libGADAdSizeWrapped.a</lib>   
  </libs>

GoogleMobileAds.framework放入到build/libs里面,并在xml指定framework寻找路径(必须要加入这些framework才行哟)

 <frameworkPaths>
		<path>build/libs</path>
  </frameworkPaths>
  <frameworks>
    <framework>UIKit</framework>
    <framework>OpenGLES</framework>
    <framework>QuartzCore</framework>
    <framework>CoreGraphics</framework>
    <framework>OpenAL</framework>
    <framework>AudioToolbox</framework>
    <framework>AVFoundation</framework>
    <framework>CoreTelephony</framework>
    <framework>EventKit</framework>
    <framework>MessageUI</framework>
    <framework>EventKitUI</framework>
    <framework>SystemConfiguration</framework>
    <framework>CoreMedia</framework>
    <framework>GoogleMobileAds</framework>
  </frameworks>
  <weakFrameworks>
		<framework>AdSupport</framework>
		<framework>StoreKit</framework>
	</weakFrameworks>

至此书写完毕,可以运行了.......为了那个自定义.a我还去注册了个开发者账号呢,....蛋疼  99刀大洋 心痛

如果您觉得对你有帮助的话请点赞,谢谢!

源码地址:https://github.com/tianqiujie/robovm-ios-bindings


你可能感兴趣的:(ios,android,admob,iOS绑定,robovm)