2

Context

I'm working on a Swift iOS app in Xcode (iOS 13 and Xcode 11.3).

Goal

Basically, from within my app, I want to be able to open a private podcast feed's URL in the iOS Podcasts app (to add it as a new subscription).

What I'm doing

My understanding is that the way of doing this is by using the open(_:options:completionHandler:) method defined on the UIApplication class and pre-fixing my URL with podcast://. Here's an example:

// My URL
let url = URL(string: "podcast://https://username:password@example.com/private-podcast-feed")!

// From inside a `UIViewController` method...
UIApplication.shared.open(url, options: [:]) { (success: Bool) in
   // ... handle the result
}

The problem

The code above does open the iOS Podcasts app and trigger an Add a Show by URL... alert box with its input pre-filled, but the original URL is now prefixed with http://; i.e. http://https://username:password@example.com/private-podcast-feed (see attached screen capture at the bottom for an example).

From a "user experience" standpoint, this behaviour is awful, since most people won't notice and the operation will fail.

A potential way around the problem would be to omit the https:// protocol inside my app and use the pre-filled http:// and have a 301 Redirect on my server (I didn't try it to see if Podcasts follows redirects though), but this is not good for my use-case, since my URL contains credentials.

My question

Does anyone know about a way around this?

enter image description here

Blazej SLEBODA
  • 8,936
  • 7
  • 53
  • 93
focorner
  • 1,927
  • 1
  • 20
  • 24

1 Answers1

1

It turns out that I was not using the correct syntax when defining my URL instance in my app. Instead of using podcast://https..., I should have been using podcast:https..., without the two forward slashes.

So here's the working example (i.e. the example that will open the Podcasts app and not prepend http to the already present https scheme):

// My URL
let url = URL(string: "podcast:https://username:password@example.com/private-podcast-feed")!

// From inside a `UIViewController` method...
UIApplication.shared.open(url, options: [:]) { (success: Bool) in
   // ... handle the result
}

What initially got me confused is that the "double forward slash" syntax is required when creating an hyperlink to be opened by an app using URL schemes in iOS. For instance, if I share a link inside an email, then I need to specify podcast://{my-url}. But if I want to open an app using one of its URL schemes from inside and other app, then I need to exclude those two slashes, else the command might confuse the target app (note: I don't have any documentation for this, but this is what I observed for this particular scenario).

I really just stumbled on the answer while working on the opposite problem: I was trying to define my own URL schemes for one of my iOS apps, and I say the syntax used in the documentation (see Defining a Custom URL Scheme for Your App).

focorner
  • 1,927
  • 1
  • 20
  • 24