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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10416338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:13:16  来源:igfitidea点击:

Open a facebook link by native Facebook app on iOS

iphoneiosxcodefacebook

提问by vietstone

If the native Facebook app is installed on the iPhone. How do I open a facebook link into the native Facebook app from my app. In the case of opening by Safari, the link is same as:

如果本机 Facebook 应用程序安装在 iPhone 上。如何从我的应用程序打开 Facebook 链接到本机 Facebook 应用程序。在Safari打开的情况下,链接与:

http://www.facebook.com/AlibabaUS

http://www.facebook.com/AlibabaUS

Thank you.

谢谢你。

回答by WrightsCS

Here are some schemes the Facebook app uses, there are a ton more on the source link:

这里有一些方案的Facebook应用程序的使用,有一吨多源链接:

Example

例子

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

Schemes

方案

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

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 应用到相册列表

If before opening this url you want to check wether the user fas the facebook app you can do the following (as explained in another answer below):

如果在打开此 url 之前您想检查用户是否使用 facebook 应用程序,您可以执行以下操作(如下面的另一个答案所述):

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

Source

来源

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

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

回答by Romano401

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

只需使用https://graph.facebook.com/(your_usernameor page name) 获取您的页面 ID,然后您就可以看到所有的拘留和您的 ID

after in your IOS app use :

在您的 IOS 应用程序中使用后:

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

回答by zoul

To add yonix's comment as an answer, the old fb://page/…URL no longer works. Apparently it was replaced by fb://profile/…, even though a page is not a profile.

要添加yonix的评论作为一个答案,旧的fb://page/…URL不再起作用。显然它被替换为fb://profile/…,即使页面不是个人资料。

回答by Brad Moore

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.

我用下面的方法在 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;
    }
}

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.

在您的应用程序更改为 Safari/Facebook 之前,并不总是调用中断。我假设您的程序将在那里停止并在您返回时调用它。

回答by HussoM

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!.

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

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

注: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];
    }

}

回答by iParesh

swift 3

迅捷 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)")
        }
 }

回答by Dan

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

就在今天证实,但如果你试图打开一个Facebook页面,你可以使用“FB://页/ {页ID}”

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

页面 ID 可以在页面下方靠近底部的关于部分中找到。

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

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

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

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

回答by user3012572

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!

如果已登录 Facebook 应用程序,则执行以下代码时将打开该页面。如果在执行代码时 Facebook 应用程序没有登录,那么用户将被重定向到 Facebook 应用程序登录,然后在连接 Facebook 后不会重定向到页面!

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

回答by Saad

This will open the URL in Safari:

这将在 Safari 中打开 URL:

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

For the iphone, you can launch the Facebook app if installed by using a url starting with fb://

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

More information can be found here: http://iphonedevtools.com/?p=302also here: http://wiki.akosma.com/IPhone_URL_Schemes#Facebook

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

Stolen from the above site:

从上述网站窃取:

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

To launch the above URLs:

要启动上述 URL:

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