我试图通过UIButton操作从我的iOS应用程序打开Instagram应用程序,但它无法正常工作。它没有任何表现。
这是我在Swift 3中的代码:
@IBAction func Instagram(_ sender: AnyObject) {
let instagramURL = URL(string: "instagram://app")!
if UIApplication.shared.canOpenURL(instagramURL) {
UIApplication.shared.openURL(instagramURL)
}
您在评论中发布的方法openUrl
在iOS 10中也已弃用
用这个
@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")
}
}
这是第一次提示您打开或取消。
还要确保为你已经完成的instagram的LSApplicationQueriesSchemes
条目。
对于swift 4.2和ios 9+此代码启动instagram,如果未安装,请在Web浏览器中启动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)
}
}