Hergen Lehmann
Hergen Lehmann

Reputation: 23

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

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

Upvotes: 2

Views: 8596

Answers (3)

Dinesh Kumar
Dinesh Kumar

Reputation: 151

XCode 15 & iOS 17.2

Below Sample code for Uber App.

Add below scheme in your PList file:

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>uber</string>
</array>

Use below code to open the app:

let appURLScheme = "uber://"
    
guard let appURL = URL(string: appURLScheme) else {
    return
}
    
if UIApplication.shared.canOpenURL(appURL) {
   UIApplication.shared.open(appURL)
}

Upvotes: -1

AshvinGudaliya
AshvinGudaliya

Reputation: 3314

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

Upvotes: 2

vadian
vadian

Reputation: 285250

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) { ...

Upvotes: 0

Related Questions