簡體   English   中英

如何從我的 iOS 應用程序打開 Instagram 應用程序?

[英]How do I open Instagram app from my iOS app?

我正在嘗試通過 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)
    }

這是我的 Plist 密鑰 -

您在評論中發布的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,如果未安裝,請在網絡瀏覽器中啟動 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM