Gaming Services iOS SDK

Sau khi bạn tích hợp phương thức Đăng nhập bằng Facebook, tính năng Chia sẻ lên Facebook hoặc Facebook Gaming, hệ thống sẽ tự động ghi và thu thập một số Sự kiện trong ứng dụng cho Trình quản lý sự kiện, trừ khi bạn tắt tính năng Tự động ghi sự kiện trong ứng dụng. Tất cả những nhà phát triển ứng dụng dùng phương thức Đăng nhập bằng Facebook, tính năng Chia sẻ lên Facebook hoặc Facebook Gaming nên nắm rõ cách hoạt động của chức năng này. Để biết chi tiết về loại thông tin được thu thập và cách tắt tính năng Tự động ghi sự kiện trong ứng dụng, hãy xem bài viết Tự động ghi sự kiện trong ứng dụng.

Gaming Services is available as a new component in the official Facebook SDK for iOS. Using Facebook official SDKs is the recommended approach and makes it easy to access services enabled by Facebook Login for Gaming including the Player Finder and Sharing (for Gaming) experiences.

Prerequisites

  1. Your application needs to enroll in Gaming Services to access features in this document. Follow the instructions to enroll your application.
  2. Make sure you have the minimal version of Facebook iOS SDK or above. Download the latest version.
  3. Ensure your App settings are properly configured for app switch.

Implementation

Facebook Login for Gaming

Facebook Login for Gaming enables a new login method for games. It supports the same implementation and SDK methods as Facebook Login. If this is the first time you're integrating the Facebook SDK into your App, refer to the configuration guide before proceeding.

Instead of receiving public_profile, your application will receive gaming_profile as the default permission. Other requestable permissions include user_friends, email, and gaming_user_picture.


Gaming Graph Domain

After integrating with Facebook Login for Gaming, your application will receive an access token for use on Gaming graph domain (graph.fb.gg), instead of the Facebook graph domain (graph.facebook.com). Read more about gaming graph domain.

You can check if the current User has connected with Facebook Login For Gaming by verifying the Graph Domain to be gaming in the Access Token:

// (c) Facebook, Inc. and its affiliates. Confidential and proprietary.
...
#import <FBSDKCoreKit/FBSDKCoreKit.h>
...
if ([[FBSDKAccessToken currentAccessToken].graphDomain isEqualToString:@"gaming"]) {
  // current user has been migrated to Facebook Login for Gaming
}

Player Finder and Sharing for Gaming

Here's an example from our sample App, which shows how to invoke the Player Finder and Sharing dialogs through the iOS SDK. It also shows the response you can expect from each of them.

// (c) Facebook, Inc. and its affiliates. Confidential and proprietary.

#import "GamingServicesCellController.h"

#import <MobileCoreServices/MobileCoreServices.h>
#import <UIKit/UIKit.h>

#import <FBSDKCoreKit/FBSDKCoreKit.h>
#import <FBSDKGamingServicesKit/FBSDKGamingServicesKit.h>
#import <FBSDKShareKit/FBSDKShareKit.h>

#import "Console.h"

static NSString *const kCellIdentifer = @"gaming-services-cell";

@interface MediaPickerDelegate : NSObject <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
@property (nonatomic, copy) void (^callback)(id media);
@end

@implementation MediaPickerDelegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
  __weak typeof(self) weakSelf = self;
  [picker dismissViewControllerAnimated:true completion:^{
    weakSelf.callback(info[UIImagePickerControllerOriginalImage]
    ?: [info objectForKey:UIImagePickerControllerMediaURL]);
  }];
}

@end

