FBUnityUtility.mm 7.1 KB

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