Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Open facebook app instead safari / iOS - C#

Discussion in 'iOS and tvOS' started by cmsleal, Jul 13, 2012.

  1. cmsleal

    cmsleal

    Joined:
    Jul 10, 2012
    Posts:
    14
    I'm trying to call the oficial facebook App from my game instead open the safari browser. When the user click on my facebook button, I want to redirect him to my facebook page using the facebook oficial app.

    I could using this:

    Code (csharp):
    1. Application.OpenURL ("fb://page/xxxxx");
    2.  
    The trick is to use fb:// instead http://www.facebook.com... So far, so good.

    But what if the user does not have the facebook application installed?

    So, I tried this one:

    Code (csharp):
    1.  void OpenFacebookPage ()
    2.  {
    3.         WWW www = new WWW("fb://page/xxxxx");
    4.         StartCoroutine(WaitForRequest(www));
    5.  }
    6.  
    7.     IEnumerator WaitForRequest(WWW www)
    8.     {
    9.         yield return www;
    10.  
    11.         // check for errors
    12.         if (www.error == null)
    13.         {
    14.             Debug.Log("Sucess!: " + www.text);
    15.         }
    16.  else
    17.  {
    18.             Debug.Log("WWW Error: "+ www.error + " Opening Safari...");
    19.             //error. Open normal address
    20.             Application.OpenURL ("http://www.facebook.com/xxxxxx");
    21.  
    22.         }    
    23.     }
    But it did not work as I expected - always return error. I guess it did not work because WWW not support the facebook protocol. In the Unity scripting reference:

    Note: http://, https:// and file:// protocols are supported on iPhone. ftp:// protocol support is limited to anonymous downloads only. Other protocols are not supported.

    So, I'm stucked here.. Anyone knows how to solve this?

    Thank you!
     
  2. J_P_

    J_P_

    Joined:
    Jan 9, 2010
    Posts:
    1,026
    I happen to be implementing this soon -- will let you know if I get it working.
     
  3. cmsleal

    cmsleal

    Joined:
    Jul 10, 2012
    Posts:
    14
    Thanks. I'll be waiting...
     
  4. J_P_

    J_P_

    Joined:
    Jan 9, 2010
    Posts:
    1,026
    Application.OpenURL("fb://page/315797608481737");

    This worked like a charm for me. One thing to note -- while my page's address is https://www.facebook.com/CakeDayApp, fb://page/CakeDayApp didn't work when I tested the links in email. On fb, go to your page, click edit page>update info and in the url bar grab the digits after id= and use those for the fb:// link
     
  5. cmsleal

    cmsleal

    Joined:
    Jul 10, 2012
    Posts:
    14
    Yes, it works. But what happen if you delete your facebook app from your device?? Will it open safari or do nothing?
     
  6. J_P_

    J_P_

    Joined:
    Jan 9, 2010
    Posts:
    1,026
    Probably do nothing. You can try waiting a second or two and do a normal OpenUrl to safari as a fallback option.

    edit: sorry, didn't read your OP fully. Yeah, I doubt WWW would be of any use for opening a custom URL scheme because it's not doing any network requests. It's just telling the OS to open an app with that URL id. As far as I know, the OS doesn't send back a message if it fails so I'd just wait a second or two and open the url in safari.
     
    Last edited: Jul 13, 2012
  7. cmsleal

    cmsleal

    Joined:
    Jul 10, 2012
    Posts:
    14
    yes, that is what I'm trying to do. But I need to test if the first link have worked, and the only way I found is using WWW form. But WWW always return error because it does not support the facebook protocol. (fb://).
     
  8. J_P_

    J_P_

    Joined:
    Jan 9, 2010
    Posts:
    1,026
    Because fb:// isn't a proper protocol -- it's an entirely different mechanic that happens to follow the same syntax.

    edit: testing my suggestion now
     
  9. cmsleal

    cmsleal

    Joined:
    Jul 10, 2012
    Posts:
    14
    hum, I think it will open the facebook app, then when the game did become active again, it will open the safari... You will have a double call for those who have the facebook app.
     
  10. cmsleal

    cmsleal

    Joined:
    Jul 10, 2012
    Posts:
    14
    I got an idea... I'll test.
     
  11. J_P_

    J_P_

    Joined:
    Jan 9, 2010
    Posts:
    1,026
    Blah, my 3GS didn't want to be recognized by Xcode (didn't want to delete fb from 4S/iPad3) -_-

    Code (csharp):
    1.     IEnumerator OpenFacebookPage(){
    2.         Application.OpenURL("fb://page/315797608481737");
    3.         yield return new WaitForSeconds(1);
    4.         if(leftApp){
    5.             leftApp = false;
    6.         }
    7.         else{
    8.             Application.OpenURL("https://www.facebook.com/CakeDayApp");
    9.         }
    10.     }
    11.    
    12.     bool leftApp = false;
    13.    
    14.     void OnApplicationPause(){
    15.         leftApp = true;
    16.     }
    This seems to work for me.

    edit: If anybody uses this code, you may want to make tweaks to handle instances where the user taps the button multiple times before the app is able to switch over
     
    Last edited: Jul 14, 2012
  12. cmsleal

    cmsleal

    Joined:
    Jul 10, 2012
    Posts:
    14
    Nice, thank you

    this also works ;)

    Code (csharp):
    1. void OpenFacebookPage ()
    2.     {
    3.         float startTime;
    4.         startTime = Time.timeSinceLevelLoad;
    5.        
    6.         //open the facebook app
    7.         Application.OpenURL(facebookApp);
    8.        
    9.         if (Time.timeSinceLevelLoad - startTime <= 1f)
    10.         {
    11.             //fail. Open safari.
    12.             Application.OpenURL(facebookAddress);
    13.         }
    14. }
     
    lifeisfunxyz likes this.
  13. cmsleal

    cmsleal

    Joined:
    Jul 10, 2012
    Posts:
    14
    same way to open the twitter app or safari. Problem solved.

    But your code is more elegant.
     
    Last edited: Jul 14, 2012
    lifeisfunxyz likes this.
  14. kromenak

    kromenak

    Joined:
    Feb 9, 2011
    Posts:
    249
    Do you happen to know if this code works on Android as well? It seems like it would; I'll report back if I hear nothing, because I'll have to get this working soon anyway!
     
  15. J_P_

    J_P_

    Joined:
    Jan 9, 2010
    Posts:
    1,026
    So, it seems like this is broken in Facebook's new native app. fb://page/315797608481737 doesn't work. fb://page/CakeDayApp doesn't work. Any ideas?

    edit: fb://profile/315797608481737 works.

    Also, I found a tutorial that mentions what seems to be a cleaner way (through native objC) to fallback to safari if they don't have the app, but I haven't tried it yet

    http://mobileorchard.com/opening-the-facebook-app-to-your-facebook-fan-page/
     
    Last edited: Sep 26, 2012
    Firemaw likes this.
  16. cmsleal

    cmsleal

    Joined:
    Jul 10, 2012
    Posts:
    14
    I'm downloading the new facebook update right now. I will check it out.
     
  17. J_P_

    J_P_

    Joined:
    Jan 9, 2010
    Posts:
    1,026
    Change "page" to "profile" ie fb://profile/315797608481737 and it should work
     
  18. cmsleal

    cmsleal

    Joined:
    Jul 10, 2012
    Posts:
    14
    I'll have to fix it in the next update because these link is in the code. Are you using an external server to get this link in your games and be able to change it anytime?
     
  19. J_P_

    J_P_

    Joined:
    Jan 9, 2010
    Posts:
    1,026
    Nah. I just haven't pushed out that update yet so it never went live. Working on this project in my free time
     
  20. skullthug

    skullthug

    Joined:
    Oct 16, 2011
    Posts:
    156
    So I'm trying out the solution JTown has posted on my Android project, but it causes Unity to crash if fb:// is called and Facebook isn't installed on the device. Same problem with trying to call twitter:// for Twitter. If the apps are installed though it works fine. Any thoughts?

    Android reports this crash:
    E/AndroidRuntime(30849): FATAL EXCEPTION: GLThread 2783
    E/AndroidRuntime(30849): android.content.ActivityNotFoundException: No Activity
    found to handle Intent { act=android.intent.action.VIEW dat=twitter://user?scree
    n_name=HandyArtTool }
     
    Last edited: Dec 11, 2012
  21. mboaj

    mboaj

    Joined:
    Jul 17, 2012
    Posts:
    2
    Is there any way to use the same protocol to post on the wall?

    For instance how this call would be expressed using fb:// I tried several options but none seems to work

    https://www.facebook.com/dialog/feed?
    app_id=458358780877780
    link=https://developers.facebook.com/docs/reference/dialogs/
    picture=http://fbrell.com/f8.jpg
    name=Facebook%20Dialogs
    caption=Reference%20Documentation
    description=Using%20Dialogs%20to%20interact%20with%20users.
    redirect_uri=https://mighty-lowlands-6381.herokuapp.com/
     
  22. aciang

    aciang

    Joined:
    May 29, 2012
    Posts:
    15
    same here..
    The app crashed if facebook and twitter is not installed on the device.

    Any workaround?
     
  23. Tapgames

    Tapgames

    Joined:
    Dec 1, 2009
    Posts:
    242
    Like to know this too! :)

    I tried this with uScript with the open browser node and filled in the "fb://profile/1234" the facebook app is opening to the correct page and when I run this on a phone or ipad without the facebook app nothing is happening no crash but also no Facebook via safari it's a start ;)
     
    Last edited: May 25, 2013
  24. buddingmonkey

    buddingmonkey

    Joined:
    Jul 1, 2013
    Posts:
    2
    Did anyone ever find a fix for this on Android?
     
  25. PStal

    PStal

    Joined:
    Dec 14, 2013
    Posts:
    5
    I run into the same issue. The problem is the Java exception (when running OpenUrl("fb://..." and there is no proper Activity to handle the intent) cannot be caught from Unity. The solution for this is to check if the Facebook app is already installed on the device - the elegant way to do this requires JNI.
    But if you would like to have simple and short solution for this, there's my code:
    Code (csharp):
    1.  
    2. private bool checkPackageAppIsPresent(string package)
    3. {
    4.        AndroidJavaClass up = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    5.        AndroidJavaObject ca = up.GetStatic<AndroidJavaObject>("currentActivity");
    6.        AndroidJavaObject packageManager = ca.Call<AndroidJavaObject>("getPackageManager");
    7.  
    8.       //take the list of all packages on the device
    9.        AndroidJavaObject appList = packageManager.Call<AndroidJavaObject>("getInstalledPackages",0);
    10.        int num = appList.Call<int>("size");
    11.        for(int i = 0; i < num; i++)
    12.        {
    13.             AndroidJavaObject appInfo = appList.Call<AndroidJavaObject>("get", i);
    14.             string packageNew = appInfo.Get<string>("packageName");
    15.             if(packageNew.CompareTo(package) == 0)
    16.             {
    17.                 return true;
    18.             }
    19.        }
    20.        return false;
    21. }
    22.  
    Finally in case of Facebook app package name we are looking for is called: "com.facebook.katana", so you'll check like this:

    Code (csharp):
    1.  
    2. if(checkPackageAppIsPresent("com.facebook.katana"))
    3. {
    4.       Application.OpenURL("fb://..."); //there is Facebook app installed so let's use it
    5. }
    6. else
    7. {
    8.       Application.OpenURL("http://..."); // no Facebook app - use built-in web browser
    9. }
    10.  
    this is required only for Android, iOS will not crash when trying open url like "fb://.." even if there is no Facebook app installed on the device. I hope it helped.
     
    Last edited: Dec 14, 2013
  26. DeveshPandey

    DeveshPandey

    Joined:
    Sep 30, 2012
    Posts:
    212
  27. Christop

    Christop

    Joined:
    Aug 21, 2012
    Posts:
    60
    I noticed today after my recent Facebook app update (version 26.0.0.22.16) that the
    fb://profile/..... doen not work on Android anymore, but the fb://page/.. works again.
    The fB://profile/... url opens up the Facebook app and throws an unavailable content message.

    In iOS (i still have iOS 7) fb://profile/.. still works on my iPad air.

    Anyone with similar experience?
     
  28. dakka

    dakka

    Joined:
    Jun 25, 2010
    Posts:
    113
    @Christop Yes just came across the same thing and found fb://profile/ not working for Android. Changed to fb://page/ and its all good.
     
  29. nawash

    nawash

    Joined:
    Jan 29, 2010
    Posts:
    164
    Hi all,
    Just FYI it seems that simple Application.OpenURL("https://www.facebook.com/<thepageid>")
    now works launching the Facebook application on iPad with the current latest version at the time I'm writting this.
     
  30. skullthug

    skullthug

    Joined:
    Oct 16, 2011
    Posts:
    156
    I'm currently having problems with it opening the Facebook app on iOS when feeding OpenURL the url https://www.facebook.com/ExamplePage... but instead of going to the page it just shows my Facebook timeline.

    This seems sorta retroactive too, as apps I already have on the store now are magically opening in the Facebook app instead of Safari [and unfortunately also just going to the timeline]. So it seems like some sort of bug with iOS or Facebook? It's very confusing.

    https://m.facebook.com/ExamplePage at least works, but bleh, nobody wants to bother with that on their phone if they have the app already installed.

    Using Unity 4.6.4f1 with iOS 8.1.3 and latest FB as of this date.
     
  31. nawash

    nawash

    Joined:
    Jan 29, 2010
    Posts:
    164
    Hi Skullthug,
    Is ExamplePage a page id or the plain text name of the page ?
    Try using the pageid instead.
     
    Andrey-Postelzhuk likes this.
  32. skullthug

    skullthug

    Joined:
    Oct 16, 2011
    Posts:
    156
    @nawash
    Ah! You're right. I think I mis-understood page ID as the plain text name when quickly reading this.
    Using the page ID # totally works, that's awesome!
    Thanks :D
     
    nawash likes this.
  33. adolfainsley8

    adolfainsley8

    Joined:
    Apr 22, 2016
    Posts:
    2
    ID as the plain text name when quickly reading this.
    Using the page ID # totally works, that's awesome!..???


    == http://www.crosswordchamp.com/ ==
     
    Last edited: Apr 29, 2016
  34. parth3333

    parth3333

    Joined:
    Oct 3, 2016
    Posts:
    5
    hello, i use unity 5.5.1, i wants to open my page link in the facebook app in ios and android , i use fb://page/xxx and fb://profile/xxx both also try https://www.facebook.com/xxx, but still facebook page open in browser, help me to solve it
     
  35. lifeisfunxyz

    lifeisfunxyz

    Joined:
    Aug 9, 2017
    Posts:
    8
    Code (csharp):
    1.  
    2. string your_page_id_number ="xxxxxxxx";
    3. string facebookApp = "fb://page/"+your_page_id_number;
    4. string facebookAddress= "https://facebook.com/"+your_page_id_number;
    5.  
    working with me on android
    good solution after search and tested many others.
     
    Last edited: Sep 29, 2017
  36. zero_null

    zero_null

    Joined:
    Mar 11, 2014
    Posts:
    159
    Yes it's working fine !
    Thanks
     
    lifeisfunxyz likes this.
  37. TobyYe

    TobyYe

    Joined:
    May 25, 2013
    Posts:
    24

    but seems not work on ios 11
     
  38. adam_mehman

    adam_mehman

    Joined:
    Dec 11, 2014
    Posts:
    102
    Yep, not working anymore.

    I haven't yet figured it out how to open a page correctly.
     
    Kornushin likes this.
  39. TGR-GBT

    TGR-GBT

    Joined:
    Aug 4, 2015
    Posts:
    1
    Hi!

    I'm try to achieve same action. Confirm app is available on phone, if not, open in browser.

    I don't understand why your condition is : Time.timeSinceLevelLoad - startTime <= 1f.
    testing this code, with or without app; I always got Time.timeSinceLevelLoad value = startTime value, so 0.
    How could it not be equal?
     
  40. cleverleal

    cleverleal

    Joined:
    May 29, 2018
    Posts:
    4
    Well, I don't remember, this post has years hehe. But I think when Application.OpenURL success to open the application goes to background, so the Time.timeSinceLevelLoad continue counting. This action takes more than 1 second.
     
  41. tponnampalam

    tponnampalam

    Joined:
    May 10, 2018
    Posts:
    2
    After some testing, I found out that the iOS and Android links are different now. For Android it is "fb://page/<YOUR ID>" and for iOS it is "fb://page/?id=<YOUR ID>".
     
    CharlesuC likes this.
  42. tponnampalam

    tponnampalam

    Joined:
    May 10, 2018
    Posts:
    2
    Also if you want to find the proper links for android and ios just follow these steps.
    1) Go to the facebook page
    2) Open up developer tool of your browser
    3) Search in the HTML code for "al:ios:url" for the ios link and "al:android:url" for the android link
     
    QuangN likes this.
  43. javiersanj_unity

    javiersanj_unity

    Joined:
    Jun 24, 2018
    Posts:
    2
    Just to keep this updated, cause it mattered to me:

    iOS:
    Code (CSharp):
    1. Application.OpenURL("fb://profile/xxxxx");
    Android:
    Code (CSharp):
    1. Application.OpenURL("fb://page/xxxxx");
    This is what worked for me.
     
  44. Aarlington

    Aarlington

    Joined:
    Jul 8, 2018
    Posts:
    11
    For android this seems to be working too:

    Code (CSharp):
    1. Application.OpenURL("fb://profile/xx);
     
  45. hiauho

    hiauho

    Joined:
    Jul 6, 2018
    Posts:
    1
    Finally, i found my solution,
    1. open the page u want to open
    2. right click -> view sources -> search for "fb://"
    3. u can find two links
    one for android:
    upload_2020-2-13_19-42-10.png
    and the other for ios
    upload_2020-2-13_19-42-27.png
    (in my case, i want to open a group page, pages, profiles the same)
    4. use these links for each platform
     

    Attached Files:

    slaczky likes this.
unityunity