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

24 facebook ios instagram swift

如何通过点击按钮打开Facebook和Instagram应用程序swift?某些应用重定向到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!)
}
Run Code Online (Sandbox Code Playgroud)

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

小智 33

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

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

http://wiki.akosma.com/IPhone_URL_Schemes

在iOS上通过原生Facebook应用打开Facebook链接

否则,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/")!)
}
Run Code Online (Sandbox Code Playgroud)


Ibr*_*him 27

Swift 4和iOS 10+的更新

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

首先,你必须修改Info.plist列表instagramfacebook使用LSApplicationQueriesSchemes.只需打开Info.plist源代码,然后粘贴:

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>instagram</string>
    <string>fb</string>
</array>
Run Code Online (Sandbox Code Playgroud)

之后,您可以使用和打开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)
    }

}
Run Code Online (Sandbox Code Playgroud)

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

let appURL = URL(string: "fb://profile/\(Username)")!
Run Code Online (Sandbox Code Playgroud)


小智 8

在迅捷3;

首先,您应该在Info.plist上添加它

在此输入图像描述

比你可以使用这个代码;

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


Doe*_*ata 7

您实际上不再需要使用网络和应用程序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)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Md *_*ury 6

快速4:

只需更改appURLwebURL即可

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

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

facebook://user?screen_name=\(screenName)
Run Code Online (Sandbox Code Playgroud)
  • iOS 10.0中不推荐使用“ openURL”:
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)
        }
    }
Run Code Online (Sandbox Code Playgroud)


小智 6

在swift5中,使用这个

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

  • 请投票赞成。通用 URL 应该是现在的首选方式。 (2认同)

Pom*_*o-J 5

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

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

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

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


Jon*_*ann 5

SwiftUI 版本

\n\n

在Info.plist中添加

\n\n

首先,您必须修改Info.plist为 listinstagramfacebookwith LSApplicationQueriesSchemesInfo.plist只需作为源代码打开,然后粘贴以下内容:

\n\n
<key>LSApplicationQueriesSchemes</key>\n<array>\n    <string>instagram</string>\n    <string>fb</string>\n</array>\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n\n

方案

\n\n
\n

fb://profile \xe2\x80\x93 打开 Facebook 应用程序到用户\xe2\x80\x99s 个人资料页面

\n\n

fb://friends \xe2\x80\x93 打开 Facebook 应用程序到好友列表

\n\n

fb://notifications \xe2\x80\x93 打开 Facebook 应用程序到通知列表(注意:此 URL 似乎存在错误。“通知”页面将打开。但是,\xe2\x80\x99 无法导航到Facebook 应用程序中的任何其他位置)

\n\n

fb://feed \xe2\x80\x93 打开 Facebook 应用程序的动态消息

\n\n

fb://events \xe2\x80\x93 打开 Facebook 应用程序至“活动”页面

\n\n

fb://requests \xe2\x80\x93 打开 Facebook 应用程序到请求列表

\n\n

fb://notes \xe2\x80\x93 打开 Facebook 应用程序至“备注”页面

\n\n

fb://albums \xe2\x80\x93 打开 Facebook 应用到相册列表\n 来源:https ://stackoverflow.com/a/10416399/8642838

\n
\n\n

SwiftUI-代码版本

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