Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[Closed] Social Networking Plugin (Facebook and Twitter) live!

Discussion in 'iOS and tvOS' started by prime31, Sep 19, 2010.

  1. prime31

    prime31

    Joined:
    Oct 9, 2008
    Posts:
    6,426
    @sonicviz, almost all of the Facebook methods are asynchronous so you will need to listen to the completion events to know when they are done. The SocialNetworkingEventListener.cs file has examples for how to listen to every available event in the plugin. The 2 you are interested in can be listened to like this:



    Code (csharp):
    1.     void Start()
    2.     {
    3.         // Start listening to the events
    4.         SocialNetworkingManager.facebookLogin += facebookLogin;
    5.         SocialNetworkingManager.facebookLoginFailed += facebookLoginFailed;
    6.     }
    7.    
    8.    
    9.     void OnDisable( string error )
    10.     {
    11.         // Stop listening to the events
    12.         SocialNetworkingManager.facebookLogin -= facebookLogin;
    13.         SocialNetworkingManager.facebookLoginFailed -= facebookLoginFailed;
    14.     }
    15.  
    16.  
    17.     void facebookLogin()
    18.     {
    19.         Debug.Log( "Successfully logged in to Facebook" );
    20.     }
    21.    
    22.    
    23.     void facebookLoginFailed( string error )
    24.     {
    25.         Debug.Log( "Facebook login failed: " + error );
    26.     }
     
  2. Paulo-Henrique025

    Paulo-Henrique025

    Joined:
    Dec 12, 2010
    Posts:
    230
    Prime, i'm a little new to C# and never saw "Events" or "Delegates" before, i figured out how to get the userName from Facebook but i do not know how to check if i'm logged in

    It works fine:
    FacebookBinding.getLoggedinUsersName();
    SocialNetworkingManager.facebookReceivedUsername += delegate(string result)
    {
    texto.text = "Welcome " + result;
    };

    but them comes:
    SocialNetworkingManager.facebookLogin += ?????

    and i do not know what to do
     
  3. prime31

    prime31

    Joined:
    Oct 9, 2008
    Posts:
    6,426
    @Paulo, the FacebookBinding.isLoggedIn method returns directly, so you can do something like this:

    Code (csharp):
    1. if( FacebookBinding.isLoggedIn() )
    2. {
    3.      Debug.Log( "user is logged into Facebook" );
    4. }
     
  4. Paulo-Henrique025

    Paulo-Henrique025

    Joined:
    Dec 12, 2010
    Posts:
    230
    I wanted to do something only one time after the login, like changing the player name to the Facebook userName
     
  5. prime31

    prime31

    Joined:
    Oct 9, 2008
    Posts:
    6,426
    @Paulo, in that case, you will want to listen to the facebookLogin event:


    Code (csharp):
    1. // In Start or OnEnable do this
    2. SocialNetworkingManager.facebookLogin += facebookLoginSuccessful;
    3.  
    4. public void facebookLoginSuccessful()
    5. {
    6.       Debug.Log( "logged in" );
    7. }
     
  6. sonicviz

    sonicviz

    Joined:
    May 19, 2009
    Posts:
    1,051
    Thanks Prime! Got it figured out now. Twitter just gave me xAuth approval too. A little more involved than getting FB approval, but aok in the end.
     
  7. gateway69

    gateway69

    Joined:
    Feb 18, 2010
    Posts:
    94
    this plugin support the new fb single sign-on functionality? http://developers.facebook.com/docs/guides/mobile/

    Also does it support the full graph api calls. Im curious how you build a web or html page on top of unity to allow for proper authorization, with the handshaking of OAuth, the fb uid (which I guess will be encrypted soon) etc
     
  8. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    you don't need to build any on top of unity.
    build the webpage that does this and bridge it and unity through javascript calls in an out
     
  9. prime31

    prime31

    Joined:
    Oct 9, 2008
    Posts:
    6,426
    @gateway69, the plugin exposes a method (graphRequest) that allows you to make just about any graph API request. All the auth happens through the standard Facebook Connect iOS web views which then uses OAuth to make all the requests.
     
  10. GamesByJerry

    GamesByJerry

    Joined:
    Oct 27, 2008
    Posts:
    71
    I'm wondering how you can get facebook posts to show up on the news feed / stream and not just on the users wall?

    Cheers!
     
  11. prime31

    prime31

    Joined:
    Oct 9, 2008
    Posts:
    6,426
    @Tagged, you would have to check the Graph API soca for that one. I am not familiar with everything that it exposes. There is a TON available.
     
  12. GamesByJerry

    GamesByJerry

    Joined:
    Oct 27, 2008
    Posts:
    71
    Thanks for the quick reply!
     
  13. GamesByJerry

    GamesByJerry

    Joined:
    Oct 27, 2008
    Posts:
    71
    Been trying to figure it out for a few hours with luck.. If anyone else stumbles on the answer would be very appreciative. Will continue to search otherwise and post if I manage to figure it out.

    Awesome plug-in btw :)
     
  14. Paulo-Henrique025

    Paulo-Henrique025

    Joined:
    Dec 12, 2010
    Posts:
    230
    Prime31, whats the correct way to build my project? Sometimes i get some errors in Xcode and have to rebuild all
     
  15. prime31

    prime31

    Joined:
    Oct 9, 2008
    Posts:
    6,426
    @Paulo, as long as you don't use Build and Run from Unity you should be fine. If you do accidentally do a Build and Run close Xcode, then do a Build followed by a Replace in Unity to rebuild the Xcode project.
     
  16. Paulo-Henrique025

    Paulo-Henrique025

    Joined:
    Dec 12, 2010
    Posts:
    230
    Prime, i'm still taming your script hehe, sorry for the amount of posts,

    I saw in some games that uses your plugin that when i left the game, open it again and try to login it auto-connect, without the need of email or pass, how can i do that?
     
  17. prime31

    prime31

    Joined:
    Oct 9, 2008
    Posts:
    6,426
    @Paulo, that will all happen automatically as long as a valid token is still around. You can also use the isLoggedIn method to check manually.
     
  18. Paulo-Henrique025

    Paulo-Henrique025

    Joined:
    Dec 12, 2010
    Posts:
    230
    Prime, i cant find this:

    // Shows a dialog allowing a user to edit the message with a link, a name for the link, a link to an image and a caption for the image
    public static void showPostMessageDialogWithMessageAndLinkAndLinkToImage( string message, string link, string linkName, string linkToImage, string caption )
     
  19. prime31

    prime31

    Joined:
    Oct 9, 2008
    Posts:
    6,426
    @Paulo, it looks like the docs weren't updated with the new name for that method: showPostMessageDialogWithOptions

    I just updated the docs to correct the misnamed method.
     
  20. GamesByJerry

    GamesByJerry

    Joined:
    Oct 27, 2008
    Posts:
    71
    Prime, just need a little guidance here (sorry if this is a really basic/obvious answer).

    I tracked down an issue with how facebook handles mobile cookies, something to do with logging in multiple ways which I assume is safari and the native facebook app. It spits out an Error 110 when trying to post to a wall because the cookie has been invalidated. Is there a way to detect an invalid cookie login prior to calling showPostMessageDialogWithOptions ? I'm using isLoggedIn already and that comes back as true even with an invalid cookie. Right now to avoid this I'm simply logging out and in each time, which would be annoying for the user!
     
  21. prime31

    prime31

    Joined:
    Oct 9, 2008
    Posts:
    6,426
    @Tagged, I am not aware of any way to detect the invalid cookie. The showPostMessageDialogWithOptions method pulls a web page off the Facebook server so I can't imagine there is much you can do with it. You could get the text input in a custom view and just use the postMessage or postMessageWithLink method which might work around it seeing as how they don't grab a web page from the Facebook servers.
     
  22. GamesByJerry

    GamesByJerry

    Joined:
    Oct 27, 2008
    Posts:
    71
    Prime,

    I've noticed when I run the game in debug mode I get an error message in the xcode console after pressing OK:

    facebookDialogFailed: The operation couldn't be completed. (facebookErrDomain error 190.)

    Which can also come up as error 110

    and if I press the cross button to close the dialog window

    facebookDialogDidntComplete

    Perhaps there is a way to grab these errors and then I could ask the user to log-in again?
     
    Last edited: Feb 11, 2011
  23. prime31

    prime31

    Joined:
    Oct 9, 2008
    Posts:
    6,426
    @Tagged, are you listening to the various completion and failed events? They should provide you with the information you need.
     
  24. Paulo-Henrique025

    Paulo-Henrique025

    Joined:
    Dec 12, 2010
    Posts:
    230
    @Tagged, stop using isLogged, listen to the events you need and take the necessary action
     
  25. GamesByJerry

    GamesByJerry

    Joined:
    Oct 27, 2008
    Posts:
    71
    Cheers! Will do that instead :)

    Thanks guys
     
  26. AetherRepublic

    AetherRepublic

    Guest

    Joined:
    Sep 21, 2009
    Posts:
    28
    I didn't read every post on here, so please forgive me if this has been requested for/asked.

    I don't see a nice clean method where I can have the username returned as a string. Is there a reason that FacebookBinding.getLoggedinUsersName(); couldn't just return the username in a string? I would modify it to include the above code and return a string, but then I'll have to remember to do it after patches.

    Thanks and I love the plugin,
    Kevin

    P.S. After I get this working in my app I plan to buy the GameCenter plugin. Good work bro.
     
  27. Paulo-Henrique025

    Paulo-Henrique025

    Joined:
    Dec 12, 2010
    Posts:
    230
    This is for sure a nice and clean method to get the user name

    public string userName;

    FacebookBinding.getLoggedinUsersName(); // Get the user name
    SocialNetworkingManager.facebookReceivedUsername += delegate(string result) // When the plugin receive the string DO SOMETHING
    {
    userName = result;
    print(result);
    };

    You can lissen to this in many objects, and then do something with it, otherwise you must receive the name, put it on a Global, send the string via ScriptAcess or SendMessage to the objects

    Take a better look, you'll see that its better, faster, nice and clean
     
  28. prime31

    prime31

    Joined:
    Oct 9, 2008
    Posts:
    6,426
    Paulos code is one way of doing it. The reason the method can't just return a string is because it needs so send a request off to the Facebook servers, wait for a response and parse the response to get the username. To learn a bit more about events, have a look at the events tutorial we made. A link to it is available in the docs.
     
  29. kenlem

    kenlem

    Joined:
    Oct 16, 2008
    Posts:
    1,630
    For some reason, my facebook code just started returning this message:

    facebookDialogFailed: The certificate for this server is invalid. You might be connecting to a server that is pretending to be “www.facebook.com” which could put your confidential information at risk.

    Anyone else seen this?
     
  30. DarkJedi

    DarkJedi

    Joined:
    Apr 15, 2009
    Posts:
    48
    Firstly, many thanks for these plugins! You say it would save us 6-10 hours over doing this ourselves but I know it would take me an awful lot longer than that! ;)

    I am encountering the same issue that DCP mentioned:
    I'm fine with working around this by storing a flag in userprefs so that the app knows that FB should be connected and can refresh the login, but the issue is that this refresh is not "silent" - FacebookBinding.login() flashes up an empty white dialog box for a split second before successfully authenticating (no username or pwd required when refreshing like this).

    Is there any way of avoiding this?

    Perhaps there could be an additional FacebookBinding.loginRefresh() method that we could leverage in such circumstances and that either omits the dialog box or masks it somehow?

    Many thanks! :)
    Dan
     
  31. prime31

    prime31

    Joined:
    Oct 9, 2008
    Posts:
    6,426
    @Dark, unfortunately the code that is run in that popup is coming from the Facebook server. I assume it is checking a cookie to see if the user is logged in. All Facebook gives us to check is an access token. If the date of the access token is expired, the plugin returns not logged in because it doesn't have access to the other checks Facebook is doing.
     
  32. DarkJedi

    DarkJedi

    Joined:
    Apr 15, 2009
    Posts:
    48
    Thanks Mike - I've looked into this a bit more and see that the OAuth token lasts about 2 hours before our apps will have to re-login again.

    I don't actually mind that, though it's a shame the Facebook SDK doesn't hold off drawing the UIView in FBDialog unless it actually needs to show / prompt something from the user!

    For now though, I have simply modified FacebookManager.login to request "offline_access" permissions in addition to publish_stream, which (assuming the user agrees!) returns a non-expiring token, and FacebookBinding.isLoggedIn() is allways true...

    DJ
     
  33. AetherRepublic

    AetherRepublic

    Guest

    Joined:
    Sep 21, 2009
    Posts:
    28
    Thank you for the tutorial. I had completely overlooked the events functionality. I can see already that it would have simplified my current project.
     
  34. AetherRepublic

    AetherRepublic

    Guest

    Joined:
    Sep 21, 2009
    Posts:
    28
    I really like the login and post screens for FaceBook. Any chance screens like this will be added for Twitter in the future? I think keeping the Twitter functionality you have now is important. Thus, I'm suggesting supporting both options.
     
  35. Barbur

    Barbur

    Joined:
    Oct 30, 2009
    Posts:
    160
    Hey all,

    Has someone got this error?

    It shows me up on the console when I try to post a message.

    EDIT: It worked now. It seems that I have to logout and login again when I relaunch the application.
     
    Last edited: Mar 8, 2011
  36. Barbur

    Barbur

    Joined:
    Oct 30, 2009
    Posts:
    160
    Another noob question. When you login into facebook is this login saved forever?
     
  37. DarkJedi

    DarkJedi

    Joined:
    Apr 15, 2009
    Posts:
    48
    The way I understand it, after the original user-facing login is successful Facebook returns an authorisation token, and the Prime31 plugin will save that and look after it for you.

    If you close your app and restart it FacebookBinding.isLoggedIn() will still return true. Nice.

    However, this authorisation token will time out after a couple of hours, after which FacebookBinding.isLoggedIn() will return false and you won't be able to post etc.

    At this point you will need to call FacebookBinding.Login() again to retrieve a new authorisation token, though fortunately the user will not be prompted to enter their password again - it will be a "silent" login and you'll get another couple of hours of posting goodness! ;)

    I hope that makes sense and helps!

    Dan
     
  38. JamesMobot

    JamesMobot

    Joined:
    Jul 8, 2010
    Posts:
    170
    Not sure if anyone else has come across this.
    Seems to have all the same linking errors as the game center plugin.

    There are more but here are a few... I kind of assume you dont want to see all of these lol

    Just checking if a new version is on the somewhat near horizon.

    Thanks.


    "__twitterGetHomeTimeline", referenced from:
    RegisterMonoModules() in RegisterMonoModules.o
    "__gameCenterRetrieveAchievementMetadata", referenced from:
    RegisterMonoModules() in RegisterMonoModules.o
    "__facebookLogin", referenced from:
    RegisterMonoModules() in RegisterMonoModules.o Etc, Etc.........
     
  39. FIllbiker

    FIllbiker

    Joined:
    Jun 9, 2010
    Posts:
    118
    I'm trying to make Twitter login, but with no succes. I registered my app on twitter, send the request for xAuth (still waiting for answer).
    In my TwitterBinding.init I paste my Consumer Key and Consumer secret, as strings).
    1) I have a problem with login... what should I write in TwitterBinding.login("", "") strings?? Twitter in this plugin doesn't have login screen like Facebook?
    2) Will my login work without xAuth, because they still didn't answer me. (I sent all my IDs and keys what I have in Application details on Twitter).

    3) Last question is for Facebook, If I use postMessageWithLinkAndLinkToImage() , what should I write as a path to image, if I will have one image in build root? (With Root I mean the same path as Icon.png or Default.png images in XCode project. I tried to write "/Icon.png" for test, but seems not working.

    Thank you

    Thank you
     
    Last edited: Mar 13, 2011
  40. prime31

    prime31

    Joined:
    Oct 9, 2008
    Posts:
    6,426
    @FillBiker, none of the Twitter calls will work until they grant you Xauth access. Twitter doesn't have a built in UI so you will need to prompt the user for their credentials at the appropriate time.

    For the Facebook ppostMessageWithLinkAndLinkToImage you need an actual URL that points to an image. If you want to post a a raw image file you will need to use the postImage method which takes a path to an image (rather than a URL like the other methods).
     
  41. FIllbiker

    FIllbiker

    Joined:
    Jun 9, 2010
    Posts:
    118
    Twitter: so if they will enable xAuth for my app, than it will work? But what should I write into login strings, now there is "REPLACE_WITH_YOUR_INFO"...? I don't understand, how useres will be able to login, if I set some strings there.
    Sorry for my newbe questions, but I'm totaly confused in all of these network things...:(
     
  42. prime31

    prime31

    Joined:
    Oct 9, 2008
    Posts:
    6,426
    @Fillbiker, here are the docs for the two methods you need to call to log a user in. It should be pretty clear what goes where by their params:


    // Initializes the Twitter plugin and sets up the required oAuth information
    public static void init( string consumerKey, string consumerSecret )

    // Logs in the user using xAuth
    public static void login( string username, string password )
     
  43. FIllbiker

    FIllbiker

    Joined:
    Jun 9, 2010
    Posts:
    118
    Still no answer from Twitter about xAuth. :(
    Anyway, I found a strange thing in my FB code.

    I want to have simple login + post on one button. (if I click the button and isLoggedIn() == false, login() function starts, after succesfull login automaticly post message with link to my web or app).
    Everything works, but only sometimes...sometimes nothing is posted to user's wall and I don't know why.

    my JS code:
    Code (csharp):
    1.  
    2. function Update() {
    3. ....
    4. if (Input.GetMouseButtonDown(0)  GUITexture.HitTest(Input.mousePosition)) {
    5. FacebookBinding.init(12543....blabla);
    6. if (FacebookBinding.isLoggedIn()) {
    7.  
    8. FacebookBinding.postMessageWithLink("Test", "http://blabla.com", "test");
    9.  
    10.  
    11. } else {
    12.  
    13. FacebookBinding.login();
    14. FacebookBinding.postMessageWithLink("Test", "http://blabla.com", "test");
    15.  
    16. }
    17.  
    18. }
    19.  
    this code works, but never on first click. When first click, I have to login, but no post is sent.:( On second click it works, because I'm already logged in.
    How to handle this, to be able to login + post "at once" on one click? And is it possible to logout immediately after postMessage...(); ??
     
    Last edited: Mar 15, 2011
  44. DarkJedi

    DarkJedi

    Joined:
    Apr 15, 2009
    Posts:
    48
    @Fillbiker

    The reason the FB login followed immediately by a postMessage fails is because the login hasn't completed by the time the code executes the message post.

    This is because FacebookBinding.login() processes asynchronously - a bit like doing a StartCoroutine().

    This is what the facebookLogin and facebookLoginFailed events in SocialNetworkingManager.cs are for - you add delegates in your code to monitor these events and act accordingly.

    Here's a really basic approach to get you going (not tested so may contain typos!):
    Code (csharp):
    1. private int _waitForLogin = 0;
    2.  
    3. void Start()
    4. {
    5.     SocialNetworkingManager.facebookLogin += delegate() { _waitForLogin = 1; };
    6. }
    7.  
    8. private IEnumerator PostToFaceBook()
    9. {
    10.   if (!FacebookBinding.isLoggedIn())
    11.   {
    12.     _waitForLogin = 0;
    13.     FacebookBinding.login();
    14.     while (_waitForLogin == 0) yield return new WaitForSeconds(0.5f);
    15.   }
    16.   FacebookBinding.postMessageWithLink("Test", "http://blabla.com", "test");
    17. }
    18.  
    When you want to post to facebook, simply StartCoroutine(PostToFacebook())

    Of course, you should add a delegate to catch facebookLoginFailed as well, and at the very least put a timeout in the wait loop so it doesn't loop for ever if something goes wrong, but the point here was to show how the delegates work and how you can wait for something to happen before continuing...

    Hopefully that helps! :cool:
     
  45. FIllbiker

    FIllbiker

    Joined:
    Jun 9, 2010
    Posts:
    118
    Thank you!! My knowlage of C# is 0. Can you translate this into JS please?
     
  46. DarkJedi

    DarkJedi

    Joined:
    Apr 15, 2009
    Posts:
    48
    Hehe - and my knowledge of JS is negligible, too!

    I believe it's much the same code as in C# - I think delegates work the same way, and Javascript is a bit nicer around coroutines (don't need to define functions as IEnumerator types etc)

    I've no idea whether this will work, but give it a try! ;)
    Code (csharp):
    1. var _waitForLogin = 0;
    2.  
    3. function Start()
    4. {
    5.     SocialNetworkingManager.facebookLogin += function() { _waitForLogin = 1; }
    6. }
    7.  
    8. function PostToFaceBook()
    9. {
    10.   if (!FacebookBinding.isLoggedIn())
    11.   {
    12.     _waitForLogin = 0;
    13.     FacebookBinding.login();
    14.     while (_waitForLogin == 0) yield WaitForSeconds(0.5f);
    15.   }
    16.   FacebookBinding.postMessageWithLink("Test", "http://blabla.com", "test");
    17. }
    18.  
     
    Last edited: Mar 15, 2011
  47. FIllbiker

    FIllbiker

    Joined:
    Jun 9, 2010
    Posts:
    118
    THANK YOU! It gives me more sence now. But compiler returns error on line in Start() function, "expected ; Insert a semicolon at the end"...I don't know why, it looks allright.. Any idea what is wrong here?
     
  48. DarkJedi

    DarkJedi

    Joined:
    Apr 15, 2009
    Posts:
    48
    OK, my bad for assuming delegate usage was similar to C#. I've modified the Start function's code in my post above, and I'm hoping that is now the correct way to subscribe to an event in Javascript.

    I'm shooting in the dark here, so hopefully someone who codes in JS can give us the correct syntax if this doesn't work! (And I'll modify the post again accordingly)
     
  49. FIllbiker

    FIllbiker

    Joined:
    Jun 9, 2010
    Posts:
    118
    Thx, I'll check it with C# even with JS, thank you again.
     
  50. samshosho

    samshosho

    Joined:
    Apr 12, 2010
    Posts:
    370
    Prime31,

    So far i bought your two plugins from you, the GameCenter and the StoreKit. I am planning on buying two more of your plugins, such as this one and the GameKit (wifi and bluetooth multiplayer).
    Do you have a package discount ?