Facebook SDK for Unity - Plus Windows Phone 8

1455470--78969--$facebook-sdk.png
Updated*

Hey folks,

Facebook Plugin for Unity with Windows Phone 8 support version 2.0. Yes, it’s still Free! Now your login is done through the Facebook App and the login is valid for more then 30 days! If you don’t have the app it asks if you want to download it.

I’ve provided the Source code for the Windows Phone 8 Login DLLs, both fake and real, in case you want to make changes.

Download the Unity Package here: https://bitbucket.org/chillsters/facebook-unity-sdk-with-wp8/src

NOTE: Use with caution! I have tested it but your game could crash.
The plugin is the same Facebook SDK plugin developed by Facebook but extended with Windows Phone 8 login capabilities. All the functions and method calls are the same only difference is the addition of the extra files.

So use the same function calls as you would on iOS or Android. But you still need to do some things to enable login on Windows Phone 8.

The plugin is made from the latest plugin available (v 4.3.6, build: 131121.1756) dated Dec 02, 2013.

1. Import Build Visual Studio Project in Unity.

Build your Unity project. Just like be sure to follow that steps setting up the Facebook App values; i.e App ID and App Name.

2.1 Edit your project App Manifest.

Configure your app’s manifest file (WMAppManifest.xml) to register a custom uri scheme that is of the form msft-{ProductID}, where {ProductID} is your app’s product ID (without dashes). Add the text after . It should look like this:

<Extensions>
            <Protocol Name="msft-f3bde1f120174f3c8ad9ad53098243e2" NavUriFragment="encodedLaunchUri=%s" TaskID="_default" />
</Extensions>

Important note: If you have not yet submitted your app to the Windows Phone Store, be aware that the product ID used during development is a randomly assigned placeholder. After your app has been certified, it has a different product ID. In this case, submit your app first to get a product ID assigned, but select the Manual publish option to ensure that you don’t release an app that is not set up correctly on the Facebook Developers site. Once you have your final product ID, make sure you update your WMAppManifest.xml to Use the final product ID rather than the randomly assigned placeholder.

Add ActivationPolicy=“Resume” to your Default Task. It should look like this:

 <Tasks>
      <DefaultTask Name="_default" NavigationPage="MainPage.xaml" ActivationPolicy="Resume"  />
</Tasks>

Note: If you do not add this property your game will restart everytime you try on login.

2.3 Update your app on the Facebook Developers site

You also need to enter some information about your app in the Facebook Developers site, for example, identify your application ID in the Windows Phone Store so Facebook can ensure only your app gets access tokens granted by users to your Facebook app.

  • Go to http://developers.facebook.com and log in.
  • Click Apps in the top navigation bar.
  • Select your Facebook app in the left pane (or create a new one if you are starting from scratch).
  • Click edit settings.
  • Under select how your application integrates with Facebook, find the section for Windows App and expand it.
  • Enter exactly the same product ID you entered in your WMAppManifest.xml file, the {ProductID} portion (without dashes) of your msft-{ProductID} custom URI scheme.

Important note: See earlier note on how the product ID is different during development and after submission. This is crucial if you have not yet been assigned a product ID.

Here’s an example of how the Facebook Developers site looks when set up.

1455470--98022--$image_5F00_thumb_5F00_27E606D0.png

That’s it!

Be careful when using the Login() Logout() functions though. Calling them close together can cause your app to crash.

Download the Unity Package here: https://bitbucket.org/chillsters/facebook-unity-sdk-with-wp8/src

It would be nice if Facebook was paying me to keep this plugin up to date. So I’ll try my best.

1455470--98024--$logo.png

You can support us by downloading Pigs Can’t Fly. http://www.chillsters.com/pigscantfly

