Open facebook app instead safari / iOS - C#

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:

Application.OpenURL ("fb://page/xxxxx");

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:

 void OpenFacebookPage ()
 {
        WWW www = new WWW("fb://page/xxxxx");
        StartCoroutine(WaitForRequest(www));
 }

    IEnumerator WaitForRequest(WWW www)
    {
        yield return www;

        // check for errors
        if (www.error == null)
        {
            Debug.Log("Sucess!: " + www.text);
        } 
 else 
 {
            Debug.Log("WWW Error: "+ www.error + " Opening Safari...");
            //error. Open normal address
            Application.OpenURL ("http://www.facebook.com/xxxxxx");

        }    
    }

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!

I happen to be implementing this soon – will let you know if I get it working.

Thanks. I’ll be waiting…

Application.OpenURL(“fb://page/315797608481737”);

This worked like a charm for me. One thing to note – while my page’s address is Redirecting..., 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

Yes, it works. But what happen if you delete your facebook app from your device?? Will it open safari or do nothing?

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.

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://).

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

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.

I got an idea… I’ll test.

Blah, my 3GS didn’t want to be recognized by Xcode (didn’t want to delete fb from 4S/iPad3) -_-

	IEnumerator OpenFacebookPage(){
		Application.OpenURL("fb://page/315797608481737");
		yield return new WaitForSeconds(1);
		if(leftApp){
			leftApp = false;
		}
		else{
			Application.OpenURL("https://www.facebook.com/CakeDayApp");
		}
	}
	
	bool leftApp = false;
	
	void OnApplicationPause(){
		leftApp = true;
	}

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

4 Likes

Nice, thank you

this also works :wink:

void OpenFacebookPage ()
	{
		float startTime;
		startTime = Time.timeSinceLevelLoad;
		
		//open the facebook app
        Application.OpenURL(facebookApp);
		
		if (Time.timeSinceLevelLoad - startTime <= 1f)
		{
			//fail. Open safari.
			Application.OpenURL(facebookAddress);
		}
}
1 Like

same way to open the twitter app or safari. Problem solved.

But your code is more elegant.

1 Like

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!

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/

1 Like

I’m downloading the new facebook update right now. I will check it out.

Change “page” to “profile” ie fb://profile/315797608481737 and it should work

2 Likes

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?

Nah. I just haven’t pushed out that update yet so it never went live. Working on this project in my free time

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 }