FBUnityInterface.mm 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  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. #include "FBUnityInterface.h"
  19. #import <FBSDKCoreKit/FBSDKCoreKit.h>
  20. #import <FBSDKLoginKit/FBSDKLoginKit.h>
  21. #import <FBSDKShareKit/FBSDKShareKit.h>
  22. #import <Foundation/NSJSONSerialization.h>
  23. #include "FBUnitySDKDelegate.h"
  24. #include "FBUnityUtility.h"
  25. #include "FBSDK+Internal.h"
  26. static FBUnityInterface *_instance = [FBUnityInterface sharedInstance];
  27. @interface FBUnityInterface()
  28. @property (nonatomic, copy) NSString *openURLString;
  29. @end
  30. @implementation FBUnityInterface
  31. #pragma mark Object Initialization
  32. + (FBUnityInterface *)sharedInstance
  33. {
  34. return _instance;
  35. }
  36. + (void)initialize {
  37. if(!_instance) {
  38. _instance = [[FBUnityInterface alloc] init];
  39. }
  40. }
  41. - (id)init
  42. {
  43. if(_instance != nil) {
  44. return _instance;
  45. }
  46. if ((self = [super init])) {
  47. _instance = self;
  48. self.shareDialogMode = ShareDialogMode::AUTOMATIC;
  49. UnityRegisterAppDelegateListener(self);
  50. }
  51. return self;
  52. }
  53. #pragma mark - App (Delegate) Lifecycle
  54. // didBecomeActive: and onOpenURL: are called by Unity's AppController
  55. // because we implement <AppDelegateListener> and registered via UnityRegisterAppDelegateListener(...) above.
  56. - (void)didFinishLaunching:(NSNotification *)notification
  57. {
  58. [[FBSDKApplicationDelegate sharedInstance] application:[UIApplication sharedApplication]
  59. didFinishLaunchingWithOptions:notification.userInfo];
  60. }
  61. - (void)didBecomeActive:(NSNotification *)notification
  62. {
  63. [FBSDKAppEvents activateApp];
  64. }
  65. - (void)onOpenURL:(NSNotification *)notification
  66. {
  67. NSURL *url = notification.userInfo[@"url"];
  68. BOOL isHandledByFBSDK = [[FBSDKApplicationDelegate sharedInstance] application:[UIApplication sharedApplication]
  69. openURL:url
  70. sourceApplication:notification.userInfo[@"sourceApplication"]
  71. annotation:notification.userInfo[@"annotation"]];
  72. if (!isHandledByFBSDK) {
  73. [FBUnityInterface sharedInstance].openURLString = [url absoluteString];
  74. }
  75. }
  76. #pragma mark - Implementation
  77. - (void)configureAppId:(const char *)appId
  78. frictionlessRequests:(bool)frictionlessRequests
  79. urlSuffix:(const char *)urlSuffix
  80. {
  81. self.useFrictionlessRequests = frictionlessRequests;
  82. if(appId) {
  83. [FBSDKSettings setAppID:[FBUnityUtility stringFromCString:appId]];
  84. }
  85. if(urlSuffix && strlen(urlSuffix) > 0) {
  86. [FBSDKSettings setAppURLSchemeSuffix:[FBUnityUtility stringFromCString:urlSuffix]];
  87. }
  88. NSDictionary *userData = [self getAccessTokenUserData] ?: @{};
  89. [FBUnityUtility sendMessageToUnity:FBUnityMessageName_OnInitComplete userData:userData requestId:0];
  90. }
  91. - (void)logInWithPublishPermissions:(int) requestId
  92. scope:(const char *)scope
  93. {
  94. [self startLogin:requestId scope:scope isPublishPermLogin:YES];
  95. }
  96. - (void)logInWithReadPermissions:(int) requestId
  97. scope:(const char *)scope
  98. {
  99. [self startLogin:requestId scope:scope isPublishPermLogin:NO];
  100. }
  101. - (void)startLogin:(int) requestId
  102. scope:(const char *)scope
  103. isPublishPermLogin:(BOOL)isPublishPermLogin
  104. {
  105. NSString *scopeStr = [FBUnityUtility stringFromCString:scope];
  106. NSArray *permissions = nil;
  107. if(scope && strlen(scope) > 0) {
  108. permissions = [scopeStr componentsSeparatedByString:@","];
  109. }
  110. void (^loginHandler)(FBSDKLoginManagerLoginResult *,NSError *) = ^(FBSDKLoginManagerLoginResult *result, NSError *error) {
  111. if (error) {
  112. [FBUnityUtility sendErrorToUnity:FBUnityMessageName_OnLoginComplete error:error requestId:requestId];
  113. return;
  114. } else if (result.isCancelled) {
  115. [FBUnityUtility sendCancelToUnity:FBUnityMessageName_OnLoginComplete requestId:requestId];
  116. return;
  117. }
  118. if ([self tryCompleteLoginWithRequestId:requestId]) {
  119. return;
  120. } else {
  121. [FBUnityUtility sendErrorToUnity:FBUnityMessageName_OnLoginComplete errorMessage:@"Unknown login error" requestId:requestId];
  122. }
  123. };
  124. FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
  125. if (isPublishPermLogin) {
  126. [login logInWithPublishPermissions:permissions
  127. fromViewController:nil
  128. handler:loginHandler];
  129. } else {
  130. [login logInWithReadPermissions:permissions
  131. fromViewController:nil
  132. handler:loginHandler];
  133. }
  134. }
  135. - (void)logOut
  136. {
  137. FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
  138. [login logOut];
  139. [FBUnityUtility sendMessageToUnity:FBUnityMessageName_OnLogoutComplete userData:@{} requestId:0];
  140. }
  141. - (void)appRequestWithRequestId:(int)requestId
  142. message:(const char *)message
  143. actionType:(const char *)actionType
  144. objectId:(const char *)objectId
  145. to:(const char **)to
  146. toLength:(int)toLength
  147. filters:(const char *)filters
  148. data:(const char *)data
  149. title:(const char *)title
  150. {
  151. FBSDKGameRequestContent *content = [[FBSDKGameRequestContent alloc] init];
  152. content.message = [FBUnityUtility stringFromCString:message];
  153. content.actionType = [FBUnityUtility gameRequestActionTypeFromString:[FBUnityUtility stringFromCString:actionType]];
  154. content.objectID = [FBUnityUtility stringFromCString:objectId];
  155. if(to && toLength) {
  156. NSMutableArray *toArray = [NSMutableArray array];
  157. for(int i = 0; i < toLength; i++) {
  158. [toArray addObject:[FBUnityUtility stringFromCString:to[i]]];
  159. }
  160. content.recipients = toArray;
  161. }
  162. content.filters = [FBUnityUtility gameRequestFilterFromString:[FBUnityUtility stringFromCString:filters]];
  163. content.data = [FBUnityUtility stringFromCString:data];
  164. content.title = [FBUnityUtility stringFromCString:title];
  165. FBUnitySDKDelegate *delegate = [FBUnitySDKDelegate instanceWithRequestID:requestId];
  166. NSError *error;
  167. FBSDKGameRequestDialog *dialog = [[FBSDKGameRequestDialog alloc] init];
  168. dialog.content = content;
  169. dialog.delegate = delegate;
  170. dialog.frictionlessRequestsEnabled = self.useFrictionlessRequests;
  171. if (![dialog validateWithError:&error]) {
  172. [FBUnityUtility sendErrorToUnity:FBUnityMessageName_OnAppRequestsComplete error:error requestId:requestId];
  173. }
  174. if (![dialog show]) {
  175. [FBUnityUtility sendErrorToUnity:FBUnityMessageName_OnAppRequestsComplete errorMessage:@"Failed to show request dialog" requestId:requestId];
  176. }
  177. }
  178. - (void)appInviteWithRequestId:(int)requestId
  179. appLinkUrl:(const char *)appLinkUrl
  180. previewImageUrl:(const char *)previewImageUrl
  181. {
  182. FBSDKAppInviteContent *content = [[FBSDKAppInviteContent alloc] init];
  183. content.appLinkURL = [NSURL URLWithString:[FBUnityUtility stringFromCString:appLinkUrl]];
  184. content.appInvitePreviewImageURL = [NSURL URLWithString:[FBUnityUtility stringFromCString:previewImageUrl]];
  185. FBUnitySDKDelegate *delegate = [FBUnitySDKDelegate instanceWithRequestID:requestId];
  186. [FBSDKAppInviteDialog showFromViewController:nil
  187. withContent:content
  188. delegate:delegate];
  189. }
  190. - (void)shareLinkWithRequestId:(int)requestId
  191. contentURL:(const char *)contentURL
  192. contentTitle:(const char *)contentTitle
  193. contentDescription:(const char *)contentDescription
  194. photoURL:(const char *)photoURL
  195. {
  196. FBSDKShareLinkContent *linkContent = [[FBSDKShareLinkContent alloc] init];
  197. NSString *contentUrlStr = [FBUnityUtility stringFromCString:contentURL];
  198. if (contentUrlStr) {
  199. linkContent.contentURL = [NSURL URLWithString:contentUrlStr];
  200. }
  201. NSString *contentTitleStr = [FBUnityUtility stringFromCString:contentTitle];
  202. if (contentTitleStr) {
  203. linkContent.contentTitle = contentTitleStr;
  204. }
  205. NSString *contentDescStr = [FBUnityUtility stringFromCString:contentDescription];
  206. if (contentDescStr) {
  207. linkContent.contentDescription = contentDescStr;
  208. }
  209. NSString *imageURL = [FBUnityUtility stringFromCString:photoURL];
  210. if (imageURL) {
  211. linkContent.imageURL = [NSURL URLWithString:imageURL];
  212. }
  213. [self shareContentWithRequestId:requestId
  214. shareContent:linkContent
  215. dialogMode:[self getDialogMode]];
  216. }
  217. - (void)shareFeedWithRequestId:(int)requestId
  218. toId:(const char *)toID
  219. link:(const char *)link
  220. linkName:(const char *)linkName
  221. linkCaption:(const char *)linkCaption
  222. linkDescription:(const char *)linkDescription
  223. picture:(const char *)picture
  224. mediaSource:(const char *)mediaSource
  225. {
  226. FBSDKShareLinkContent *linkContent = [[FBSDKShareLinkContent alloc] init];
  227. NSString *contentUrlStr = [FBUnityUtility stringFromCString:link];
  228. if (contentUrlStr) {
  229. linkContent.contentURL = [NSURL URLWithString:contentUrlStr];
  230. }
  231. NSString *contentTitleStr = [FBUnityUtility stringFromCString:linkName];
  232. if (contentTitleStr) {
  233. linkContent.contentTitle = contentTitleStr;
  234. }
  235. NSString *contentDescStr = [FBUnityUtility stringFromCString:linkDescription];
  236. if (contentDescStr) {
  237. linkContent.contentDescription = contentDescStr;
  238. }
  239. NSString *imageURL = [FBUnityUtility stringFromCString:picture];
  240. if (imageURL) {
  241. linkContent.imageURL = [NSURL URLWithString:imageURL];
  242. }
  243. NSMutableDictionary *feedParameters = [[NSMutableDictionary alloc] init];
  244. NSString *toStr = [FBUnityUtility stringFromCString:toID];
  245. if (toStr) {
  246. [feedParameters setObject:toStr forKey:@"to"];
  247. }
  248. NSString *captionStr = [FBUnityUtility stringFromCString:linkCaption];
  249. if (captionStr) {
  250. [feedParameters setObject:captionStr forKey:@"caption"];
  251. }
  252. NSString *sourceStr = [FBUnityUtility stringFromCString:mediaSource];
  253. if (sourceStr) {
  254. [feedParameters setObject:sourceStr forKey:@"source"];
  255. }
  256. linkContent.feedParameters = feedParameters;
  257. [self shareContentWithRequestId:requestId
  258. shareContent:linkContent
  259. dialogMode:FBSDKShareDialogModeFeedWeb];
  260. }
  261. - (void)shareContentWithRequestId:(int)requestId
  262. shareContent:(FBSDKShareLinkContent *)linkContent
  263. dialogMode:(FBSDKShareDialogMode)dialogMode
  264. {
  265. FBSDKShareDialog *dialog = [[FBSDKShareDialog alloc] init];
  266. dialog.shareContent = linkContent;
  267. dialog.mode = dialogMode;
  268. FBUnitySDKDelegate *delegate = [FBUnitySDKDelegate instanceWithRequestID:requestId];
  269. dialog.delegate = delegate;
  270. NSError *error;
  271. if (![dialog validateWithError:&error]) {
  272. [FBUnityUtility sendErrorToUnity:FBUnityMessageName_OnShareLinkComplete error:error requestId:requestId];
  273. }
  274. if (![dialog show]) {
  275. [FBUnityUtility sendErrorToUnity:FBUnityMessageName_OnShareLinkComplete errorMessage:@"Failed to show share dialog" requestId:requestId];
  276. }
  277. }
  278. - (FBSDKShareDialogMode)getDialogMode
  279. {
  280. switch (self.shareDialogMode) {
  281. case ShareDialogMode::AUTOMATIC:
  282. return FBSDKShareDialogModeAutomatic;
  283. case ShareDialogMode::NATIVE:
  284. return FBSDKShareDialogModeNative;
  285. case ShareDialogMode::WEB:
  286. return FBSDKShareDialogModeWeb;
  287. case ShareDialogMode::FEED:
  288. return FBSDKShareDialogModeFeedWeb;
  289. default:
  290. NSLog(@"Unexpected dialog mode: %@", [NSNumber numberWithInt:self.shareDialogMode]);
  291. return FBSDKShareDialogModeAutomatic;
  292. }
  293. }
  294. - (void)showJoinAppGroupDialogWithRequestId:(int) requestId
  295. groupId:(const char *) groupId
  296. {
  297. FBUnitySDKDelegate *delegate = [FBUnitySDKDelegate instanceWithRequestID:requestId];
  298. [FBSDKAppGroupJoinDialog showWithGroupID:[FBUnityUtility stringFromCString:groupId] delegate:delegate];
  299. }
  300. - (void)showCreateAppGroupDialogWithRequestId:(int) requestId
  301. groupName:(const char *) groupName
  302. groupDescription:(const char *) groupDescription
  303. groupPrivacy:(const char *) groupPrivacy
  304. {
  305. FBSDKAppGroupContent *content = [[FBSDKAppGroupContent alloc] init];
  306. content.name = [FBUnityUtility stringFromCString:groupName];
  307. content.groupDescription = [FBUnityUtility stringFromCString:groupDescription];
  308. FBSDKAppGroupPrivacy privacy;
  309. NSString *privacyStr = [FBUnityUtility stringFromCString:groupPrivacy];
  310. if ([privacyStr caseInsensitiveCompare:@"closed"] == NSOrderedSame) {
  311. privacy = FBSDKAppGroupPrivacyClosed;
  312. } else if ([privacyStr caseInsensitiveCompare:@"open"] == NSOrderedSame) {
  313. privacy = FBSDKAppGroupPrivacyOpen;
  314. } else {
  315. NSLog(@"Unexpced privacy type: %@", privacyStr);
  316. privacy = FBSDKAppGroupPrivacyClosed;
  317. }
  318. content.privacy = privacy;
  319. FBSDKAppGroupAddDialog *dialog = [[FBSDKAppGroupAddDialog alloc] init];
  320. dialog.content = content;
  321. dialog.delegate = [FBUnitySDKDelegate instanceWithRequestID:requestId];
  322. NSError *error;
  323. if (![dialog validateWithError:&error]) {
  324. [FBUnityUtility sendErrorToUnity:FBUnityMessageName_OnGroupCreateComplete error:error requestId:requestId];
  325. }
  326. if (![dialog show]) {
  327. [FBUnityUtility sendErrorToUnity:FBUnityMessageName_OnGroupCreateComplete errorMessage:@"Failed to show group create dialog" requestId:requestId];
  328. }
  329. }
  330. - (BOOL)tryCompleteLoginWithRequestId:(int) requestId
  331. {
  332. NSDictionary *userData = [self getAccessTokenUserData];
  333. if (userData) {
  334. [FBUnityUtility sendMessageToUnity:FBUnityMessageName_OnLoginComplete
  335. userData:userData
  336. requestId:requestId];
  337. return YES;
  338. } else {
  339. return NO;
  340. }
  341. }
  342. - (NSDictionary *)getAccessTokenUserData
  343. {
  344. FBSDKAccessToken *token = [FBSDKAccessToken currentAccessToken];
  345. if (token) {
  346. // Old v3 sdk tokens don't always contain a UserID. If the user ID is null
  347. // treat the token as bad and clear it. These values are all required
  348. // on c# side for initlizing a token.
  349. NSDictionary *userData = [FBUnityUtility getUserDataFromAccessToken:token];
  350. if (userData) {
  351. return userData;
  352. } else {
  353. // The token is missing a required value. Clear the token
  354. [[[FBSDKLoginManager alloc] init] logOut];
  355. }
  356. }
  357. return nil;
  358. }
  359. @end
  360. #pragma mark - Actual Unity C# interface (extern C)
  361. extern "C" {
  362. void IOSFBInit(const char *_appId, bool _frictionlessRequests, const char *_urlSuffix, const char *_userAgentSuffix)
  363. {
  364. // Set the user agent before calling init to ensure that calls made during
  365. // init use the user agent suffix.
  366. [FBSDKSettings setUserAgentSuffix:[FBUnityUtility stringFromCString:_userAgentSuffix]];
  367. [[FBUnityInterface sharedInstance] configureAppId:_appId
  368. frictionlessRequests:_frictionlessRequests
  369. urlSuffix:_urlSuffix];
  370. }
  371. void IOSFBLogInWithReadPermissions(int requestId,
  372. const char *scope)
  373. {
  374. [[FBUnityInterface sharedInstance] logInWithReadPermissions:requestId scope:scope];
  375. }
  376. void IOSFBLogInWithPublishPermissions(int requestId,
  377. const char *scope)
  378. {
  379. [[FBUnityInterface sharedInstance] logInWithPublishPermissions:requestId scope:scope];
  380. }
  381. void IOSFBLogOut()
  382. {
  383. [[FBUnityInterface sharedInstance] logOut];
  384. }
  385. void IOSFBSetShareDialogMode(int mode)
  386. {
  387. [FBUnityInterface sharedInstance].shareDialogMode = static_cast<ShareDialogMode>(mode);
  388. }
  389. void IOSFBAppRequest(int requestId,
  390. const char *message,
  391. const char *actionType,
  392. const char *objectId,
  393. const char **to,
  394. int toLength,
  395. const char *filters,
  396. const char **excludeIds, //not supported on mobile
  397. int excludeIdsLength, //not supported on mobile
  398. bool hasMaxRecipients, //not supported on mobile
  399. int maxRecipients, //not supported on mobile
  400. const char *data,
  401. const char *title)
  402. {
  403. [[FBUnityInterface sharedInstance] appRequestWithRequestId: requestId
  404. message: message
  405. actionType: actionType
  406. objectId: objectId
  407. to: to
  408. toLength: toLength
  409. filters: filters
  410. data: data
  411. title: title];
  412. }
  413. void IOSFBAppInvite(int requestId,
  414. const char *appLinkUrl,
  415. const char *previewImageUrl)
  416. {
  417. [[FBUnityInterface sharedInstance] appInviteWithRequestId:requestId
  418. appLinkUrl:appLinkUrl
  419. previewImageUrl:previewImageUrl];
  420. }
  421. void IOSFBGetAppLink(int requestId)
  422. {
  423. NSURL *url = [NSURL URLWithString:[FBUnityInterface sharedInstance].openURLString];
  424. [FBUnityUtility sendMessageToUnity:FBUnityMessageName_OnGetAppLinkComplete
  425. userData:[FBUnityUtility appLinkDataFromUrl:url]
  426. requestId:requestId];
  427. [FBUnityInterface sharedInstance].openURLString = nil;
  428. }
  429. void IOSFBShareLink(int requestId,
  430. const char *contentURL,
  431. const char *contentTitle,
  432. const char *contentDescription,
  433. const char *photoURL)
  434. {
  435. [[FBUnityInterface sharedInstance] shareLinkWithRequestId:requestId
  436. contentURL:contentURL
  437. contentTitle:contentTitle
  438. contentDescription:contentDescription
  439. photoURL:photoURL];
  440. }
  441. void IOSFBFeedShare(int requestId,
  442. const char *toId,
  443. const char *link,
  444. const char *linkName,
  445. const char *linkCaption,
  446. const char *linkDescription,
  447. const char *picture,
  448. const char *mediaSource)
  449. {
  450. [[FBUnityInterface sharedInstance] shareFeedWithRequestId:requestId
  451. toId:toId
  452. link:link
  453. linkName:linkName
  454. linkCaption:linkCaption
  455. linkDescription:linkDescription
  456. picture:picture
  457. mediaSource:mediaSource];
  458. }
  459. void IOSFBJoinGameGroup(int requestId, const char *groupId)
  460. {
  461. [[FBUnityInterface sharedInstance] showJoinAppGroupDialogWithRequestId:requestId groupId:groupId];
  462. }
  463. void IOSFBCreateGameGroup(int requestId, const char *groupName, const char *groupDescription, const char *groupPrivacy)
  464. {
  465. [[FBUnityInterface sharedInstance] showCreateAppGroupDialogWithRequestId:requestId groupName:groupName groupDescription:groupDescription groupPrivacy:groupPrivacy];
  466. }
  467. void IOSFBSettingsActivateApp(const char *appId)
  468. {
  469. [FBSDKAppEvents activateApp];
  470. }
  471. void IOSFBAppEventsLogEvent(const char *eventName,
  472. double valueToSum,
  473. int numParams,
  474. const char **paramKeys,
  475. const char **paramVals)
  476. {
  477. NSDictionary *params = [FBUnityUtility dictionaryFromKeys:paramKeys values:paramVals length:numParams];
  478. [FBSDKAppEvents logEvent:[FBUnityUtility stringFromCString:eventName] valueToSum:valueToSum parameters:params];
  479. }
  480. void IOSFBAppEventsLogPurchase(double amount,
  481. const char *currency,
  482. int numParams,
  483. const char **paramKeys,
  484. const char **paramVals)
  485. {
  486. NSDictionary *params = [FBUnityUtility dictionaryFromKeys:paramKeys values:paramVals length:numParams];
  487. [FBSDKAppEvents logPurchase:amount currency:[FBUnityUtility stringFromCString:currency] parameters:params];
  488. }
  489. void IOSFBAppEventsSetLimitEventUsage(BOOL limitEventUsage)
  490. {
  491. [FBSDKSettings setLimitEventAndDataUsage:limitEventUsage];
  492. }
  493. char* IOSFBSdkVersion()
  494. {
  495. const char* string = [[FBSDKSettings sdkVersion] UTF8String];
  496. char* res = (char*)malloc(strlen(string) + 1);
  497. strcpy(res, string);
  498. return res;
  499. }
  500. void IOSFBFetchDeferredAppLink(int requestId)
  501. {
  502. [FBSDKAppLinkUtility fetchDeferredAppLink:^(NSURL *url, NSError *error) {
  503. if (error) {
  504. [FBUnityUtility sendErrorToUnity:FBUnityMessageName_OnFetchDeferredAppLinkComplete error:error requestId:requestId];
  505. return;
  506. }
  507. [FBUnityUtility sendMessageToUnity:FBUnityMessageName_OnFetchDeferredAppLinkComplete
  508. userData:[FBUnityUtility appLinkDataFromUrl:url]
  509. requestId:requestId];
  510. }];
  511. }
  512. void IOSFBRefreshCurrentAccessToken(int requestId)
  513. {
  514. [FBSDKAccessToken refreshCurrentAccessToken:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
  515. if (error) {
  516. [FBUnityUtility sendErrorToUnity:FBUnityMessageName_OnRefreshCurrentAccessTokenComplete error:error requestId:requestId];
  517. return;
  518. }
  519. [FBUnityUtility sendMessageToUnity:FBUnityMessageName_OnRefreshCurrentAccessTokenComplete
  520. userData:[FBUnityUtility getUserDataFromAccessToken:[FBSDKAccessToken currentAccessToken]]
  521. requestId:requestId];
  522. }];
  523. }
  524. }