FBUnityUtility.mm 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. const char* const FB_OBJECT_NAME = "UnityFacebookSDKPlugin";
  25. // Helper method to create C string copy
  26. static char* FBUnityMakeStringCopy (const char* string)
  27. {
  28. if (string == NULL)
  29. return NULL;
  30. char* res = (char*)malloc(strlen(string) + 1);
  31. strcpy(res, string);
  32. return res;
  33. }
  34. @implementation FBUnityUtility
  35. + (void) sendCancelToUnity:(NSString *)unityMessage
  36. requestId:(int)requestId
  37. {
  38. [self sendMessageToUnity:unityMessage
  39. userData:@{ @"cancelled" : @"true" }
  40. requestId:requestId];
  41. }
  42. + (void) triggerUploadViewHierarchy
  43. {
  44. [self sendMessageToUnity:@"CaptureViewHierarchy"
  45. userData:nil
  46. requestId:0];
  47. }
  48. + (void) triggerUpdateBindings:(NSString *)json
  49. {
  50. [self sendMessageToUnity:@"OnReceiveMapping"
  51. message:json
  52. requestId:0];
  53. }
  54. + (void)sendErrorToUnity:(NSString *)unityMessage
  55. error:(NSError *)error
  56. requestId:(int)requestId
  57. {
  58. NSString *errorMessage =
  59. error.userInfo[FBSDKErrorLocalizedDescriptionKey] ?:
  60. error.userInfo[FBSDKErrorDeveloperMessageKey] ?:
  61. error.localizedDescription;
  62. [self sendErrorToUnity:unityMessage
  63. errorMessage:errorMessage
  64. requestId:requestId];
  65. }
  66. + (void)sendErrorToUnity:(NSString *)unityMessage
  67. errorMessage:(NSString *)errorMessage
  68. requestId:(int)requestId
  69. {
  70. [self sendMessageToUnity:unityMessage
  71. userData:@{ @"error" : errorMessage }
  72. requestId:requestId];
  73. }
  74. + (void)sendMessageToUnity:(NSString *)unityMessage
  75. userData:(NSDictionary *)userData
  76. requestId:(int)requestId
  77. {
  78. NSMutableDictionary *resultDictionary = [ @{ @"callback_id": [@(requestId) stringValue] } mutableCopy];
  79. [resultDictionary addEntriesFromDictionary:userData];
  80. if (![NSJSONSerialization isValidJSONObject:resultDictionary]) {
  81. [self sendErrorToUnity:unityMessage errorMessage:@"Result cannot be converted to json" requestId:requestId];
  82. return;
  83. }
  84. NSError *serializationError = nil;
  85. NSData *jsonData = [NSJSONSerialization dataWithJSONObject:resultDictionary options:0 error:&serializationError];
  86. if (serializationError) {
  87. [self sendErrorToUnity:unityMessage error:serializationError requestId:requestId];
  88. return;
  89. }
  90. NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
  91. if (!jsonString) {
  92. [self sendErrorToUnity:unityMessage errorMessage:@"Failed to generate response string" requestId:requestId];
  93. return;
  94. }
  95. const char *cString = [jsonString UTF8String];
  96. UnitySendMessage(FB_OBJECT_NAME, [unityMessage cStringUsingEncoding:NSASCIIStringEncoding], FBUnityMakeStringCopy(cString));
  97. }
  98. + (void)sendMessageToUnity:(NSString *)unityMessage
  99. message:(NSString *)message
  100. requestId:(int)requestId
  101. {
  102. const char *cString = [message UTF8String];
  103. UnitySendMessage(FB_OBJECT_NAME, [unityMessage cStringUsingEncoding:NSASCIIStringEncoding], FBUnityMakeStringCopy(cString));
  104. }
  105. + (NSString *)stringFromCString:(const char *)string {
  106. if (string && string[0] != 0) {
  107. return [NSString stringWithUTF8String:string];
  108. }
  109. return nil;
  110. }
  111. + (NSDictionary *)dictionaryFromKeys:(const char **)keys
  112. values:(const char **)vals
  113. length:(int)length
  114. {
  115. NSMutableDictionary *params = nil;
  116. if(length > 0 && keys && vals) {
  117. params = [NSMutableDictionary dictionaryWithCapacity:length];
  118. for(int i = 0; i < length; i++) {
  119. if (vals[i] && vals[i] != 0 && keys[i] && keys[i] != 0) {
  120. params[[NSString stringWithUTF8String:keys[i]]] = [NSString stringWithUTF8String:vals[i]];
  121. }
  122. }
  123. }
  124. return params;
  125. }
  126. + (FBSDKGameRequestFilter) gameRequestFilterFromString:(NSString *)filter {
  127. if (filter.length == 0 || [filter isEqualToString:@"none"]) {
  128. return FBSDKGameRequestFilterNone;
  129. } else if ([filter isEqualToString:@"app_users"]) {
  130. return FBSDKGameRequestFilterAppUsers;
  131. } else if ([filter isEqualToString:@"app_non_users"]) {
  132. return FBSDKGameRequestFilterAppNonUsers;
  133. }
  134. NSLog(@"Unexpected filter type: %@", filter);
  135. return FBSDKGameRequestFilterNone;
  136. }
  137. + (FBSDKGameRequestActionType) gameRequestActionTypeFromString:(NSString *)actionType {
  138. NSString *actionUpper = [actionType uppercaseString];
  139. if (actionUpper.length == 0 || [actionUpper isEqualToString:@"NONE"]) {
  140. return FBSDKGameRequestActionTypeNone;
  141. } else if ([actionUpper isEqualToString:@"SEND"]) {
  142. return FBSDKGameRequestActionTypeSend;
  143. } else if ([actionUpper isEqualToString:@"ASKFOR"]) {
  144. return FBSDKGameRequestActionTypeAskFor;
  145. } else if ([actionUpper isEqualToString:@"TURN"]) {
  146. return FBSDKGameRequestActionTypeTurn;
  147. }
  148. NSLog(@"Unexpected action type: %@", actionType);
  149. return FBSDKGameRequestActionTypeNone;
  150. }
  151. + (NSDictionary *)appLinkDataFromUrl:(NSURL *)url
  152. {
  153. NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
  154. if (url) {
  155. [dict setObject:url.absoluteString forKey:@"url"];
  156. FBSDKURL *parsedUrl = [FBSDKURL URLWithInboundURL:url sourceApplication:nil];
  157. if (parsedUrl) {
  158. if (parsedUrl.appLinkExtras) {
  159. [dict setObject:parsedUrl.appLinkExtras forKey:@"extras"];
  160. // TODO - Try to parse ref param out and pass back
  161. }
  162. if (parsedUrl.targetURL) {
  163. [dict setObject:parsedUrl.targetURL.absoluteString forKey:@"target_url"];
  164. }
  165. }
  166. } else {
  167. [dict setObject:@true forKey:@"did_complete"];
  168. }
  169. return dict;
  170. }
  171. + (NSDictionary *)getUserDataFromAccessToken:(FBSDKAccessToken *)token
  172. {
  173. if (token) {
  174. if (token.tokenString &&
  175. token.expirationDate &&
  176. token.userID &&
  177. token.permissions &&
  178. token.declinedPermissions) {
  179. NSInteger expiration = token.expirationDate.timeIntervalSince1970;
  180. NSInteger lastRefreshDate = token.refreshDate ? token.refreshDate.timeIntervalSince1970 : 0;
  181. return @{
  182. @"opened" : @"true",
  183. @"access_token" : token.tokenString,
  184. @"expiration_timestamp" : [@(expiration) stringValue],
  185. @"user_id" : token.userID,
  186. @"permissions" : [token.permissions allObjects],
  187. @"granted_permissions" : [token.permissions allObjects],
  188. @"declined_permissions" : [token.declinedPermissions allObjects],
  189. @"last_refresh" : [@(lastRefreshDate) stringValue],
  190. @"graph_domain" : @"facebook",
  191. };
  192. }
  193. }
  194. return nil;
  195. }
  196. @end