Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open link to Facebook page from iOS app

Tags:

ios

facebook

I want to redirect user to my App's Facebook page, so I have the following code:

[[UIApplication sharedApplication] openURL:     [NSURL URLWithString: @"https://facebook.com/myAppsPage"]]; 

this works great, if there is no Facebook app on device. Then the link opens in Safari.

If there is Facebook app installed on device, then after executing above code Facebook app opens, but it is showing users timeline, not my app's page. I tried to change link to @"fb://pages/myAppsPage" and to @"fb://profile/myAppsPage, but this again opened users timeline.

So, my question is: how can I open my apps page in safari, if there is no Facebook app, or in Facebook app, if the one is installed.

like image 267
medvedNick Avatar asked May 16 '13 13:05

medvedNick


People also ask

How do I open links directly on Facebook app iPhone?

On an iPhone 6S through XS, 3D Touch (that's a forceful press) on the link that you want opened. One of the options, among others, will be to open in Facebook. On an iPhone up to the 6 and from the iPhone 11 onward, the process is the same, except you press and hold the link, since these phones do not have 3D Touch.


2 Answers

You have to use the facebook ID for the given page rather than the vanity URL. To get the pages ID, enter your page's vanity name at the following URL and copy the value for "id":

https://graph.facebook.com/yourappspage

Once you have the id, you can set up the method to check if FB is installed using the canOpenURL method and serve the appropriate content for both states:

NSURL *facebookURL = [NSURL URLWithString:@"fb://profile/113810631976867"]; if ([[UIApplication sharedApplication] canOpenURL:facebookURL]) {     [[UIApplication sharedApplication] openURL:facebookURL]; } else {     [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://facebook.com"]]; } 
like image 94
Sean Kladek Avatar answered Oct 20 '22 13:10

Sean Kladek


It seems that since the last answer Facebook have changed the scheme for pages (profile is same as before)

the page_id is now the part of URL query. So in order to redirect to fb app, the url has to be in the format:

fb://page/?id=your_page_numeric_id 

Also make sure to whitelist fb scheme in your apps Info.plist

<key>LSApplicationQueriesSchemes</key> <array>     <string>fb</string> </array> 

In case there will be any further modifications or you don't know your facebook page's page_id, you can extract the precise iOS fb URL from your Facebook web page source.

  1. Open your Facebook page in Safari
  2. Right click -> Inspect Element
  3. Search for al:ios:url

You should be able to find:

<meta property="al:ios:url" content="fb://page/?id='your_page_numeric_id'"> 

Copying this URL into your app and then calling:

guard let facebookURL = NSURL(string: "fb://page/?id=your_page_numeric_id") else {     return }  if (UIApplication.sharedApplication().canOpenURL(facebookURL)) {     UIApplication.sharedApplication().openURL(facebookURL) } else {      guard let webpageURL = NSURL(string: "http://facebook.com/yourPage/") else {         return     }      UIApplication.sharedApplication().openURL(webpageURL) } 

should do the job...

like image 35
ambientlight Avatar answered Oct 20 '22 14:10

ambientlight