Snippets
Created by
Taras Leskiv
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | /**
* 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)
You can clone a snippet to your computer for local editing. Learn more.