static void UploadImage(UIImage *image)
{
  if (![image isKindOfClass:[UIImage class]]) {
    ConsoleError([NSError new], @"Bad Image passed to Image Upload");
    return;
  }

  FBSDKGamingImageUploaderConfiguration *config =
  [[FBSDKGamingImageUploaderConfiguration alloc]
   initWithImage:image
   caption:@"Cool Photo"
   shouldLaunchMediaDialog:YES];

  [FBSDKGamingImageUploader
   uploadImageWithConfiguration:config
   andCompletionHandler:^(BOOL success, NSError * _Nullable error) {
    if (!success || error) {
      ConsoleError(error, @"Failed to upload Image");
    } else {
      ConsoleSucceed(@"Image Upload Complete");
    }
  }];
}

static void UploadVideo(NSURL *videoURL)
{
  if (![videoURL isKindOfClass:[NSURL class]]) {
    ConsoleError([NSError new], @"Bad URL passed to Video Upload");
    return;
  }

  FBSDKGamingVideoUploaderConfiguration *config =
  [[FBSDKGamingVideoUploaderConfiguration alloc]
   initWithVideoURL:videoURL
   caption:@"Cool Video"];

  [FBSDKGamingVideoUploader
   uploadVideoWithConfiguration:config
   andCompletionHandler:^(BOOL success, NSError * _Nullable error) {
    if (!success || error) {
      ConsoleError(error, @"Failed to upload Video");
    } else {
      ConsoleSucceed(@"Video Upload Complete");
    }
  }];
}

static void LaunchMediaPicker(NSString *type)
{
  static MediaPickerDelegate *mediaDelegate;
  if (mediaDelegate == nil) {
    mediaDelegate = [MediaPickerDelegate new];
  }

  UIImagePickerController *const picker = [UIImagePickerController new];
  picker.delegate = mediaDelegate;
  picker.modalPresentationStyle = UIModalPresentationCurrentContext;

  if ([type isEqualToString:@"VIDEO"]) {
    picker.mediaTypes = @[(NSString*)kUTTypeMovie, (NSString*)kUTTypeAVIMovie, (NSString*)kUTTypeVideo, (NSString*)kUTTypeMPEG4];
    picker.videoQuality = UIImagePickerControllerQualityTypeHigh;
    mediaDelegate.callback = ^(id media) {
      UploadVideo(media);
    };
  } else {
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    mediaDelegate.callback = ^(id media) {
      UploadImage(media);
    };
  }

  [[UIApplication sharedApplication].delegate.window.rootViewController
   presentViewController:picker
   animated:true
   completion:nil];
}

void GamingServicesRegisterCells(UITableView *tableView)
{
  [tableView
   registerClass:UITableViewCell.class
   forCellReuseIdentifier:kCellIdentifer];
}

UITableViewCell *GamingServicesConfiguredCell(NSIndexPath *indexPath, UITableView *tableView)
{
  UITableViewCell *const cell =
  [tableView
   dequeueReusableCellWithIdentifier:kCellIdentifer
   forIndexPath:indexPath];

  switch ((GamingServicesCellRow) indexPath.row) {
    case GamingServicesCellRowFriendFinder:
      cell.textLabel.text = @"Launch Player Finder";
      cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
      break;

    case GamingServicesCellRowUploadPhoto:
      cell.textLabel.text = @"Upload Photo";
      cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
      break;

    case GamingServicesCellRowUploadVideo:
      cell.textLabel.text = @"Upload Video";
      cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
      break;
  }

  return cell;
}

void GamingServicesDidSelectCell(GamingServicesCellRow row)
{
  switch (row) {
    case GamingServicesCellRowFriendFinder:
      [FBSDKFriendFinderDialog
       launchFriendFinderDialogWithCompletionHandler:^(BOOL success, NSError * _Nullable error) {
        if (!success || error) {
          ConsoleError(error, @"Failed to launch Player Finder");
        } else {
          ConsoleSucceed(@"Friend Finding Complete");
        }
      }];
      break;

    case GamingServicesCellRowUploadPhoto:
      LaunchMediaPicker(@"IMAGE");
      break;

    case GamingServicesCellRowUploadVideo:
      LaunchMediaPicker(@"VIDEO");
      break;
  }
}