njs_ios_gamecenter.html 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <meta charset="utf-8"/>
  5. <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
  6. <meta name="HandheldFriendly" content="true"/>
  7. <meta name="MobileOptimized" content="320"/>
  8. <title>Hello H5+</title>
  9. <script type="text/javascript" src="../js/common.js"></script>
  10. <script type="text/javascript">
  11. /*
  12. NSNotificationCenter *nc = nil;
  13. // 游戏玩家登录状态监听函数
  14. - (void)authenticationChanged:(NSNotification*)notification
  15. {
  16. // 获取游戏玩家共享实例对象
  17. GKLocalPlayer *player = notification.object;
  18. if ( player.isAuthenticated ) {
  19. // 玩家已登录认证,获取玩家信息
  20. [self playerInformation:player];
  21. } else {
  22. // 玩家未登录认证,提示用户登录
  23. NSLog(@"请登录!");
  24. }
  25. // 释放使用的对象
  26. [player release];
  27. }
  28. // 获取游戏玩家状态信息
  29. - (void)playerInformation:(GKPlayer *)player
  30. {
  31. // 获取游戏玩家的名称
  32. NSLog(@"Name: %@",player.displayName);
  33. }
  34. // 登录到游戏中心
  35. - (void)loginGamecenter
  36. {
  37. // 获取游戏玩家共享实例对象
  38. GKLocalPlayer *localplayer = [GKLocalPlayer localPlayer];
  39. // 判断游戏玩家是否已经登录认证
  40. if ( localplayer.isAuthenticated ) {
  41. // 玩家已登录认证,获取玩家信息
  42. [self playerInformation:localplayer];
  43. } else {
  44. // 监听用户登录状态变更事件
  45. if(!nc){
  46. nc = [NSNotificationCenter defaultCenter];
  47. [nc addObserver:self
  48. selector:@selector(authenticationChanged)
  49. name:@"GKPlayerAuthenticationDidChangeNotificationName"
  50. object:nil];
  51. }
  52. // 玩家未登录认证,发起认证请求
  53. [localplayer authenticateWithCompletionHandler:nil];
  54. NSLog(@"登录中...");
  55. }
  56. // 释放使用的对象
  57. [localplayer release];
  58. }
  59. // 停止监听登录游戏状态变化
  60. - (void)logoutGamecenter
  61. {
  62. // 取消监听用户登录状态变化
  63. if(nc){
  64. [nc removeObserver:self
  65. name:@"GKPlayerAuthenticationDidChangeNotificationName"
  66. object:nil];
  67. }
  68. }
  69. */
  70. // H5 plus事件处理
  71. var GKLocalPlayer=null,NSNotificationCenter=null;
  72. var nc=null,delegate=null;
  73. function plusReady(){
  74. plus.webview.currentWebview().setStyle({popGesture:"none"});
  75. if ( plus.os.name == "iOS" ) {
  76. GKLocalPlayer = plus.ios.importClass("GKLocalPlayer");
  77. NSNotificationCenter = plus.ios.importClass("NSNotificationCenter");
  78. longinGamecenter();
  79. } else {
  80. eStart.innerText = "欢迎您";
  81. bLogin = true;
  82. setTimeout( function(){
  83. plus.nativeUI.toast( "此平台不支持Game Center功能!" );
  84. }, 500 );
  85. }
  86. }
  87. if(window.plus){
  88. plusReady();
  89. }else{
  90. document.addEventListener("plusready",plusReady,false);
  91. }
  92. // DOMContentLoaded事件处理
  93. var eStart=null,bLogin=false;
  94. document.addEventListener("DOMContentLoaded",function(){
  95. eStart = document.getElementById( "start" );
  96. },false);
  97. // 游戏玩家登录状态监听函数
  98. function authenticationChanged( notification ){
  99. // 获取游戏玩家共享实例对象
  100. var player = notification.plusGetAttribute("object");
  101. if ( player.plusGetAttribute("isAuthenticated") ) {
  102. // 玩家已登录认证,获取玩家信息
  103. playerInformation(player);
  104. bLogin = true;
  105. } else {
  106. // 玩家未登录认证,提示用户登录
  107. setTimeout(function(){
  108. eStart.innerText = "请登录";
  109. bLogin = false;
  110. },500);
  111. }
  112. // 释放使用的对象
  113. // plus.ios.deleteObject(player);
  114. }
  115. // 获取游戏玩家状态信息
  116. function playerInformation( player ){
  117. var name = player.plusGetAttribute("displayName");
  118. eStart.innerText = name+" 已登录!";
  119. }
  120. // 登录到游戏中心
  121. function longinGamecenter(){
  122. if ( bLogin ){
  123. return;
  124. }
  125. // 获取游戏玩家共享实例对象
  126. var localplayer = GKLocalPlayer.localPlayer();
  127. // 判断游戏玩家是否已经登录认证
  128. if ( localplayer.plusGetAttribute("isAuthenticated") ) {
  129. // 玩家已登录认证,获取玩家信息
  130. playerInformation( localplayer );
  131. bLogin = true;
  132. } else {
  133. eStart.innerText = "登录中...";
  134. // 监听用户登录状态变更事件
  135. if(!nc){
  136. nc = NSNotificationCenter.defaultCenter();
  137. delegate||(delegate=plus.ios.implements("NSObject",{"authenticationChanged:":authenticationChanged}));
  138. nc.addObserverselectornameobject(delegate,
  139. plus.ios.newObject("@selector","authenticationChanged:"),
  140. "GKPlayerAuthenticationDidChangeNotificationName",
  141. null);
  142. }
  143. // 玩家未登录认证,发起认证请求
  144. localplayer.authenticateWithCompletionHandler(null);
  145. }
  146. // 释放使用的对象
  147. plus.ios.deleteObject(localplayer);
  148. }
  149. // 停止监听登录游戏状态变化
  150. function stopGamecenterObserver()
  151. {
  152. // 取消监听用户登录状态变化
  153. nc&&nc.removeObservernameobject(delegate,"GKPlayerAuthenticationDidChangeNotificationName",null);
  154. plus.ios.deleteObject(nc);nc = null;
  155. plus.ios.deleteObject(delegate);delegate = null;
  156. }
  157. </script>
  158. <style type="text/css">
  159. * {
  160. -webkit-user-select: none;
  161. -ms-touch-select: none;
  162. }
  163. html {
  164. width: 100%;
  165. height: 100%;
  166. }
  167. body {
  168. margin: 0;
  169. padding: 0;
  170. width: 100%;
  171. height: 100%;
  172. text-align: center;
  173. -webkit-touch-callout:none;
  174. -webkit-tap-highlight-color:rgba(0,0,0,0);
  175. }
  176. header {
  177. height: 44px;
  178. font-size: 22px;
  179. line-height: 44px;
  180. font-weight: bold;
  181. color: #000000;
  182. }
  183. .game {
  184. position:fixed;
  185. top:44px;
  186. width:100%;
  187. bottom: 60px;
  188. background: no-repeat center center url(../img/5.jpg);
  189. }
  190. .button {
  191. font-size: 18px;
  192. font-weight: normal;
  193. text-decoration: none;
  194. display: block;
  195. text-align: center;
  196. color: #fff;
  197. background-color: #FFCC33;
  198. border: 1px solid #ECB100;
  199. padding: .5em 0em;
  200. margin: .5em .7em;
  201. -webkit-border-radius: 5px;
  202. border-radius: 5px;
  203. }
  204. .button:active {
  205. outline: 0;
  206. -webkit-box-shadow: inset 0 3px 5px rgba(0, 0, 0, 0.125);
  207. box-shadow: inset 0 3px 5px rgba(0, 0, 0, 0.125);
  208. }
  209. .start {
  210. width: 50%;
  211. font-size: 18px;
  212. font-weight: normal;
  213. text-decoration: none;
  214. display: block;
  215. text-align: center;
  216. color: #666;
  217. background-color: #EBEBEB;
  218. border: 1px solid #E0E0E0;
  219. padding: .5em 0em;
  220. margin: .5em auto;
  221. -webkit-border-radius: 5px;
  222. border-radius: 5px;
  223. }
  224. </style>
  225. <style type="text/css" media="screen and (min-aspect-ratio: 1/1)">
  226. .autobg {
  227. background-size: auto 100%;
  228. }
  229. </style>
  230. <style type="text/css" media="screen and (max-aspect-ratio: 1/1)">
  231. .autobg {
  232. background-size: 100% auto;
  233. }
  234. </style>
  235. </head>
  236. <body style="background:#CCCCCC;">
  237. <header id=header>五子棋</header>
  238. <div class="game autobg">
  239. </div>
  240. <div style="position:fixed;bottom:0;width:100%;height:60px;text-align:center;">
  241. <div class="button" onclick="try{stopGamecenterObserver();}catch(e){}back();">退出</div>
  242. </div>
  243. <div style="position:fixed;top:30%;width:100%;text-align:center;">
  244. <div id="start" class="start" onclick="longinGamecenter();">登录中...</div>
  245. </div>
  246. </body>
  247. </html>