【问题标题】:How to open fb and instagram app by tapping on button in Swift如何通过点击 Swift 中的按钮打开 fb 和 instagram 应用程序
【发布时间】:2015-07-06 07:22:11
【问题描述】:

如何通过点击swift 中的按钮打开 Facebook 和 Instagram 应用程序?一些应用程序重定向到 Facebook 应用程序并打开特定页面。我怎样才能做同样的事情?

我找到了:

var url = NSURL(string: "itms://itunes.apple.com/de/app/x-gift/id839686104?mt=8&uo=4")

if UIApplication.sharedApplication().canOpenURL(url!) {
  UIApplication.sharedApplication().openURL(url!)
}

但我必须知道应用程序URL。其他例子在ObjectiveC,我不知道=/

【问题讨论】:

  • 请在您的帖子中包含what you have tried
  • @A-Live 我确实更新了我的问题
  • 有趣的是你如何使用我的应用程序的应用商店链接:D

标签: ios facebook swift instagram


【解决方案1】:

Swift 4 和 iOS 10+ 的更新

好的,在 Swift 3 中有两个简单的步骤可以实现这一点:

首先,您必须修改Info.plist 以列出instagramfacebookLSApplicationQueriesSchemes。只需打开 Info.plist 作为源代码,然后粘贴:

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>instagram</string>
    <string>fb</string>
</array>

之后,您可以使用instagram://fb:// 打开instagramfacebook 应用程序。这是 instagram 的完整代码,您可以对 facebook 执行相同的操作,您可以将此代码链接到您拥有的任何按钮作为操作:

@IBAction func InstagramAction() {

    let Username =  "instagram" // Your Instagram Username here
    let appURL = URL(string: "instagram://user?username=\(Username)")!
    let application = UIApplication.shared

    if application.canOpenURL(appURL) {
        application.open(appURL)
    } else {
        // if Instagram app is not installed, open URL inside Safari
        let webURL = URL(string: "https://instagram.com/\(Username)")!
        application.open(webURL)
    }

}

对于facebook,您可以使用此代码:

let appURL = URL(string: "fb://profile/\(Username)")!

