简体   繁体   English

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

[英]Open a facebook link by native Facebook app on iOS

If the native Facebook app is installed on the iPhone.如果本机 Facebook 应用程序安装在 iPhone 上。 How do I open a facebook link into the native Facebook app from my app.如何从我的应用程序打开 Facebook 链接到本机 Facebook 应用程序。 In the case of opening by Safari, the link is same as:在Safari打开的情况下,链接与:

http://www.facebook.com/AlibabaUS http://www.facebook.com/AlibabaUS

Thank you.谢谢你。

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://profile – 打开 Facebook 应用到用户的个人资料

fb://friends – Open Facebook app to the friends list fb://friends – 打开 Facebook 应用到好友列表

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://notifications – 打开 Facebook 应用到通知列表(注意:此 URL 似乎存在错误。通知页面打开。但是,无法导航到 Facebook 应用中的其他任何位置)

fb://feed – Open Facebook app to the News Feed fb://feed – 打开 Facebook 应用到新闻提要

fb://events – Open Facebook app to the Events page fb://events – 打开 Facebook 应用到活动页面

fb://requests – Open Facebook app to the Requests list fb://requests – 打开 Facebook 应用到请求列表

fb://notes – Open Facebook app to the Notes page fb://notes – 打开 Facebook 应用到 Notes 页面

fb://albums – Open Facebook app to Photo Albums list 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

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只需使用https://graph.facebook.com/(your_username or page name) 获取您的页面 ID,然后您就可以看到所有的拘留和您的 ID

after in your IOS app use :在您的 IOS 应用程序中使用后:

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

To add yonix's comment as an answer, the old fb://page/… URL no longer works.要将 yonix 的评论添加为答案,旧的fb://page/… URL 不再有效。 Apparently it was replaced by fb://profile/… , even though a page is not a profile.显然它被fb://profile/…取代,即使页面不是个人资料。

I did this in MonoTouch in the following method, I'm sure a pure Obj-C based approach wouldn't be too different.我用下面的方法在 MonoTouch 中做到了这一点,我确信基于纯 Obj-C 的方法不会有太大的不同。 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.我在一个有时会更改 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.在您的应用程序更改为 Safari/Facebook 之前,并不总是调用中断。 I assume your program will halt there and call it when you come back to it.我假设您的程序将在那里停止并在您返回时调用它。

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.只需将这两个方法添加到任何类。代码检查是否安装了 facebook 应用程序,如果未安装,则在浏览器中打开 url。如果在尝试查找 profileId 时发生任何错误,页面将在浏览器中打开。 Just pass the url(example, http://www.facebook.com/AlibabaUS ) to openUrl: and it will do all the magic.只需将 url(例如, http://www.facebook.com/AlibabaUS )传递给 openUrl: 它将发挥所有作用。 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.注意: 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];
    }

}

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

If the Facebook application is logged in, the page will be opened when executing the following code.如果已登录 Facebook 应用程序,则执行以下代码时将打开该页面。 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 后不会重定向到页面!

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

Just verified this today, but if you are trying to open a Facebook page , you can use "fb://page/{Page ID}"今天刚刚验证了这一点,但是如果您尝试打开 Facebook页面,则可以使用“fb://page/{Page 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}"));

Previous method fb://profile/[ID] is not working.以前的方法fb://profile/[ID]不起作用。

try fb://profile?id=[ID]试试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)
}

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=302 also 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];

2020 answer 2020 答案

Just open the url.直接打开网址。 Facebook automatically registers for deep links. Facebook 会自动注册深层链接。

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如果已安装,这将在 Facebook 应用程序中打开,否则将在您的默认浏览器中打开

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM