iOSImpl.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. using System;
  2. using UnityEngine;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Runtime.InteropServices;
  6. namespace cn.sharesdk.unity3d {
  7. #if UNITY_IPHONE || UNITY_IOS
  8. public class iOSImpl : ShareSDKImpl {
  9. private string _callbackObjectName = "Main Camera";
  10. private string _appKey;
  11. [DllImport("__Internal")]
  12. private static extern void __iosShareSDKRegisterAppAndSetPltformsConfig(string appKey, string configInfo);
  13. [DllImport("__Internal")]
  14. private static extern void __iosShareSDKAuthorize(int reqID, int platType, string observer);
  15. [DllImport("__Internal")]
  16. private static extern void __iosShareSDKCancelAuthorize(int platType);
  17. [DllImport("__Internal")]
  18. private static extern bool __iosShareSDKHasAuthorized(int platType);
  19. [DllImport("__Internal")]
  20. private static extern void __iosShareSDKGetUserInfo(int reqID, int platType, string observer);
  21. [DllImport("__Internal")]
  22. private static extern void __iosShareSDKShare(int reqID, int platType, string content, string observer);
  23. [DllImport("__Internal")]
  24. private static extern void __iosShareSDKShareWithActivity(int reqID, int platType, string content, string observer);
  25. [DllImport("__Internal")]
  26. private static extern void __iosShareSDKOneKeyShare(int reqID, string platTypes, string content, string observer);
  27. [DllImport("__Internal")]
  28. private static extern void __iosShareSDKShowShareMenu(int reqID, string platTypes, string content, int x, int y, string observer);
  29. [DllImport("__Internal")]
  30. private static extern void __iosShareSDKShowShareView(int reqID, int platType, string content, string observer);
  31. [DllImport("__Internal")]
  32. private static extern void __iosShareSDKGetFriendsList(int reqID, int platType,int count, int page, string observer);
  33. [DllImport("__Internal")]
  34. private static extern void __iosShareSDKFollowFriend(int reqID, int platform,string account, string observer);
  35. [DllImport("__Internal")]
  36. private static extern string __iosShareSDKGetCredential(int platType);
  37. [DllImport("__Internal")]
  38. private static extern bool __iosShareSDKIsClientInstalled(int platType);
  39. [DllImport("__Internal")]
  40. private static extern void __iosShareSDKShareWithContentName(int reqID, int platform, string contentName, string customFields, string observer);
  41. [DllImport("__Internal")]
  42. private static extern void __iosShareSDKShowShareMenuWithContentName(int reqID, string contentName, string customFields, string platTypes, int x, int y, string observer);
  43. [DllImport("__Internal")]
  44. private static extern void __iosShareSDKShowShareViewWithContentName(int reqID, int platform, string contentName, string customFields, string observer);
  45. [DllImport("__Internal")]
  46. private static extern bool __iosShareSDKOpenMiniProgram(String userName, String path, int miniProgramType);
  47. [DllImport("__Internal")]
  48. private static extern bool __iosShareSDKWXRequestSendTokenToGetUser(String uid, String token);
  49. [DllImport("__Internal")]
  50. private static extern bool __iosShareSDKWXRequestToken(String observer);
  51. [DllImport("__Internal")]
  52. private static extern bool __iosShareSDKWXRefreshSendTokenToGetUser(String token);
  53. [DllImport("__Internal")]
  54. private static extern bool __iosShareSDKWXRefreshRequestToken(String observer);
  55. [DllImport("__Internal")]
  56. private static extern void __iosShareSDKShareWithCommand(string customFields, string observer);
  57. public iOSImpl(GameObject go) {
  58. try {
  59. _callbackObjectName = go.name;
  60. } catch(Exception e) {
  61. Console.WriteLine("{0} Exception caught.", e);
  62. }
  63. }
  64. public override void InitSDK(string appKey) {
  65. _appKey = appKey;
  66. }
  67. public override void InitSDK(string appKey, string secret) {
  68. _appKey = appKey;
  69. }
  70. public override void PrepareLoopShare() {
  71. throw new NotImplementedException();
  72. }
  73. public override void setChannelId() {
  74. throw new NotImplementedException();
  75. }
  76. public override void SetPlatformConfig(Hashtable configs) {
  77. String json = MiniJSON.jsonEncode(configs);
  78. if(Application.platform == RuntimePlatform.IPhonePlayer)
  79. {
  80. __iosShareSDKRegisterAppAndSetPltformsConfig(_appKey, json);
  81. }
  82. }
  83. public override void Authorize(int reqId, PlatformType platform) {
  84. __iosShareSDKAuthorize(reqId, (int)platform, _callbackObjectName);
  85. }
  86. public override void CancelAuthorize(PlatformType platform) {
  87. __iosShareSDKCancelAuthorize((int)platform);
  88. }
  89. public override bool IsAuthorized(PlatformType platform) {
  90. return __iosShareSDKHasAuthorized((int)platform);
  91. }
  92. public override bool IsClientValid(PlatformType platform) {
  93. return __iosShareSDKIsClientInstalled((int)platform);
  94. }
  95. public override void GetUserInfo(int reqId, PlatformType platform) {
  96. __iosShareSDKGetUserInfo(reqId, (int)platform, _callbackObjectName);
  97. }
  98. public override void ShareContent(int reqId, PlatformType platform, ShareContent content) {
  99. __iosShareSDKShare(reqId, (int)platform, content.GetShareParamsStr(), _callbackObjectName);
  100. }
  101. public override void ShareContent(int reqId, PlatformType[] platforms, ShareContent content) {
  102. string platTypesStr = null;
  103. if (platforms != null) {
  104. List<int> platTypesArr = new List<int>();
  105. foreach (PlatformType type in platforms) {
  106. platTypesArr.Add((int)type);
  107. }
  108. platTypesStr = MiniJSON.jsonEncode(platTypesArr.ToArray());
  109. }
  110. __iosShareSDKOneKeyShare(reqId, platTypesStr, content.GetShareParamsStr(), _callbackObjectName);
  111. }
  112. public override void ShowPlatformList(int reqId, PlatformType[] platforms, ShareContent content, int x, int y) {
  113. string platTypesStr = null;
  114. if (platforms != null) {
  115. List<int> platTypesArr = new List<int>();
  116. foreach (PlatformType type in platforms) {
  117. platTypesArr.Add((int)type);
  118. }
  119. platTypesStr = MiniJSON.jsonEncode(platTypesArr.ToArray());
  120. }
  121. __iosShareSDKShowShareMenu(reqId, platTypesStr, content.GetShareParamsStr(), x, y, _callbackObjectName);
  122. }
  123. public override void ShowShareContentEditor(int reqId, PlatformType platform, ShareContent content) {
  124. __iosShareSDKShowShareView(reqId, (int)platform, content.GetShareParamsStr(), _callbackObjectName);
  125. }
  126. public override void ShareWithContentName(int reqId, PlatformType platform, string contentName, Hashtable customFields) {
  127. String customFieldsStr = MiniJSON.jsonEncode(customFields);
  128. __iosShareSDKShareWithContentName(reqId, (int)platform, contentName, customFieldsStr, _callbackObjectName);
  129. }
  130. public override void ShowPlatformListWithContentName(int reqId, string contentName, Hashtable customFields, PlatformType[] platforms, int x, int y) {
  131. String customFieldsStr = MiniJSON.jsonEncode(customFields);
  132. string platTypesStr = null;
  133. if (platforms != null) {
  134. List<int> platTypesArr = new List<int>();
  135. foreach (PlatformType type in platforms) {
  136. platTypesArr.Add((int)type);
  137. }
  138. platTypesStr = MiniJSON.jsonEncode(platTypesArr.ToArray());
  139. }
  140. __iosShareSDKShowShareMenuWithContentName(reqId, contentName, customFieldsStr, platTypesStr, x, y, _callbackObjectName);
  141. }
  142. public override void ShowShareContentEditorWithContentName(int reqId, PlatformType platform, string contentName, Hashtable customFields) {
  143. String customFieldsStr = MiniJSON.jsonEncode(customFields);
  144. __iosShareSDKShowShareViewWithContentName(reqId, (int)platform, contentName, customFieldsStr, _callbackObjectName);
  145. }
  146. public override void GetFriendList(int reqID, PlatformType platform, int count, int page) {
  147. __iosShareSDKGetFriendsList(reqID, (int)platform, count, page, _callbackObjectName);
  148. }
  149. public override void AddFriend(int reqID, PlatformType platform, string account) {
  150. __iosShareSDKFollowFriend(reqID, (int)platform, account, _callbackObjectName);
  151. }
  152. public override Hashtable GetAuthInfo(PlatformType platform) {
  153. string credStr = __iosShareSDKGetCredential((int)platform);
  154. return (Hashtable)MiniJSON.jsonDecode(credStr);
  155. }
  156. public override void DisableSSO(bool disable) {
  157. Console.WriteLine ("#waring : no this interface on iOS");
  158. }
  159. public override bool openMiniProgram(string userName, string path, int miniProgramType) {
  160. return __iosShareSDKOpenMiniProgram(userName, path, miniProgramType);
  161. }
  162. public override void getWXRequestToken() {
  163. __iosShareSDKWXRequestToken(_callbackObjectName);
  164. }
  165. public override void getWXRefreshToken() {
  166. __iosShareSDKWXRefreshRequestToken(_callbackObjectName);
  167. }
  168. public override void sendWXRefreshToken(string token) {
  169. __iosShareSDKWXRefreshSendTokenToGetUser(token);
  170. }
  171. public override void sendWXRequestToken(string uid, string token) {
  172. __iosShareSDKWXRequestSendTokenToGetUser(uid, token);
  173. }
  174. public override void shareSDKWithCommand(Hashtable content) {
  175. String customFieldsStr = MiniJSON.jsonEncode(content);
  176. __iosShareSDKShareWithCommand(customFieldsStr, _callbackObjectName);
  177. }
  178. public override void ShareContentWithActivity(int reqID, PlatformType platform, ShareContent content) {
  179. __iosShareSDKShareWithActivity(reqID, (int)platform, content.GetShareParamsStr(), _callbackObjectName);
  180. }
  181. }
  182. #endif
  183. }