简体   繁体   English

从 iOS 应用打开 Facebook 页面的链接

[英]Open link to Facebook page from iOS app

I want to redirect user to my App's Facebook page, so I have the following code:我想将用户重定向到我的应用程序的 Facebook 页面,所以我有以下代码:

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

this works great, if there is no Facebook app on device.如果设备上没有 Facebook 应用程序,这很好用。 Then the link opens in Safari.然后链接在 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.如果设备上安装了 Facebook 应用程序,则在执行上述代码后 Facebook 应用程序打开,但它显示的是用户时间线,而不是我的应用程序页面。 I tried to change link to @"fb://pages/myAppsPage" and to @"fb://profile/myAppsPage , but this again opened users timeline.我试图将链接更改为@"fb://pages/myAppsPage"@"fb://profile/myAppsPage ,但这再次打开了用户时间线。

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.所以,我的问题是:如果没有 Facebook 应用程序或 Facebook 应用程序(如果安装了),我如何在 safari 中打开我的应用程序页面。

You have to use the facebook ID for the given page rather than the vanity URL.您必须使用给定页面的 facebook ID 而不是虚 URL。 To get the pages ID, enter your page's vanity name at the following URL and copy the value for "id":要获取页面 ID,请在以下 URL 中输入页面的虚名并复制“id”的值:

https://graph.facebook.com/yourappspage 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:获得 id 后,您可以设置方法以检查是否使用 canOpenURL 方法安装了 FB 并为两种状态提供适当的内容:

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

It seems that since the last answer Facebook have changed the scheme for pages (profile is same as before)似乎自上次回答以来 Facebook 已经更改了页面方案(配置文件与以前相同)

the page_id is now the part of URL query. page_id现在是 URL 查询的一部分。 So in order to redirect to fb app, the url has to be in the format:因此,为了重定向到 fb 应用程序,网址必须采用以下格式:

fb://page/?id=your_page_numeric_id

Also make sure to whitelist fb scheme in your apps Info.plist还要确保在您的应用程序Info.plist中将fb方案列入白名单

<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.如果将有任何进一步的修改或您不知道您的 Facebook 页面的page_id ,您可以从您的 Facebook 网页源中提取精确的 iOS fb URL。

  1. Open your Facebook page in Safari在 Safari 中打开您的 Facebook 页面
  2. Right click -> Inspect Element右键单击 -> 检查元素
  3. Search for al:ios:url搜索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:将此 URL 复制到您的应用程序中,然后调用:

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...应该做这个工作...

I was happy to find skladek's answer, so here's my contribution: Swift + other social networks.我很高兴找到 skladek 的答案,所以这是我的贡献:Swift + 其他社交网络。

I found that I needed 3 forward slashes for twitter, and only 2 for Facebook and Google+, I didn't investigate more than that…我发现 Twitter 需要 3 个正斜杠,而 Facebook 和 Google+ 只需要 2 个,我没有调查更多……

// Go to https://graph.facebook.com/PageName to get your page id
func openFacebookPage() {
    let facebookURL = NSURL(string: "fb://profile?id=PageName")!
    if UIApplication.sharedApplication().canOpenURL(facebookURL) {
        UIApplication.sharedApplication().openURL(facebookURL)
    } else {
        UIApplication.sharedApplication().openURL(NSURL(string: "https://www.facebook.com/PageName")!)
    }
}

func openTwitterProfile() {
    let twitterURL = NSURL(string: "twitter:///user?screen_name=USERNAME")!
    if UIApplication.sharedApplication().canOpenURL(twitterURL) {
        UIApplication.sharedApplication().openURL(twitterURL)
    } else {
        UIApplication.sharedApplication().openURL(NSURL(string: "https://twitter.com/USERNAME")!)
    }
}

func openGooglePlusPage() {
    let googlePlusURL = NSURL(string: "gplus://plus.google.com/u/0/PageId")!
    if UIApplication.sharedApplication().canOpenURL(googlePlusURL) {
        UIApplication.sharedApplication().openURL(googlePlusURL)
    } else {
        UIApplication.sharedApplication().openURL(NSURL(string: "https://plus.google.com/PageId")!)
    }
}

An attempt at refactoring:重构的尝试:

struct SocialNetworkUrl {
    let scheme: String
    let page: String

    func openPage() {
        let schemeUrl = NSURL(string: scheme)!
        if UIApplication.sharedApplication().canOpenURL(schemeUrl) {
            UIApplication.sharedApplication().openURL(schemeUrl)
        } else {
            UIApplication.sharedApplication().openURL(NSURL(string: page)!)
        } 
    }
}

enum SocialNetwork {
    case Facebook, GooglePlus, Twitter, Instagram
    func url() -> SocialNetworkUrl {
        switch self {
        case .Facebook: return SocialNetworkUrl(scheme: "fb://profile/PageId", page: "https://www.facebook.com/PageName")
        case .Twitter: return SocialNetworkUrl(scheme: "twitter:///user?screen_name=USERNAME", page: "https://twitter.com/USERNAME")
        case .GooglePlus: return SocialNetworkUrl(scheme: "gplus://plus.google.com/u/0/PageId", page: "https://plus.google.com/PageId")
        case .Instagram: return SocialNetworkUrl(scheme: "instagram://user?username=USERNAME", page:"https://www.instagram.com/USERNAME")
        }
    }
    func openPage() {
        self.url().openPage()
    }
}

Now you can call:现在你可以调用:

SocialNetwork.Twitter.openPage()

