ios 如何通过点击 Swift 中的按钮打开 fb 和 instagram 应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29902534/
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-31 05:48:13  来源:igfitidea点击:

How to open fb and instagram app by tapping on button in Swift

iosfacebookswiftinstagram

提问by Ibrahim

How can I open Facebook and Instagram app by tapping on a button in swift? Some apps redirect to the Facebook app and open a specific page. How can I do the same thing?

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

I found it:

我找到了:

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

but I have to know the app URL. Other examples were in ObjectiveC, which I don't know =/

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

回答by jregnauld

Take a look at these links, it can help you:

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

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

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

http://wiki.akosma.com/IPhone_URL_Schemes

http://wiki.akosma.com/IPhone_URL_Schemes

Open a facebook link by native Facebook app on iOS

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

Otherwise, there is a quick example with Instagram for opening a specific profile (nickname: johndoe) here:

否则,这里有一个 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/")!)
}

回答by Ibrahim

Update for Swift 4 and iOS 10+

Swift 4 和 iOS 10+ 的更新

OK, there are two easy steps to achieve this in Swift 3:

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

First, you have to modify Info.plistto list instagramand facebookwith LSApplicationQueriesSchemes. Simply open Info.plistas a Source Code, and paste this:

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

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

After that, you can open instagramand facebookapps by using instagram://and fb://. Here is a complete code for instagram and you can do the same for facebook, you can link this code to any button you have as an Action:

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

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

}

For facebook, you can use this code:

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

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

回答by Celil Bozkurt

In swift 3;

在 swift 3;

First you should add this on your Info.plist

首先,您应该将其添加到您的 Info.plist 中

enter image description here

在此处输入图片说明

Than you can use this code;

比您可以使用此代码;

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

回答by DoesData

You actually don't need to use a web and app URL anymore. The web URL will automatically open in the app if the user has it. Instagram or other apps implement this on their end as a Universal Link

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

Swift 4

斯威夫特 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)
        }
    }
}

回答by Md Imran Choudhury

In swift 4:

在快速 4:

Just change appURLand webURL:

只需更改appURLwebURL

twitter://user?screen_name=\(screenName)

instagram://user?screen_name=\(screenName)

facebook://user?screen_name=\(screenName)
  • 'openURL' was deprecated in iOS 10.0:
  • '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)
        }
    }

回答by Pomo-J

Based on accepted answer here is the way to do this more elegantly with Swift 4

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

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

And truly remember to add the scheme to allow the app to open. However even if you forget that instagram will open in Safari.

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

The tryUrl is an extension similar to presented here: https://stackoverflow.com/a/29376811/704803

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

回答by Talha Rasool

In swift5, use this

在swift5中,使用这个

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

回答by Jonas Deichelmann

SwiftUI Version

SwiftUI 版本

Add in Info.plist

在 Info.plist 中添加

First, you have to modify Info.plistto list instagramand facebookwith LSApplicationQueriesSchemes. Simply open Info.plistas a Source Code, and paste this:

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

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

When you want to open the Facebook App and direct to a Facebook-Page, use the Page-ID. Here is a Link, where you could find them: https://www.facebook.com/help/1503421039731588

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

Schemes

方案

fb://profile – Open Facebook app to the user's profile ORpages

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 Source: https://stackoverflow.com/a/10416399/8642838

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-Code Version

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