AdsWrapper.mm 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /****************************************************************************
  2. Copyright (c) 2013 cocos2d-x.org
  3. http://www.cocos2d+x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #import "AdsWrapper.h"
  21. #include "PluginUtilsIOS.h"
  22. #include "ProtocolAds.h"
  23. using namespace cocos2d::plugin;
  24. @implementation AdsWrapper
  25. + (void) onAdsResult:(id) obj withRet:(AdsResult) ret withMsg:(NSString*) msg
  26. {
  27. PluginProtocol* plugin = PluginUtilsIOS::getPluginPtr(obj);
  28. ProtocolAds* adsPlugin = dynamic_cast<ProtocolAds*>(plugin);
  29. if (adsPlugin) {
  30. const char* chMsg = [msg UTF8String];
  31. AdsResultCode cRet = (AdsResultCode) ret;
  32. AdsListener* listener = adsPlugin->getAdsListener();
  33. ProtocolAds::ProtocolAdsCallback callback = adsPlugin->getCallback();
  34. if (listener)
  35. {
  36. listener->onAdsResult(cRet, chMsg);
  37. }else if(callback){
  38. std::string stdmsg(chMsg);
  39. callback(cRet,stdmsg);
  40. }
  41. } else {
  42. PluginUtilsIOS::outputLog("Can't find the C++ object of the ads plugin");
  43. }
  44. }
  45. + (void) onPlayerGetPoints:(id) obj withPoints: (int) points
  46. {
  47. PluginProtocol* plugin = PluginUtilsIOS::getPluginPtr(obj);
  48. ProtocolAds* adsPlugin = dynamic_cast<ProtocolAds*>(plugin);
  49. if (adsPlugin) {
  50. AdsListener* listener = adsPlugin->getAdsListener();
  51. if (listener)
  52. {
  53. listener->onPlayerGetPoints(adsPlugin, points);
  54. }
  55. } else {
  56. PluginUtilsIOS::outputLog("Can't find the C++ object of the ads plugin");
  57. }
  58. }
  59. + (NSString*)buildVersion
  60. {
  61. NSString *SDKPlatformVersion = [[NSBundle mainBundle] infoDictionary][@"DTPlatformVersion"];
  62. if (SDKPlatformVersion) {
  63. return SDKPlatformVersion;
  64. }
  65. // adapted from http://stackoverflow.com/questions/25540140/can-one-determine-the-ios-sdk-version-used-to-build-a-binary-programmatically
  66. // form character set of digits and punctuation
  67. NSMutableCharacterSet *characterSet = [[NSCharacterSet decimalDigitCharacterSet] mutableCopy];
  68. [characterSet formUnionWithCharacterSet: [NSCharacterSet punctuationCharacterSet]];
  69. // get only those things in characterSet from the SDK name
  70. NSString *SDKName = [[NSBundle mainBundle] infoDictionary][@"DTSDKName"];
  71. NSArray *components = [[SDKName componentsSeparatedByCharactersInSet: [characterSet invertedSet]]
  72. filteredArrayUsingPredicate: [NSPredicate predicateWithFormat:@"length != 0"]];
  73. if([components count]) {
  74. return components[0];
  75. }
  76. return nil;
  77. }
  78. + (BOOL)wasBuiltForiOS8orLater
  79. {
  80. return [[self buildVersion] compare:@"8.0"] != NSOrderedAscending;
  81. }
  82. + (BOOL)requireRotation
  83. {
  84. return ![self wasBuiltForiOS8orLater] || ([[[UIDevice currentDevice] systemVersion] floatValue] < 8.0);
  85. }
  86. + (void) addAdView:(UIView*) view atPos:(AdsPosEnum) pos
  87. {
  88. UIViewController* controller = [AdsWrapper getCurrentRootViewController];
  89. if (nil == controller) {
  90. PluginUtilsIOS::outputLog("Can't get the UIViewController object");
  91. return;
  92. }
  93. CGSize rootSize = controller.view.frame.size;
  94. CGSize viewSize = view.frame.size;
  95. CGPoint viewOrigin;
  96. if ([self requireRotation] && UIInterfaceOrientationIsLandscape(controller.interfaceOrientation)){
  97. CGFloat temp = rootSize.width;
  98. rootSize.width = rootSize.height;
  99. rootSize.height = temp;
  100. }
  101. switch (pos) {
  102. case kPosTop:
  103. viewOrigin.x = (rootSize.width - viewSize.width) / 2;
  104. viewOrigin.y = 0.0f;
  105. break;
  106. case kPosTopLeft:
  107. viewOrigin.x = 0.0f;
  108. viewOrigin.y = 0.0f;
  109. break;
  110. case kPosTopRight:
  111. viewOrigin.x = rootSize.width - viewSize.width;
  112. viewOrigin.y = 0.0f;
  113. break;
  114. case kPosBottom:
  115. viewOrigin.x = (rootSize.width - viewSize.width) / 2;
  116. viewOrigin.y = rootSize.height - viewSize.height;
  117. break;
  118. case kPosBottomLeft:
  119. viewOrigin.x = 0.0f;
  120. viewOrigin.y = rootSize.height - viewSize.height;
  121. break;
  122. case kPosBottomRight:
  123. viewOrigin.x = rootSize.width - viewSize.width;
  124. viewOrigin.y = rootSize.height - viewSize.height;
  125. break;
  126. case kPosCenter:
  127. default:
  128. viewOrigin.x = (rootSize.width - viewSize.width) / 2;
  129. viewOrigin.y = (rootSize.height - viewSize.height) / 2;
  130. break;
  131. }
  132. CGRect rect = CGRectMake(viewOrigin.x, viewOrigin.y, viewSize.width, viewSize.height);
  133. view.frame = rect;
  134. [controller.view addSubview:view];
  135. }
  136. + (UIViewController *)getCurrentRootViewController {
  137. UIViewController *result = nil;
  138. // Try to find the root view controller programmically
  139. // Find the top window (that is not an alert view or other window)
  140. UIWindow *topWindow = [[UIApplication sharedApplication] keyWindow];
  141. if (topWindow.windowLevel != UIWindowLevelNormal)
  142. {
  143. NSArray *windows = [[UIApplication sharedApplication] windows];
  144. for(topWindow in windows)
  145. {
  146. if (topWindow.windowLevel == UIWindowLevelNormal)
  147. break;
  148. }
  149. }
  150. UIView *rootView = [[topWindow subviews] objectAtIndex:0];
  151. id nextResponder = [rootView nextResponder];
  152. if ([nextResponder isKindOfClass:[UIViewController class]])
  153. result = nextResponder;
  154. else if ([topWindow respondsToSelector:@selector(rootViewController)] && topWindow.rootViewController != nil)
  155. result = topWindow.rootViewController;
  156. else
  157. NSAssert(NO, @"Could not find a root view controller.");
  158. return result;
  159. }
  160. @end