繁体   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