簡體   English   中英

UIApplication.shared.open(_ url:URL等)沒有打開任何東西

[英]UIApplication.shared.open(_ url: URL,etc.) doesn't open anything

我已經在Info.plist文件的LSApplicationQueriesSchemes中添加了“ instagram”,但在調用以下函數時,它仍然無法打開Safari和instagram:

func openinstagram () {
    let instaurl = URL(fileURLWithPath: "https://www.instagram.com/(myusername)/?hl=de")
    if UIApplication.shared.canOpenURL(instaurl) {
        UIApplication.shared.open(instaurl, options: [:] ) { (sucess) in
            print("url opened")
        }
    }
}

除了“在日志中打開了網址”外,它什么也沒告訴我

有什么解決方案? 先感謝您

您使用了錯誤的API

  • URL(fileURLWithPath用於以/開頭的文件系統URL
  • URL(string用於以方案開頭的URL( https://file://

而且myusername的字符串插值看起來錯誤(在查詢問號之前缺少反斜杠和多余的斜杠)。

    if let instaurl = URL(string: "https://www.instagram.com/\(myusername)?hl=de"),
       UIApplication.shared.canOpenURL(instaurl) { ...

您可以嘗試一下。

URL(fileURLWithPath:)用於獲取以/開頭的文件

URL(string:)是創建網址

func openinstagram () {
    if let instaurl = URL(string: "https://www.instagram.com/(myusername)/?hl=de"),
        UIApplication.shared.canOpenURL(instaurl) {

        if #available(iOS 10.0, *) {
            UIApplication.shared.open(instaurl)
        } else {
            UIApplication.shared.openURL(instaurl)
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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