65

如果 iPhone 上安装了原生 Facebook 应用程序。如何从我的应用程序中打开进入本机 Facebook 应用程序的 facebook 链接。Safari打开的情况下,链接同:

http://www.facebook.com/AlibabaUS

谢谢你。

4

11 回答 11

132

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

例子

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

计划

fb://profile – 打开 Facebook 应用到用户的个人资料

fb://friends – 打开 Facebook 应用到好友列表

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

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

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

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

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

fb://albums – 打开 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

于 2012-05-02T15:02:29.753 回答
27

Just use https://graph.facebook.com/(your_username or page name) to get your page ID and after you can see all the detain and your ID

after in your IOS app use :

 NSURL *url = [NSURL URLWithString:@"fb://profile/[your ID]"];
 [[UIApplication sharedApplication] openURL:url];
于 2012-11-20T09:53:31.287 回答
26
于 2012-11-07T09:35:19.440 回答
10

I did this in MonoTouch in the following method, I'm sure a pure Obj-C based approach wouldn't be too different. I used this inside a class which had changing URLs at times which is why I just didn't put it in a if/elseif statement.

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;
    }
}

The break is not always called before your app changes to Safari/Facebook. I assume your program will halt there and call it when you come back to it.

于 2012-11-14T01:42:32.087 回答
8

I know the question is old, but in support of the above answers i wanted to share my working code. Just add these two methods to whatever class.The code checks if facebook app is installed, if not installed the url is opened in a browser.And if any errors occur when trying to find the profileId, the page will be opened in a browser. Just pass the url(example, http://www.facebook.com/AlibabaUS) to openUrl: and it will do all the magic. Hope it helps someone!.

NOTE: UrlUtils was the class that had the code for me, you might have to change it to something else to suite your needs.

+(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];
    }

}
于 2013-09-02T13:38:08.523 回答
4

swift 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)")
        }
 }
于 2017-02-24T11:35:47.983 回答
1

If the Facebook application is logged in, the page will be opened when executing the following code. If the Facebook application is not logged in when executing the code, the user will then be redirected to the Facebook app to login and then after connecting the Facebook is not redirected to the page!

NSURL *fbNativeAppURL = [NSURL URLWithString:@"fb://page/yourPageIDHere"] [[UIApplication sharedApplication] openURL:fbNativeAppURL]
于 2013-11-20T10:23:43.840 回答
1

Just verified this today, but if you are trying to open a Facebook page, you can use "fb://page/{Page ID}"

Page ID can be found under your page in the about section near the bottom.

Specific to my use case, in Xamarin.Forms, you can use this snippet to open in the app if available, otherwise in the browser.

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

于 2017-03-22T01:57:16.307 回答
1

Previous method fb://profile/[ID] is not working.

try fb://profile?id=[ID]

Example of usage:

// Open Profile or page
// To open in Application
if UIApplication.shared.canOpenURL(URL(string: "fb://profile?id=vikramchaudhary.ios")!) {
    UIApplication.shared.open(URL(string: "fb://profile?id=vikramchaudhary.ios")!, options: [:], completionHandler: nil)
} else {
    // To open in safari
    UIApplication.shared.open(URL(string: "https://facebook.com/vikramchaudhary.ios/")!, options: [:], completionHandler: nil)
}
于 2021-03-06T05:33:05.803 回答
0

这将在 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 应用程序

更多信息可以在这里找到: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];
于 2012-05-02T15:04:17.987 回答
0

2020 answer

Just open the url. Facebook automatically registers for deep links.

let url = URL(string:"https://www.facebook.com/TheGoodLordAbove")!
UIApplication.shared.open(url,completionHandler:nil)

this opens in the facebook app if installed, and in your default browser otherwise

于 2020-09-29T10:57:53.497 回答