For iOS 9, You must whitelist the url's that your app will call out to using the LSApplicationQueriesSchemes key in your Info.plist.对于 iOS 9,您必须将应用程序将使用 Info.plist 中的 LSApplicationQueriesSchemes 键调用的 URL 列入白名单。

Check here .检查这里

<key>LSApplicationQueriesSchemes</key>
    <array>
     <string>fb</string>
     <string>fbapi</string>
     <string>fbauth2</string>
     <string>fbshareextension</string>
     <string>fb-messenger-api</string>
     <string>twitter</string>
     <string>whatsapp</string>
     <string>wechat</string>
     <string>line</string>
     <string>instagram</string>
     <string>kakaotalk</string>
     <string>mqq</string>
     <string>vk</string>
     <string>comgooglemaps</string>
    </array>

I just found this URL scheme fb://profile?id={userid/username/pageid} , and it is working fine as of 30 Aug 2020.我刚刚找到了这个 URL 方案fb://profile?id={userid/username/pageid} ,截至 2020 年 8 月 30 日它运行良好。

eg: fb://profile?id=4 OR fb://profile?id=zuck例如: fb://profile?id=4fb://profile?id=zuck

Warning: Please use at your own risk as this is not documented in Facebook developers documentation.警告:请自担风险,因为 Facebook 开发人员文档中没有记录。 This URL scheme may be removed by Facebook app in the future without any warning and indication. Facebook 应用程序将来可能会在没有任何警告和指示的情况下删除此 URL 方案。

After much testing I have found one of the most effective solutions:经过多次测试,我找到了最有效的解决方案之一:

NSString *facebookID = @"XXXXXXXXXX";
NSString *facebookURL = @"www.facebook.com/XXXXXXXX";
NSURL *urlModal = [NSURL URLWithString:[NSString stringWithFormat:@"fb://facewebmodal/f?href=%@", facebookURL]];
NSURL *urlWithID = [NSURL URLWithString:[NSString stringWithFormat:@"fb://page/%@", facebookID]]; // or "fb://profile/%@" or another type of ID
NSURL *url = [NSURL URLWithString:facebook_url];


if(facebookID && !facebookID.isEmpty && [[UIApplication sharedApplication] canOpenURL:urlWithID]) {
    // open facebook app with facebookID
    [[UIApplication sharedApplication] openURL:urlWithID];
} else if (facebookURL && !facebookURL.isEmpty && [[UIApplication sharedApplication] canOpenURL:urlModal]) {
    // open facebook app with facebookUrl
    [[UIApplication sharedApplication] openURL:urlModal];
} else if (![[UIApplication sharedApplication] openURL:url]) {
    // somehow open
    NSLog(@"%@%@",@"Failed to open url:",[url description]);
}

Order of sentences "if else" vary to your liking ... “if else”的句子顺序因您的喜好而异......

Enjoy it!!好好享受!! :-) :-)

Swift 2.3斯威夫特 2.3

For code you can use this example:对于代码,您可以使用此示例:

Note: If you can't find the facebook profile id can you use the follow site to do it ( https://findmyfbid.com/ )注意:如果您找不到 facebook 个人资料 ID,您可以使用以下网站来完成 ( https://findmyfbid.com/ )

func goFacebook() {
    let profileID = "" //Here put profile id Example:'62260589592'
    let facebookURL = NSURL(string: "fb://profile/\(profileID)")!

    if UIApplication.sharedApplication().canOpenURL(facebookURL) {
        UIApplication.sharedApplication().openURL(facebookURL)
    } else {
        UIApplication.sharedApplication().openURL(NSURL(string: "https://www.facebook.com/PageName")!)
    }
}

For Info.plist you need set the follow parameters:对于 Info.plist,您需要设置以下参数:

Note: For open the Info.plist on Source Mode注意:用于在源模式下打开 Info.plist

-Go to Info.plist file and click right - 转到 Info.plist 文件并单击右键

-Select "Open As" - 选择“打开为”

-Select "Source Code" - 选择“源代码”

-Put code above of another - 把代码放在另一个上面

<key>LSApplicationQueriesSchemes</key>
<array>
 <string>fb</string>
 <string>fbapi</string>
 <string>fbauth2</string>
 <string>fbshareextension</string>
 <string>fb-messenger-api</string>
</array>

// Swift 5, you can use this function to open fb page/profile // Swift 5,你可以使用这个函数打开fb页面/个人资料

       func openFacebookPage() {
            let facebookURL = NSURL(string: "fb://profile/PageId")!
            if UIApplication.shared.canOpenURL(facebookURL as URL) {
            UIApplication.shared.open(facebookURL as URL)
        } else {
            UIApplication.shared.open(NSURL(string: 
             "https://www.facebook.com/PageName")! as URL)
           }
      }
//By Using PageId or name only
      func openFacebookPage(pageID:String) {
            let facebookURL = NSURL(string: "fb://profile/\(pageID)")!
            if UIApplication.shared.canOpenURL(facebookURL as URL) {
            UIApplication.shared.open(facebookURL as URL)
         } else {
             UIApplication.shared.open(NSURL(string: "https://www.facebook.com/\ 
           (pageID)")! as URL)
         }
     }
//By Using Url only
     func openFacebookPageURL(url:String) {
               let facebookURL = NSURL(string: url)!
               if UIApplication.shared.canOpenURL(facebookURL as URL) {
               UIApplication.shared.open(facebookURL as URL)
            } else {
               UIApplication.shared.open(NSURL(string: url)! as URL)
            }
        }

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

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