[英]How do I open Instagram app from my iOS app?
I am trying to open the Instagram application from my iOS app through UIButton action but it's not working.我正在尝试通过 UIButton 操作从我的 iOS 应用程序打开 Instagram 应用程序,但它不起作用。 It shows nothing.
它什么也没显示。
Here is my code in Swift 3:这是我在 Swift 3 中的代码:
@IBAction func Instagram(_ sender: AnyObject) {
let instagramURL = URL(string: "instagram://app")!
if UIApplication.shared.canOpenURL(instagramURL) {
UIApplication.shared.openURL(instagramURL)
}
The method openUrl
which you have posted in comments in deprecated too in iOS 10您在评论中发布的
openUrl
方法在 iOS 10 中也已弃用
Use this使用这个
@IBAction func openInstagramButtonPressed(_ sender: Any) {
let instagram = URL(string: "instagram://app")!
if UIApplication.shared.canOpenURL(instagram) {
UIApplication.shared.open(instagram, options: ["":""], completionHandler: nil)
} else {
print("Instagram not installed")
}
}
For the first time it will prompt you to open or cancel.第一次它会提示您打开或取消。
Also make sure to do the LSApplicationQueriesSchemes
entry for instagram which you had already done.还要确保为您已经完成的 instagram 执行
LSApplicationQueriesSchemes
条目。
For swift 4.2+ and ios 9+ This code launch Instagram, if it's not installed, launch the Instagram profile page in a web browser.对于 swift 4.2+ 和 ios 9+ 此代码启动 Instagram,如果未安装,请在网络浏览器中启动 Instagram 个人资料页面。
let screenName = "mehdico" // CHANGE THIS
let appURL = URL(string: "instagram://user?username=\(screenName)")
let webURL = URL(string: "https://instagram.com/\(screenName)")
if UIApplication.shared.canOpenURL(appURL) {
if #available(iOS 10.0, *) {
UIApplication.shared.open(appURL, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(appURL)
}
} else {
//redirect to safari because the user doesn't have Instagram
if #available(iOS 10.0, *) {
UIApplication.shared.open(webURL, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(webURL)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.