【讨论】:

    【解决方案2】:

    看看这些链接,它可以帮助你:

    https://instagram.com/developer/mobile-sharing/iphone-hooks/

    http://wiki.akosma.com/IPhone_URL_Schemes

    Open a facebook link by native Facebook app on iOS

    否则,Instagram 有一个快速示例,用于在此处打开特定个人资料(昵称:johndoe):

    var instagramHooks = "instagram://user?username=johndoe"
    var instagramUrl = NSURL(string: instagramHooks)
    if UIApplication.sharedApplication().canOpenURL(instagramUrl!) {  
      UIApplication.sharedApplication().openURL(instagramUrl!)
    } else {
      //redirect to safari because the user doesn't have Instagram
      UIApplication.sharedApplication().openURL(NSURL(string: "http://instagram.com/")!)
    }
    

    【讨论】:

    • 但是 youtube 呢?我该如何使用它? youtube://channel/channel_code?我做到了,但它没有显示我的频道
    • 正如答案应该指向您的那样,当您有这样的问题时,您需要寻找 youtube 应用程序(或您感兴趣的任何其他应用程序)的 API。或者,您可以下载该应用程序并检查其 info.plist 以研究可用的应用程序 url 方案,但前提是没有文档,因为这可能不受所有版本的支持。
    • @A-Live 我查看了 API 文档,但这里没有关于 iPhone 挂钩的内容
    • Swift 4 版本: if UIApplication.shared.canOpenURL(URL(string:yourUrl)!) { UIApplication.shared.open( URL(string: yourUrl)!, options: [:]) } else { //重定向到 safari 因为用户没有 Instagram UIApplication.shared.open( URL(string: yourUrl)!, options: [:]) }
    • 您提供的前两个链接已失效。
    【解决方案3】:

    您实际上不再需要使用网络和应用 URL。如果用户拥有该 Web URL,它将在应用程序中自动打开。 Instagram 或其他应用程序最终将其实现为 Universal Link

    斯威夫特 4

    func openInstagram(instagramHandle: String) {
        guard let url = URL(string: "https://instagram.com/\(instagramHandle)")  else { return }
        if UIApplication.shared.canOpenURL(url) {
            if #available(iOS 10.0, *) {
                UIApplication.shared.open(url, options: [:], completionHandler: nil)
            } else {
                UIApplication.shared.openURL(url)
            }
        }
    }
    

    【讨论】:

      【解决方案4】:

      在 swift 3 中;

      首先你应该将它添加到你的 Info.plist 中

      比你可以使用这个代码;

          let instagramUrl = URL(string: "instagram://app")
          UIApplication.shared.canOpenURL(instagramUrl!)
          UIApplication.shared.open(instagramUrl!, options: [:], completionHandler: nil)
      

      【讨论】:

        【解决方案5】:

        在 Swift 4 中:

        只需更改 appURLwebURL

        twitter://user?screen_name=\(screenName)
        
        instagram://user?screen_name=\(screenName)
        
        facebook://user?screen_name=\(screenName)
        
        • 'openURL' 在 iOS 10.0 中已弃用:
        let screenName =  "imrankst1221"
            let appURL = NSURL(string: "instagram://user?screen_name=\(screenName)")!
            let webURL = NSURL(string: "https://twitter.com/\(screenName)")!
        
            if UIApplication.shared.canOpenURL(appURL as URL) {
                if #available(iOS 10.0, *) {
                    UIApplication.shared.open(appURL as URL, options: [:], completionHandler: nil)
                } else {
                    UIApplication.shared.openURL(appURL as URL)
                }
            } else {
                //redirect to safari because the user doesn't have Instagram
                if #available(iOS 10.0, *) {
                    UIApplication.shared.open(webURL as URL, options: [:], completionHandler: nil)
                } else {
                    UIApplication.shared.openURL(webURL as URL)
                }
            }
        

        【讨论】:

        • 这个问题的最佳答案。
        【解决方案6】:

        在swift5中,使用这个

           guard let instagram = URL(string: "https://www.instagram.com/yourpagename") else { return }
           UIApplication.shared.open(instagram)
        

        【讨论】:

        • 请投票。现在应该首选通用 URL。
        【解决方案7】:

        根据接受的答案,这里是使用 Swift 4 更优雅地做到这一点的方法

        UIApplication.tryURL([
                "instagram://user?username=johndoe", // App
                "https://www.instagram.com/johndoe/" // Website if app fails
                ])
        

        并真正记住添加方案以允许应用程序打开。但是,即使您忘记了 instagram 会在 Safari 中打开。

        tryUrl 是类似于此处介绍的扩展:https://stackoverflow.com/a/29376811/704803

        【讨论】:

        • tryURL 不是 Swift 4 功能...它是您添加的扩展(可能类似于此:stackoverflow.com/a/29376811/704803
        • @andrewtweber 你是对的!我怎么会错过:(将您的建议添加到答案本身。
        【解决方案8】:

        SwiftUI 版本

        在 Info.plist 中添加

        首先,您必须修改Info.plist 以列出instagramfacebookLSApplicationQueriesSchemes。只需打开 Info.plist 作为源代码,然后粘贴:

        <key>LSApplicationQueriesSchemes</key>
        <array>
            <string>instagram</string>
            <string>fb</string>
        </array>
        

        当您想要打开 Facebook 应用程序并定向到 Facebook 页面时,请使用页面 ID。这是一个链接,您可以在其中找到它们:https://www.facebook.com/help/1503421039731588

        方案

        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 应用程序到相册列表 来源:https://stackoverflow.com/a/10416399/8642838

        SwiftUI-代码版

            Button(action: {
                let url = URL(string: "fb://profile/<PAGE_ID>")!
                let application = UIApplication.shared
                // Check if the facebook App is installed
                if application.canOpenURL(url) {
                    application.open(url)
                } else {
                    // If Facebook App is not installed, open Safari with Facebook Link
                    application.open(URL(string: "https://de-de.facebook.com/apple")!)
                }
            }, label: {
                Text("Facebook")
            })
        

        【讨论】:

          【解决方案9】:

          对于从您的应用打开 instagram 或 facebook 页面,它对我有用 只是为了使用像 www.facebook.com/userwww.instagram.com/user 这样的链接

          执行此操作时,instagram 和 facebook 应用程序会自动打开。

          【讨论】:

          • 这取决于设备设置,它也可以打开浏览器,在您的情况下,您的设备已经记住您要在各自的应用程序中查看这些网站。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-09-11
          • 2012-01-16
          • 1970-01-01
          • 1970-01-01
          • 2022-11-12
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多