(Update #1: Fixed Compile bug for Windows Phone 8, caused by FB.cs and FB.Screen.cs)

Finally Updated the SDK to use the Facebook App instead of a WebView. Meaning logins last 30 days plus and it’s less likely to crash (i think?).

Go here to read more: http://chillsters.com/facebook-sdk-for-unity-plus-windows-phone-8/

Sorry, can you upload your “LoginPage.xaml” and “LoginPage.xmal.cs” ?
I can’t find them in your link: https://bitbucket.org/chillsters/facebook-unity-sdk-with-wp8/src

Any chance of getting this working with the latest version of the Facebook Unity SDK?

at the end of #1:

I think Window Phone community should pay him to keep this plugin up to date :slight_smile: we must do it by ourselves :slight_smile:

There is a bug in WindowsPhoneFacebook.cs line 177:

if(_link.Length > 0) url += "&link=" +_toId;

Should be:

if(_link.Length > 0) url += "&link=" + _link;

I fixed another problem; when the user logs in and the second time he wants to use the app, you check the saved token in CheckAccessTokenValid() function. But usually it freezes. I solved this by running the code in another thread like this:

                System.Threading.ThreadPool.QueueUserWorkItem(delegate
                {
                    string url = "https://graph.facebook.com/" + UserID + "?access_token=" + AccessToken;
                    Uri uri = new Uri(url);
                    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
                    request.BeginGetResponse(new AsyncCallback(ReadCallback),
                    request);
                }, null);

Hi

Firstly thank you for the time and effort you have put in to prepare this plugin.

Im only interested in the Windows section and have run into an issue with the login. I can’t seem to ever login successfully the login view appears fine and it redirects back to the app but the value for FacebookWP8.IsLoggedIn is always false.

Thanks

If you guys didn’t know, the awesome chillsters have updated the plugin for SDK 5.1!

You can grab it from here http://chillsters.com/facebook-sdk-for-unity-plus-windows-phone-8/

Hello, Mahdi or Zee
My app crashes when resuming back from Facebook app to my game. I know it fails during the Login process but don’t have any idea how to debug.

I want to try Mahdi proposed fix but I don’t know how. I downloaded the source code for FacebookWinPhone8.dll, made the change and replaced the DLL directly in the Visual Studio project that Unity generated. But then my game crashes on start, I tried compiling for Release | ARM and Any Release | Any CPU

Thanks very much for having created this tool

1 Like

Seconded andrescavallin’s post.
We are receiving the following exception:

An unhandled exception of type ‘System.IO.FileLoadException’ occurred in System.Windows.ni.dll
Additional information: Could not load file or assembly ‘IFacebook, Version=0.0.0.0, Culture=neutral, PublicKeyToken=d9f0385cc69e16d8’ or one of its dependencies. The located assembly’s manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

Exactly same problem here! Is there another plugin you can use or what are my options with WP8 + FB?

Same problem for me as well - what version of Unity are you guys using? I’ve just updated to 4.5.2 and wondering if its related?

I’m going to downgrade and see if it still works

Works if you downgrade :slight_smile:

I downgraded from 4.5.2 to 4.5.1f3 and solved the unhandled exception problem.
To solve the first problem I posted, about crashing when returning from Facebook App, I followed Mahdi’s two suggestions.
I was able to make this changes by opening the project from Visual Studio: FILE → Add → Exiting Project…, navigate to Unity project folder and select Assembly-CSharp.csproj file and looking for the code…but I think It also might be changed directly on Unity/Mono Develop by opening Assets\Facebook\Scripts\WindowsPhoneFacebook.cs
Sadly I had reported a Unity 4.5.1 bug for capturing screenshots using Texture2D.ReadPixels on my Nokia Lumia that may be solved in future versions and now I can’t upgrade to see if this was fixed on 4.5.2 . If I come with a solution I will let you know.

I’m doing a basic test for a simple post. As I know for a simple post is not needed to ask for approval, so you can use it as it is. I did it for iOS, and Android with success. The same code for Windows Phone, looks does not authenticate to me. Can anyone provide me some lines of code of the authentication and login part you use with success for WP8. Thank you very much.

Hello Latas, I can provide a sample code but I use the exact same code for all platforms including iOS, Android, Web and WP8. The difference is within the SDK, not on how you use it.
Please double check steps 2.1 and 2.3 at the beginning of this post, since once you have it running for iOS and Android the same code will work for WP8 assuming you did those steps and also assuming you are using this SDK instead of the Facebook’s one.
If you need help finding the specific reason for why the POST is failing, check out this post:

I’ve started getting this since fixing the compile issue. the moment I click on my Login with Facebook button, it fails with an SEHException, which then makes the whole thing fail. It doesn’t crash out completely, but if you’re running in debug, it gives you a call stack. I have Mahdi’s changes in the DLL’s and recompiled them, but I get the same problem. Here’s the call stack after the exception:

Exception: External component has thrown an exception.
Type: System.Runtime.InteropServices.SEHException
Module: UnityEngine
InnerException: <No Data>
AdditionalInfo:<No Data>
   at UnityEngine.Internal.$Calli.Invoke90(Int32 arg0, Int64 arg1, IntPtr method)
   at UnityEngine.MonoBehaviour.StartCoroutine_Auto(IEnumerator routine)
   at UnityEngine.MonoBehaviour.StartCoroutine(IEnumerator routine)
   at Facebook.WindowsPhoneFacebook.Login(String scope, FacebookDelegate callback)
   at LoginManager.FacebookLogin()
   at tk2dUIItem.Release()
   at tk2dUIManager.CheckInputs()
   at tk2dUIManager.Update()
   at tk2dUIManager.$Invoke30(Int64 instance, Int64* args)
   at UnityEngine.Internal.$MethodUtility.InvokeMethod(Int64 instance, Int64* args, IntPtr method)

Any ideas?

Hello. I add canvas url https://www.google.com/, and same in advanced Valid OAuth redirect URIs . I get an error on windows phone after i trying to login -“Invalid redirect URI, please refer to Facebook login documentation”. Can someone help me?