[英]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
,我不知道 =/
Swift 4 和 iOS 10+ 的更新
好的,在 Swift 3 中有兩個簡單的步驟可以實現這一點:
首先,您必須修改Info.plist
以使用LSApplicationQueriesSchemes
列出instagram
和facebook
。 只需打開Info.plist
作為源代碼,然后粘貼:
<key>LSApplicationQueriesSchemes</key>
<array>
<string>instagram</string>
<string>fb</string>
</array>
之后,您可以使用instagram://
和fb://
來打開instagram
和facebook
應用程序。 這是 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)")!
看看這些鏈接,它可以幫助你:
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/")!)
}
您實際上不再需要使用網絡和應用程序 URL。 如果用戶擁有 Web URL,它將在應用程序中自動打開。 Instagram 或其他應用程序將其作為通用鏈接實現
斯威夫特 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:
只需更改appURL和webURL :
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)
}
}
在swift5中,使用這個
guard let instagram = URL(string: "https://www.instagram.com/yourpagename") else { return }
UIApplication.shared.open(instagram)
基於這里接受的答案是使用 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
首先,您必須修改Info.plist
以使用LSApplicationQueriesSchemes
列出instagram
和facebook
。 只需打開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
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或www.instagram.com/user等鏈接
這樣做時,Instagram 和 Facebook 應用程序會自動打開。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.