在iOS上通过本地Facebook应用程序打开Facebook链接

如果原生Facebook应用程序安装在iPhone上。 如何从我的应用程序中打开Facebook链接到本地​​Facebook应用程序。 在通过Safari打开的情况下,链接是相同的:

http://www.facebook.com/AlibabaUS

谢谢。

以下是Facebook应用程序使用的一些scheme,源链接上还有更多:

NSURL *url = [NSURL URLWithString:@"fb://profile/<id>"]; [[UIApplication sharedApplication] openURL:url]; 

计划

fb:// profile – 打开Facebook应用程序到用户的configuration文件

FB://朋友 – 打开Facebook应用程序的朋友列表

fb:// notifications – 打开Facebook应用程序到通知列表(注意:这个url似乎存在一个错误,通知页面打开,但无法导航到Facebook应用程序的其他任何地方)

fb:// feed – 打开Facebook应用程序到新闻源

fb:// events – 打开Facebook应用程序到事件页面

fb:// requests – 打开Facebook应用程序到请求列表

fb:// notes – 打开Facebook应用程序到Notes页面

fb://相册 – 打开Facebook应用程序到相册列表

如果在打开这个URL之前,你想检查用户是否有Facebook应用程序,你可以执行以下操作(如下面的另一个答案所述):

 if ([[UIApplication sharedApplication] canOpenURL:nsurl]){ [[UIApplication sharedApplication] openURL:nsurl]; } else { //Open the url as usual } 

资源

http://wiki.akosma.com/IPhone_URL_Schemes#Facebook

只需使用https://graph.facebook.com/(您的用户名或网页名称)来获取您的网页ID,并在您看到所有的扣留和您的ID后;

在你的IOS应用程序使用后:

  NSURL *url = [NSURL URLWithString:@"fb://profile/[your ID]"]; [[UIApplication sharedApplication] openURL:url]; 

要将yonix的评论添加为答案,旧的fb://page/…url不再有效。 显然,它被replace为fb://profile/… ,即使页面不是configuration文件。

我用下面的方法在MonoTouch中做了这个,我确定一个纯粹的基于Obj-C的方法不会太不同。 我在一个有时候改变URL的类中使用了这个,这就是为什么我没有把它放在if / elseif语句中。

 NSString *myUrls = @"fb://profile/100000369031300|http://www.facebook.com/ytn3rd"; NSArray *urls = [myUrls componentsSeparatedByString:@"|"]; for (NSString *url in urls){ NSURL *nsurl = [NSURL URLWithString:url]; if ([[UIApplication sharedApplication] canOpenURL:nsurl]){ [[UIApplication sharedApplication] openURL:nsurl]; break; } } 

在您的应用更改为Safari / Facebook之前,rest并不总是被调用。 我假设你的程序将停在那里,当你回到它的时候调用它。

我知道这个问题很老,但是为了支持上面的答案,我想分享我的工作代码。 只要将这两个方法添加到任何类中。代码将检查是否安装了facebook应用程序,如果未安装,则在浏览器中打开该URL。如果在查找profileId时发生任何错误,页面将在浏览器中打开。 只要通过URL(例如, http : //www.facebook.com/AlibabaUS )openUrl:它会做所有的魔术。 希望它可以帮助别人!

注意:UrlUtils是有我的代码的类,你可能需要改变它以满足你的需求。

 +(void) openUrlInBrowser:(NSString *) url { if (url.length > 0) { NSURL *linkUrl = [NSURL URLWithString:url]; [[UIApplication sharedApplication] openURL:linkUrl]; } } +(void) openUrl:(NSString *) urlString { //check if facebook app exists if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"fb://"]]) { // Facebook app installed NSArray *tokens = [urlString componentsSeparatedByString:@"/"]; NSString *profileName = [tokens lastObject]; //call graph api NSURL *apiUrl = [NSURL URLWithString:[NSString stringWithFormat:@"https://graph.facebook.com/%@",profileName]]; NSData *apiResponse = [NSData dataWithContentsOfURL:apiUrl]; NSError *error = nil; NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:apiResponse options:NSJSONReadingMutableContainers error:&error]; //check for parse error if (error == nil) { NSString *profileId = [jsonDict objectForKey:@"id"]; if (profileId.length > 0) {//make sure id key is actually available NSURL *fbUrl = [NSURL URLWithString:[NSString stringWithFormat:@"fb://profile/%@",profileId]]; [[UIApplication sharedApplication] openURL:fbUrl]; } else{ [UrlUtils openUrlInBrowser:urlString]; } } else{//parse error occured [UrlUtils openUrlInBrowser:urlString]; } } else{//facebook app not installed [UrlUtils openUrlInBrowser:urlString]; } } 

如果Facebook应用程序已login,则在执行以下代码时将打开该页面。 如果Facebook应用程序在执行代码时未login,则用户将被redirect到Facebook应用程序以进行login,然后在连接之后Facebook不会redirect到该页面!

 NSURL *fbNativeAppURL = [NSURL URLWithString:@"fb://page/yourPageIDHere"] [[UIApplication sharedApplication] openURL:fbNativeAppURL] 

迅速3

 if let url = URL(string: "fb://profile/<id>") { if #available(iOS 10, *) { UIApplication.shared.open(url, options: [:],completionHandler: { (success) in print("Open fb://profile/<id>: \(success)") }) } else { let success = UIApplication.shared.openURL(url) print("Open fb://profile/<id>: \(success)") } } 

这将在Safari中打开URL:

 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.facebook.com/comments.php?href=http://wombeta.jiffysoftware.com/ViewWOMPrint.aspx?WPID=317"]]; 

对于iPhone,如果使用以fb开头的url进行安装,则可以启动Facebook应用程序://

更多信息可以在这里find: http : //iphonedevtools.com/?p=302也在这里: http : //wiki.akosma.com/IPhone_URL_Schemes#Facebook

从以上网站盗取:

 fb://profile – Open Facebook app to the user's profile fb://friends – Open Facebook app to the friends list fb://notifications – Open Facebook app to the notifications list (NOTE: there appears to be a bug with this URL. The Notifications page opens. However, it's not possible to navigate to anywhere else in the Facebook app) fb://feed – Open Facebook app to the News Feed fb://events – Open Facebook app to the Events page fb://requests – Open Facebook app to the Requests list fb://notes - Open Facebook app to the Notes page fb://albums – Open Facebook app to Photo Albums list 

要启动上述url:

 NSURL *theURL = [NSURL URLWithString:@"fb://<insert function here>"]; [[UIApplication sharedApplication] openURL:theURL]; 

今天刚刚validation过,但是如果您尝试打开Facebook 页面 ,则可以使用“fb:// page / {Page ID}”

页面ID可以在您的页面底部附近的关于部分中find。

具体到我的用例,在Xamarin.Forms,你可以使用这个片段在应用程序中打开(如果可用),否则在浏览器中。

Device.OpenUri(new Uri("fb://page/{id}"));