2020-03-20 Unity Facebook登录,分享,邀请接入

海外项目需要facebook登录等功能,国内有一些集成sdk特别简单好用,但是游戏项目需要邀请功能,所以只能集成原生SDK了,在此记录一下。

ShareSDK http://www.mob.com/mobService/sharesdk集成sdk,包挺小的,服务也很好,客服随时都能联系上,但是facebook打不开好友列表,很郁闷,只能接原生的了

Facebook 开发者后台地址 https://developers.facebook.com/apps/ 需在此注册应用

Facebook原生sdk下载地址 https://developers.facebook.com/docs/unity/downloads/ sdk需接入工程

第一步,注册应用

1.注册facebook账户,注册开发者,创建应用进入下图页面(facebook老是被封,怎么解决...)

image

2.点击基本填写应用基本信息

image

3.翻到页面最下面,点击添加平台

image

4.以Android为例,添加完平台,包名类名秘钥都都在项目中自动生成

image

获取密钥方式在此:https://www.jianshu.com/p/9557a469bcf2

第二步,导入SDK

1.下载sdk导入工程

image

2.点击设置

image

填写项目名称,编号

将包名,类名,秘钥填写到后台到此sdk接入完成,剩下的就是API的应用

image

下面上代码

public delegate void OnFBLoginFaild(bool isCancel, string errorInfo);

    public delegate void OnFBShareLinkSucced(string postId);
    public delegate void OnFBShareLinkFaild(bool isCancel, string errorInfo);

    public delegate void OnGotFBFriendInGame(string resultJsonStr);

    public delegate void OnGotFBMyInfo(string resultJsonStr);
    public delegate void OnGetFBInfoFaild();

    internal static void GetMyInfo(Action<string> p, object v)
    {
        throw new NotImplementedException();
    }

    public delegate void OnFBInvitedSucceed(string resultJsonStr);

    private static string appLinkUrl;

    /// <summary>
    /// 初始化
    /// </summary>
    public static void Init(Action action)
    {
        if (!FB.IsInitialized)
        {
            FB.Init(() =>
            {

                if (FB.IsInitialized)
                {
                    // Signal an app activation App Event
                    FB.ActivateApp();
                    // Continue with Facebook SDK
                    // ...
                    FBGetAPPLinkUrl();

                    if (action != null)
                    {
                        action();
                    }
                }
                else
                {
                    Debug.Log("Failed to Initialize the Facebook SDK");
                }
            }, OnHideUnity);
        }
        else
        {
            FB.ActivateApp();

            if (action != null)
            {
                action();
            }
        }
    }

    private static void OnHideUnity(bool isGameShown)
    {
        if (!isGameShown)
        {
            // Pause the game - we will need to hide
            Time.timeScale = 0;
        }
        else
        {
            // Resume the game - we're getting focus again
            Time.timeScale = 1;
        }
    }

    /// <summary>
    /// 登录
    /// </summary>
    public static void LoginResult(Action action)
    {
        List<string> perms = new List<string>() { "public_profile", "email", "user_friends" };
        FB.LogInWithReadPermissions(perms, (result) =>
        {
            if (FB.IsLoggedIn)
            {
                // AccessToken class will have session details
                var aToken = Facebook.Unity.AccessToken.CurrentAccessToken;

                if (action != null)
                {
                    action();
                }

            }
            else
            {

                Debug.Log("User cancelled login");
            }

        });
    }

    public static void Share()
    {
       // UpdateManager.Ins.verData.data.iosId = "1459965214";

        if (PlayerManager.Ins.IsFaceBook)
        {
#if UNITY_ANDROID && !UNITY_EDITOR
           FB.ShareLink(new Uri("https://play.google.com/store/apps/details?id=" + UpdateManager.Ins.verData.data.googleId), callback: ShareCallback);
#elif UNITY_IOS && !UNITY_EDITOR
            FB.ShareLink(new Uri("https://apps.apple.com/us/app/face-meme-emoji-gif-maker/id" + UpdateManager.Ins.verData.data.iosId), callback: ShareCallback);
#endif
        }
        else
        {
            PlayerManager.Ins.FackBookInit(() =>
            {
                PlayerManager.Ins.Player.Inquire();
#if UNITY_ANDROID && !UNITY_EDITOR
           FB.ShareLink(new Uri("https://play.google.com/store/apps/details?id=" + UpdateManager.Ins.verData.data.googleId), callback: ShareCallback);
#elif UNITY_IOS && !UNITY_EDITOR
           FB.ShareLink(new Uri("https://apps.apple.com/us/app/face-meme-emoji-gif-maker/id" + UpdateManager.Ins.verData.data.iosId), callback: ShareCallback);
#endif
            });
        }
    }


    private static void ShareCallback(IShareResult result)
    {
        if (result.Cancelled || !String.IsNullOrEmpty(result.Error))
        {
            Debug.Log("ShareLink Error: " + result.Error);
        }
        else if (!String.IsNullOrEmpty(result.PostId))
        {
            // Print post identifier of the shared content
            Debug.Log(result.PostId);
        }
        else
        {
            // Share succeeded without postID
            Debug.Log("ShareLink success!");
        }
    }

    /// <summary>   
    /// 邀请
    /// </summary>
    public static void Invite()
    {
        //string message, IEnumerable<string> to = null, IEnumerable<object> filters = null, IEnumerable<string> excludeIds = null, int? maxRecipients = default(int?), string data = "", string title = "", FacebookDelegate<IAppRequestResult> callback = null
        // FB.AppRequest(APP.facebookID,null/*to*/,null/**/,null);

        //FB.AppRequest("Come play this great game!", null, null, null, null, null, null, 
        //        delegate (IAppRequestResult result) {
        //            Debug.Log(result.RawResult); Debug.Log(result.RawResult);
        //});


    }

    /// <summary>
    /// 获取自己的信息
    /// </summary>
    /// <param name="onGotFBMyInfo"></param>
    public static void GetMyInfo(OnGotFBMyInfo onGotFBMyInfo = null, OnGetFBInfoFaild onGetFaild = null)
    {
        //Logger.LogUI("GetMyInfo");
        //没有授权
        if (FB.IsLoggedIn == false)
        {
            if (onGetFaild != null)
            {
                onGetFaild();
            }
            return;
        }

        //Logger.LogUI("API");
        FB.API("me?fields=id,name,picture", HttpMethod.GET, (result) =>
        {
            //Logger.LogUI(result.RawResult);
            if (onGotFBMyInfo != null)
            {
                onGotFBMyInfo(result.RawResult);
            }
        });
    }

    /// <summary>
    ///  获取APPLink, 获取失败,TODO
    /// </summary>
    public static void FBGetAPPLinkUrl()
    {
        FB.GetAppLink((result) =>
        {
            Debug.Log(result.RawResult);
            Debug.Log("Ref: " + result.Ref);
            Debug.Log("TargetUrl: " + result.TargetUrl);
            Debug.Log("Url: " + result.Url);
            appLinkUrl = result.Url;
            //Logger.LogUI(appLinkUrl);
        });
    }

DeepLink实现,基于ShareSdk实现

深度链接,我们分享邀请生成的链接,要实现的目的是用户点击链接,如果已经安装应用,拉起应用,并携带参数,如果没有安装跳转第三方,去下载应用

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 级别:★☆☆☆☆标签:「iOS 接入 Google、Facebook 登录」「Firebase Google」「F...
    QiShare阅读 12,612评论 6 11
  • 由于近期工作需要自己抽时间搞了一下第三方分享,这里使用的是shareSDK的第三方,在使用的过程中有一些心得和体会...
    灿烂先森阅读 13,882评论 29 69
  • 级别:★☆☆☆☆标签:「iOS 接入 Google、Facebook 登录」「iOS Google 登录」「iOS...
    QiShare阅读 8,838评论 1 4
  • 这个季节来的是如此的陌生、匆忙 还来不及欣赏夏季的百花齐放 就已到了果实累累的季节 望着夜空中玄月渐渐丰满 听着秋...
    海海_b7f9阅读 1,234评论 0 1
  • 简书钻和简书贝,我一直没搞懂它们的价值,有看到简书钻下面约等于人民币127.46,内心还是满开心的,可不知道如何花...
    禧珍阅读 4,010评论 1 4

友情链接更多精彩内容