123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- // Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
- //
- // You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
- // copy, modify, and distribute this software in source code or binary form for use
- // in connection with the web services and APIs provided by Facebook.
- //
- // As with any software that integrates with the Facebook platform, your use of
- // this software is subject to the Facebook Developer Principles and Policies
- // [http://developers.facebook.com/policy/]. This copyright notice shall be
- // included in all copies or substantial portions of the software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
- // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
- // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
- // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
- // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- #import "FBUnityUtility.h"
- #include <string>
- #import <Foundation/Foundation.h>
- #import <FBSDKCoreKit/FBSDKCoreKit.h>
- #import <FBSDKLoginKit/FBSDKLoginKit.h>
- #import <FBSDKShareKit/FBSDKShareKit.h>
- #import <Bolts/Bolts.h>
- const char* const FB_OBJECT_NAME = "UnityFacebookSDKPlugin";
- // Helper method to create C string copy
- static char* FBUnityMakeStringCopy (const char* string)
- {
- if (string == NULL)
- return NULL;
- char* res = (char*)malloc(strlen(string) + 1);
- strcpy(res, string);
- return res;
- }
- @implementation FBUnityUtility
- + (void) sendCancelToUnity:(NSString *)unityMessage
- requestId:(int)requestId
- {
- [self sendMessageToUnity:unityMessage
- userData:@{ @"cancelled" : @"true" }
- requestId:requestId];
- }
- + (void)sendErrorToUnity:(NSString *)unityMessage
- error:(NSError *)error
- requestId:(int)requestId
- {
- NSString *errorMessage =
- error.userInfo[FBSDKErrorLocalizedDescriptionKey] ?:
- error.userInfo[FBSDKErrorDeveloperMessageKey] ?:
- error.localizedDescription;
- [self sendErrorToUnity:unityMessage
- errorMessage:errorMessage
- requestId:requestId];
- }
- + (void)sendErrorToUnity:(NSString *)unityMessage
- errorMessage:(NSString *)errorMessage
- requestId:(int)requestId
- {
- [self sendMessageToUnity:unityMessage
- userData:@{ @"error" : errorMessage }
- requestId:requestId];
- }
- + (void)sendMessageToUnity:(NSString *)unityMessage
- userData:(NSDictionary *)userData
- requestId:(int)requestId
- {
- NSMutableDictionary *resultDictionary = [ @{ @"callback_id": [@(requestId) stringValue] } mutableCopy];
- [resultDictionary addEntriesFromDictionary:userData];
- if (![NSJSONSerialization isValidJSONObject:resultDictionary]) {
- [self sendErrorToUnity:unityMessage errorMessage:@"Result cannot be converted to json" requestId:requestId];
- return;
- }
- NSError *serializationError = nil;
- NSData *jsonData = [NSJSONSerialization dataWithJSONObject:resultDictionary options:0 error:&serializationError];
- if (serializationError) {
- [self sendErrorToUnity:unityMessage error:serializationError requestId:requestId];
- return;
- }
- NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
- if (!jsonString) {
- [self sendErrorToUnity:unityMessage errorMessage:@"Failed to generate response string" requestId:requestId];
- return;
- }
- const char *cString = [jsonString UTF8String];
- UnitySendMessage(FB_OBJECT_NAME, [unityMessage cStringUsingEncoding:NSASCIIStringEncoding], FBUnityMakeStringCopy(cString));
- }
- + (NSString *)stringFromCString:(const char *)string {
- if (string && string[0] != 0) {
- return [NSString stringWithUTF8String:string];
- }
- return nil;
- }
- + (NSDictionary *)dictionaryFromKeys:(const char **)keys
- values:(const char **)vals
- length:(int)length
- {
- NSMutableDictionary *params = nil;
- if(length > 0 && keys && vals) {
- params = [NSMutableDictionary dictionaryWithCapacity:length];
- for(int i = 0; i < length; i++) {
- if (vals[i] && vals[i] != 0 && keys[i] && keys[i] != 0) {
- params[[NSString stringWithUTF8String:keys[i]]] = [NSString stringWithUTF8String:vals[i]];
- }
- }
- }
- return params;
- }
- + (FBSDKGameRequestFilter) gameRequestFilterFromString:(NSString *)filter {
- if (filter.length == 0 || [filter isEqualToString:@"none"]) {
- return FBSDKGameRequestFilterNone;
- } else if ([filter isEqualToString:@"app_users"]) {
- return FBSDKGameRequestFilterAppUsers;
- } else if ([filter isEqualToString:@"app_non_users"]) {
- return FBSDKGameRequestFilterAppNonUsers;
- }
- NSLog(@"Unexpected filter type: %@", filter);
- return FBSDKGameRequestFilterNone;
- }
- + (FBSDKGameRequestActionType) gameRequestActionTypeFromString:(NSString *)actionType {
- NSString *actionUpper = [actionType uppercaseString];
- if (actionUpper.length == 0 || [actionUpper isEqualToString:@"NONE"]) {
- return FBSDKGameRequestActionTypeNone;
- } else if ([actionUpper isEqualToString:@"SEND"]) {
- return FBSDKGameRequestActionTypeSend;
- } else if ([actionUpper isEqualToString:@"ASKFOR"]) {
- return FBSDKGameRequestActionTypeAskFor;
- } else if ([actionUpper isEqualToString:@"TURN"]) {
- return FBSDKGameRequestActionTypeTurn;
- }
- NSLog(@"Unexpected action type: %@", actionType);
- return FBSDKGameRequestActionTypeNone;
- }
- + (NSDictionary *)appLinkDataFromUrl:(NSURL *)url
- {
- NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
- if (url) {
- [dict setObject:url.absoluteString forKey:@"url"];
- BFURL *parsedUrl = [BFURL URLWithInboundURL:url sourceApplication:nil];
- if (parsedUrl) {
- if (parsedUrl.appLinkExtras) {
- [dict setObject:parsedUrl.appLinkExtras forKey:@"extras"];
- // TODO - Try to parse ref param out and pass back
- }
- if (parsedUrl.targetURL) {
- [dict setObject:parsedUrl.targetURL.absoluteString forKey:@"target_url"];
- }
- }
- } else {
- [dict setObject:@true forKey:@"did_complete"];
- }
- return dict;
- }
- + (NSDictionary *)getUserDataFromAccessToken:(FBSDKAccessToken *)token
- {
- if (token) {
- if (token.tokenString &&
- token.expirationDate &&
- token.userID &&
- token.permissions &&
- token.declinedPermissions) {
- NSInteger expiration = token.expirationDate.timeIntervalSince1970;
- NSInteger lastRefreshDate = token.refreshDate ? token.refreshDate.timeIntervalSince1970 : 0;
- return @{
- @"opened" : @"true",
- @"access_token" : token.tokenString,
- @"expiration_timestamp" : [@(expiration) stringValue],
- @"user_id" : token.userID,
- @"permissions" : [token.permissions allObjects],
- @"granted_permissions" : [token.permissions allObjects],
- @"declined_permissions" : [token.declinedPermissions allObjects],
- @"last_refresh" : [@(lastRefreshDate) stringValue]
- };
- }
- }
- return nil;
- }
- @end
|