如何通过点击 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
中,我不知道=/
最佳答案
Update for Swift 4 and iOS 10+
好的,在 Swift 3 中只需两个简单的步骤即可实现此目的:
首先,您必须修改Info.plist
列表 instagram
和facebook
与 LSApplicationQueriesSchemes
。只需打开Info.plist
作为源代码,然后粘贴:
<key>LSApplicationQueriesSchemes</key>
<array>
<string>instagram</string>
<string>fb</string>
</array>
之后就可以打开instagram
和facebook
使用 instagram://
的应用程序和fb://
。这是 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)")!
关于ios - 如何通过点击 Swift 中的按钮来打开 Facebook 和 Instagram 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56367760/