2

I have added "instagram" to my LSApplicationQueriesSchemes in the Info.plist file but it still doesn't open up neither safari nor instagram when the following function is called:

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")
        }
    }
}

It doesn't tell me anything but "url opened" in the Log

What is the solution to this? Thank you in advance

2 Answers2

2

You can try out this.

URL(fileURLWithPath:) use to get file starting with /

URL(string:) is create web url

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)
        }
    }
}
AshvinGudaliya
  • 3,234
  • 19
  • 37
0

You are using the wrong API

  • URL(fileURLWithPath is for file system URLs starting with /
  • URL(string is for URLs starting with a scheme (https://, file://)

And the string interpolation of myusername looks wrong (missing backslash and extraneous slash before the query question mark).

    if let instaurl = URL(string: "https://www.instagram.com/\(myusername)?hl=de"),
       UIApplication.shared.canOpenURL(instaurl) { ...
vadian
  • 274,689
  • 30
  • 353
  • 361