AdAgentIOS.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import AdAgent from "./AdAgent";
  2. import { BannerLocation, BannerLocationToString } from "./YZ_Constant";
  3. import PlatUtils from "./PlatUtils";
  4. import { utils } from "./Utils";
  5. const { ccclass, property } = cc._decorator;
  6. const ST_JNIMessage: string = "JNIMessage";
  7. const ST_VideoCallback: string = "VideoCallback";
  8. @ccclass
  9. export default class AdAgentIOS extends AdAgent {
  10. private _className: string = "JNIHelper";
  11. private _videoCallback: Function = null;
  12. public get ServerConfig() {
  13. return utils.Tool_IOS.ServerConfig;
  14. }
  15. public Init() {
  16. if (PlatUtils.IsNativeIOS) {
  17. cc.game.on(ST_JNIMessage, (event: any) => {
  18. if (event.type == ST_VideoCallback) {
  19. if (this._videoCallback) {
  20. if (!event.ret) {
  21. this._videoCallback(event.ret, event.message ? event.message : "视频播放失败!");
  22. } else {
  23. this._videoCallback(event.ret);
  24. }
  25. }
  26. }
  27. });
  28. }
  29. }
  30. _showBannerTimerId: number = 0;
  31. public ShowBanner(location: BannerLocation = BannerLocation.Home, args: any = null) {
  32. if (PlatUtils.IsNativeIOS) {
  33. utils.showLog("AdAgentNative ShowBanner");
  34. // let interval: number = 18;
  35. // if (this.ServerConfig && this.ServerConfig.refresh_ad_time) {
  36. // interval = this.ServerConfig.refresh_ad_time;
  37. // }
  38. // try {
  39. jsb.reflection.callStaticMethod(this._className, "showBanner:", BannerLocationToString(location));
  40. // } catch (error) {
  41. // utils.showLog(error);
  42. // }
  43. // clearInterval(this._showBannerTimerId);
  44. // this._showBannerTimerId = setInterval(function () {
  45. // utils.showLog(`定时刷新显示Banner广告!location:${location}; args:${JSON.stringify(args)}; 间隔时间:${interval}`);
  46. // this.ShowBanner(location, args);
  47. // }.bind(this), interval * 1000);
  48. }
  49. }
  50. public HideBanner(location: BannerLocation = BannerLocation.Home) {
  51. clearInterval(this._showBannerTimerId);
  52. jsb.reflection.callStaticMethod(this._className, "hideBanner:", BannerLocationToString(location));
  53. }
  54. public ShowInterstitial() {
  55. if (PlatUtils.IsNativeIOS) {
  56. try {
  57. let delayShowTime = 1;
  58. if (this.ServerConfig && this.ServerConfig.intersititia_delay_show_time) {
  59. delayShowTime = this.ServerConfig.intersititia_delay_show_time;
  60. }
  61. utils.showLog("AdAgentNative ShowInterstitial 延迟", delayShowTime, "秒调用!");
  62. utils.delayCall(() => {
  63. jsb.reflection.callStaticMethod(this._className, "showInterstitial");
  64. }, delayShowTime);
  65. } catch (error) {
  66. utils.showLog(error);
  67. }
  68. }
  69. }
  70. public ShowVideo(callback: Function) {
  71. if (PlatUtils.IsNativeIOS) {
  72. utils.showLog("AdAgentNative ShowVideo");
  73. this._videoCallback = callback;
  74. try {
  75. jsb.reflection.callStaticMethod(this._className, "showVideo");
  76. } catch (error) {
  77. utils.showLog(error);
  78. if (callback) {
  79. callback(false);
  80. }
  81. }
  82. }
  83. }
  84. public showFullScreenVideo(callback?: Function) {
  85. if (PlatUtils.IsNativeIOS) {
  86. utils.showLog("AdAgentNative ShowVideo");
  87. this._videoCallback = callback;
  88. try {
  89. jsb.reflection.callStaticMethod(this._className, "showFullScreenVideo");
  90. } catch (error) {
  91. utils.showLog(error);
  92. if (callback) {
  93. callback(false);
  94. }
  95. }
  96. }
  97. }
  98. }
  99. export class NativeIosCallBack {
  100. /**
  101. * 1:播放完成
  102. * 2:播放失败
  103. * 3:无广告
  104. *
  105. */
  106. public static videoCallBack(result: string, msg: string) {
  107. utils.showLog("视频广告回调函数 ------>#result=" + result + " #msg=" + msg ? msg : "");
  108. if (result == "1") {
  109. //播放成功
  110. cc.game.emit(ST_JNIMessage, { type: ST_VideoCallback, ret: true });
  111. } else {
  112. //播放失败
  113. cc.game.emit(ST_JNIMessage, { type: ST_VideoCallback, ret: false, message: msg });
  114. }
  115. }
  116. }
  117. window["NativeIosCallBack"] = NativeIosCallBack;