Open link to Facebook page from iOS app

前端 未结 8 1059
孤独总比滥情好
孤独总比滥情好 2020-11-30 18:29

I want to redirect user to my App\'s Facebook page, so I have the following code:

[[UIApplication sharedApplication] openURL:
    [NSURL URLWithString: @\"ht         


        
8条回答
  •  有刺的猬
    2020-11-30 18:59

    It seems that since the last answer Facebook have changed the scheme for pages (profile is same as before)

    the page_id is now the part of URL query. So in order to redirect to fb app, the url has to be in the format:

    fb://page/?id=your_page_numeric_id
    

    Also make sure to whitelist fb scheme in your apps Info.plist

    LSApplicationQueriesSchemes
    
        fb
    
    

    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.

    1. Open your Facebook page in Safari
    2. Right click -> Inspect Element
    3. Search for al:ios:url

    You should be able to find:

    
    

    Copying this URL into your app and then calling:

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

提交回复
热议问题