FBUnityUtility.mm 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. // Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  2. //
  3. // You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
  4. // copy, modify, and distribute this software in source code or binary form for use
  5. // in connection with the web services and APIs provided by Facebook.
  6. //
  7. // As with any software that integrates with the Facebook platform, your use of
  8. // this software is subject to the Facebook Developer Principles and Policies
  9. // [http://developers.facebook.com/policy/]. This copyright notice shall be
  10. // included in all copies or substantial portions of the software.
  11. //
  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, FITNESS
  14. // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  15. // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  16. // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  17. // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  18. #import "FBUnityUtility.h"
  19. #include <string>
  20. #import <Foundation/Foundation.h>
  21. #import <FBSDKCoreKit/FBSDKCoreKit.h>
  22. #import <FBSDKLoginKit/FBSDKLoginKit.h>
  23. #import <FBSDKShareKit/FBSDKShareKit.h>
  24. // Value-tested, never with defined(). See the note in FBUnityInterface.h.
  25. #if UNITY_XCODE_PROJECT_TYPE_SWIFT
  26. // UnitySendMessage resolves through the trampoline prefix header under the Objective-C Xcode
  27. // project type. The Swift project type has no prefix header, so the public plugin interface has
  28. // to be imported explicitly.
  29. #import "UnityPluginInterface.h"
  30. #endif
  31. const char* const FB_OBJECT_NAME = "UnityFacebookSDKPlugin";
  32. // Helper method to create C string copy
  33. static char* FBUnityMakeStringCopy (const char* string)
  34. {
  35. if (string == NULL)
  36. return NULL;
  37. char* res = (char*)malloc(strlen(string) + 1);
  38. strcpy(res, string);
  39. return res;
  40. }
  41. @implementation FBUnityUtility
  42. + (void) sendCancelToUnity:(NSString *)unityMessage
  43. requestId:(int)requestId
  44. {
  45. [self sendMessageToUnity:unityMessage
  46. userData:@{ @"cancelled" : @"true" }
  47. requestId:requestId];
  48. }
  49. + (void) triggerUploadViewHierarchy
  50. {
  51. [self sendMessageToUnity:@"CaptureViewHierarchy"
  52. userData:nil
  53. requestId:0];
  54. }
  55. + (void) triggerUpdateBindings:(NSString *)json
  56. {
  57. [self sendMessageToUnity:@"OnReceiveMapping"
  58. message:json
  59. requestId:0];
  60. }
  61. + (void)sendErrorToUnity:(NSString *)unityMessage
  62. error:(NSError *)error
  63. requestId:(int)requestId
  64. {
  65. NSString *errorMessage =
  66. error.userInfo[FBSDKErrorLocalizedDescriptionKey] ?:
  67. error.userInfo[FBSDKErrorDeveloperMessageKey] ?:
  68. error.localizedDescription;
  69. [self sendErrorToUnity:unityMessage
  70. errorMessage:errorMessage
  71. requestId:requestId];
  72. }
  73. + (void)sendErrorToUnity:(NSString *)unityMessage
  74. errorMessage:(NSString *)errorMessage
  75. requestId:(int)requestId
  76. {
  77. [self sendMessageToUnity:unityMessage
  78. userData:@{ @"error" : errorMessage }
  79. requestId:requestId];
  80. }
  81. + (void)sendMessageToUnity:(NSString *)unityMessage
  82. userData:(NSDictionary *)userData
  83. requestId:(int)requestId
  84. {
  85. NSMutableDictionary *resultDictionary = [ @{ @"callback_id": [@(requestId) stringValue] } mutableCopy];
  86. [resultDictionary addEntriesFromDictionary:userData];
  87. if (![NSJSONSerialization isValidJSONObject:resultDictionary]) {
  88. [self sendErrorToUnity:unityMessage errorMessage:@"Result cannot be converted to json" requestId:requestId];
  89. return;
  90. }
  91. NSError *serializationError = nil;
  92. NSData *jsonData = [NSJSONSerialization dataWithJSONObject:resultDictionary options:0 error:&serializationError];
  93. if (serializationError) {
  94. [self sendErrorToUnity:unityMessage error:serializationError requestId:requestId];
  95. return;
  96. }
  97. NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
  98. if (!jsonString) {
  99. [self sendErrorToUnity:unityMessage errorMessage:@"Failed to generate response string" requestId:requestId];
  100. return;
  101. }
  102. const char *cString = [jsonString UTF8String];
  103. UnitySendMessage(FB_OBJECT_NAME, [unityMessage cStringUsingEncoding:NSASCIIStringEncoding], FBUnityMakeStringCopy(cString));
  104. }
  105. + (void)sendMessageToUnity:(NSString *)unityMessage
  106. message:(NSString *)message
  107. requestId:(int)requestId
  108. {
  109. const char *cString = [message UTF8String];
  110. UnitySendMessage(FB_OBJECT_NAME, [unityMessage cStringUsingEncoding:NSASCIIStringEncoding], FBUnityMakeStringCopy(cString));
  111. }
  112. + (NSString *)stringFromCString:(const char *)string {
  113. if (string && string[0] != 0) {
  114. return [NSString stringWithUTF8String:string];
  115. }
  116. return nil;
  117. }
  118. + (NSDictionary *)dictionaryFromKeys:(const char **)keys
  119. values:(const char **)vals
  120. length:(int)length
  121. {
  122. NSMutableDictionary *params = nil;
  123. if(length > 0 && keys && vals) {
  124. params = [NSMutableDictionary dictionaryWithCapacity:length];
  125. for(int i = 0; i < length; i++) {
  126. if (vals[i] && vals[i] != 0 && keys[i] && keys[i] != 0) {
  127. params[[NSString stringWithUTF8String:keys[i]]] = [NSString stringWithUTF8String:vals[i]];
  128. }
  129. }
  130. }
  131. return params;
  132. }
  133. + (FBSDKGameRequestFilter) gameRequestFilterFromString:(NSString *)filter {
  134. if (filter.length == 0 || [filter isEqualToString:@"none"]) {
  135. return FBSDKGameRequestFilterNone;
  136. } else if ([filter isEqualToString:@"app_users"]) {
  137. return FBSDKGameRequestFilterAppUsers;
  138. } else if ([filter isEqualToString:@"app_non_users"]) {
  139. return FBSDKGameRequestFilterAppNonUsers;
  140. }
  141. NSLog(@"Unexpected filter type: %@", filter);
  142. return FBSDKGameRequestFilterNone;
  143. }
  144. + (FBSDKGameRequestActionType) gameRequestActionTypeFromString:(NSString *)actionType {
  145. NSString *actionUpper = [actionType uppercaseString];
  146. if (actionUpper.length == 0 || [actionUpper isEqualToString:@"NONE"]) {
  147. return FBSDKGameRequestActionTypeNone;
  148. } else if ([actionUpper isEqualToString:@"SEND"]) {
  149. return FBSDKGameRequestActionTypeSend;
  150. } else if ([actionUpper isEqualToString:@"ASKFOR"]) {
  151. return FBSDKGameRequestActionTypeAskFor;
  152. } else if ([actionUpper isEqualToString:@"TURN"]) {
  153. return FBSDKGameRequestActionTypeTurn;
  154. }
  155. NSLog(@"Unexpected action type: %@", actionType);
  156. return FBSDKGameRequestActionTypeNone;
  157. }
  158. + (NSDictionary *)appLinkDataFromUrl:(NSURL *)url
  159. {
  160. NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
  161. if (url) {
  162. [dict setObject:url.absoluteString forKey:@"url"];
  163. FBSDKURL *parsedUrl = [FBSDKURL URLWithInboundURL:url sourceApplication:nil];
  164. if (parsedUrl) {
  165. if (parsedUrl.appLinkExtras) {
  166. [dict setObject:parsedUrl.appLinkExtras forKey:@"extras"];
  167. // TODO - Try to parse ref param out and pass back
  168. }
  169. if (parsedUrl.targetURL) {
  170. [dict setObject:parsedUrl.targetURL.absoluteString forKey:@"target_url"];
  171. }
  172. }
  173. } else {
  174. [dict setObject:@true forKey:@"did_complete"];
  175. }
  176. return dict;
  177. }
  178. + (NSDictionary *)getUserDataFromAccessToken:(FBSDKAccessToken *)token
  179. {
  180. if (token) {
  181. if (token.tokenString &&
  182. token.expirationDate &&
  183. token.userID &&
  184. token.permissions &&
  185. token.declinedPermissions) {
  186. NSInteger expiration = token.expirationDate.timeIntervalSince1970;
  187. NSInteger lastRefreshDate = token.refreshDate ? token.refreshDate.timeIntervalSince1970 : 0;
  188. return @{
  189. @"opened" : @"true",
  190. @"access_token" : token.tokenString,
  191. @"expiration_timestamp" : [@(expiration) stringValue],
  192. @"user_id" : token.userID,
  193. @"permissions" : [token.permissions allObjects],
  194. @"granted_permissions" : [token.permissions allObjects],
  195. @"declined_permissions" : [token.declinedPermissions allObjects],
  196. @"last_refresh" : [@(lastRefreshDate) stringValue],
  197. @"graph_domain" : @"facebook",
  198. };
  199. }
  200. }
  201. return nil;
  202. }
  203. @end