Snippets

GetSocial Facebook Unity Invite Plugin for Facebook Unity SDK version 7.+

Created by Taras Leskiv
/**
 *     Copyright 2015-2016 GetSocial B.V.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
using System.Collections.Generic;
using System;
using GetSocialSdk.Core;
using UnityEngine;
using Facebook.Unity;

public class FacebookInvitePlugin : IInvitePlugin
{
    #region IInvitePlugin implementation

    public bool IsAvailableForDevice()
    {
        return true;
    }

    public void InviteFriends(string subject, string text, string referralDataUrl, byte[] image, 
                              Action<string, List<string>> completeCallback,
                              Action cancelCallback,
                              Action<Exception> errorCallback)
    {
        if(FB.IsLoggedIn)
        {
            SendInvite(referralDataUrl, completeCallback, cancelCallback, errorCallback);
        }
        else
        {
#if UNITY_ANDROID
            // NOTE: Due to a bug with the facebook plugin for Unity4x for the Android platform we need to:
            // 1) close the window
            // 2) log in to Facebook
            // 3) reopen the view
            // 4) send the invite

            // close the Smart Invites window
            GetSocial.Instance.CloseView(true);
#endif

            // Your required permissions
            var requiredPermissions = new [] 
            {
                "public_profile",
                "user_friends"
            };
            FB.LogInWithReadPermissions(requiredPermissions, result =>
            {
#if UNITY_ANDROID
                // reopen the Smart Invites window
                GetSocial.Instance.RestoreView();
#endif

                if(FB.IsLoggedIn)
                {
                    SendInvite(referralDataUrl, completeCallback, cancelCallback, errorCallback);
                }
                else
                {
                    // The auth failed or the user cancelled the login
                }
            });
        }
    }

    #endregion

    private void SendInvite(string referralDataUrl, 
                            Action<string, List<string>> completeCallback, 
                            Action cancelCallback, 
                            Action<Exception> errorCallback)
    {
        FB.Mobile.AppInvite(new Uri(referralDataUrl), null, 
            result =>
        {
            if(result.Cancelled)
            {
                cancelCallback();
                return;
            }
            if(!string.IsNullOrEmpty(result.Error))
            {
                var errorMsg = "Failed sending app invite: " + result.Error;
                Debug.LogError(errorMsg);
                errorCallback(new Exception(errorMsg));
                return;
            }

            completeCallback(null, null);
        });
    }
}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.