如何通过点击 Swift 中的按钮打开 fb 和 instagram 应用程序
How to open fb and instagram app by tapping on button in Swift
如何通过点击 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
中,我不知道 =/
看看这些链接,它可以帮助你:
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/")!)
}
在swift3;
首先你应该把这个添加到你的 Info.plist
您可以使用此代码;
let instagramUrl = URL(string: "instagram://app")
UIApplication.shared.canOpenURL(instagramUrl!)
UIApplication.shared.open(instagramUrl!, options: [:], completionHandler: nil)
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://
和fb://
打开instagram
和facebook
个应用程序。这是 instagram 的完整代码,您可以对 facebook 执行相同的操作,您可以将此代码 link 作为您拥有的任何按钮的操作:
@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)")!
根据接受的答案,这里是使用 Swift 4
更优雅地做到这一点的方法
UIApplication.tryURL([
"instagram://user?username=johndoe", // App
"https://www.instagram.com/johndoe/" // Website if app fails
])
并真正记得添加方案以允许应用程序打开。然而,即使您忘记了 instagram 将在 Safari 中打开。
tryUrl 是类似于此处介绍的扩展:
在swift 4:
Just change appURL and webURL :
twitter://user?screen_name=\(screenName)
instagram://user?screen_name=\(screenName)
facebook://user?screen_name=\(screenName)
- 'openURL' was deprecated in 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)
}
}
您实际上不再需要使用网络和应用程序 URL。如果用户拥有网络 URL,它将在应用程序中自动打开。 Instagram 或其他应用程序最终将其实现为 Universal Link
Swift 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)
}
}
}
在swift5中,使用这个
guard let instagram = URL(string: "https://www.instagram.com/yourpagename") else { return }
UIApplication.shared.open(instagram)
SwiftUI 版本
加入Info.plist
首先,您必须修改 Info.plist
以将 instagram
和 facebook
列为 LSApplicationQueriesSchemes
。只需打开 Info.plist
作为源代码,然后粘贴:
<key>LSApplicationQueriesSchemes</key>
<array>
<string>instagram</string>
<string>fb</string>
</array>
当您想打开 Facebook 应用程序并定向到 Facebook 页面时,请使用页面 ID。这是一个 Link,您可以在其中找到它们:https://www.facebook.com/help/1503421039731588
计划
fb://profile – Open Facebook app to the user’s profile OR pages
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:
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")
})
用于从您的应用程序打开 instagram 或 facebook 页面,它对我有用
只是为了使用像 www.facebook.com/user , or www.instagram.com/user
这样的链接
执行此操作时,instagram 和 facebook 应用会自动打开。
如何通过点击 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
中,我不知道 =/
看看这些链接,它可以帮助你:
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/")!)
}
在swift3;
首先你应该把这个添加到你的 Info.plist
您可以使用此代码;
let instagramUrl = URL(string: "instagram://app")
UIApplication.shared.canOpenURL(instagramUrl!)
UIApplication.shared.open(instagramUrl!, options: [:], completionHandler: nil)
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://
和fb://
打开instagram
和facebook
个应用程序。这是 instagram 的完整代码,您可以对 facebook 执行相同的操作,您可以将此代码 link 作为您拥有的任何按钮的操作:
@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)")!
根据接受的答案,这里是使用 Swift 4
更优雅地做到这一点的方法UIApplication.tryURL([
"instagram://user?username=johndoe", // App
"https://www.instagram.com/johndoe/" // Website if app fails
])
并真正记得添加方案以允许应用程序打开。然而,即使您忘记了 instagram 将在 Safari 中打开。
tryUrl 是类似于此处介绍的扩展:
在swift 4:
Just change appURL and webURL :
twitter://user?screen_name=\(screenName)
instagram://user?screen_name=\(screenName)
facebook://user?screen_name=\(screenName)
- 'openURL' was deprecated in 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)
}
}
您实际上不再需要使用网络和应用程序 URL。如果用户拥有网络 URL,它将在应用程序中自动打开。 Instagram 或其他应用程序最终将其实现为 Universal Link
Swift 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)
}
}
}
在swift5中,使用这个
guard let instagram = URL(string: "https://www.instagram.com/yourpagename") else { return }
UIApplication.shared.open(instagram)
SwiftUI 版本
加入Info.plist
首先,您必须修改 Info.plist
以将 instagram
和 facebook
列为 LSApplicationQueriesSchemes
。只需打开 Info.plist
作为源代码,然后粘贴:
<key>LSApplicationQueriesSchemes</key>
<array>
<string>instagram</string>
<string>fb</string>
</array>
当您想打开 Facebook 应用程序并定向到 Facebook 页面时,请使用页面 ID。这是一个 Link,您可以在其中找到它们:https://www.facebook.com/help/1503421039731588
计划
fb://profile – Open Facebook app to the user’s profile OR pages
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:
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")
})
用于从您的应用程序打开 instagram 或 facebook 页面,它对我有用 只是为了使用像 www.facebook.com/user , or www.instagram.com/user
这样的链接执行此操作时,instagram 和 facebook 应用会自动打开。