Opening app from my app

There's a dictionary app (Logeion) that I want my app to open. When the user hightlights a word in my app, and presses the Dictionary toolbar button, the dictionary app should open with the dictionary entry for the highlighted word displayed.


This is exactly what happens so long as the highlighted word has only ascii characters. But when the word has unicode characters, the dictionary opens, but the entry for the word is not displayed.


I know that URLs will not accept unicode characters as-is, so I tried escaping them with this code:


let escapedString = selectedText.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) ?? ""
let logeionURL = URL(string: "LogeionLookUp://" + escapedString)
if let url = logeionURL {
  UIApplication.shared.open(url, options: [:], completionHandler: {success in print(url, "\n", success)})
}


The selectedText variable is a String that holds the text that the user has highlighted. (Yes, I've checked.) I've tried a few other values besides .urlPathAllowed in line #1, but without effect. I know that the url is valid, because the app does open and the completionHandler runs, even with unicode strings.


Is there some other way to get uincode characters into the URL? I know that the dictionary app will work with unicode characters, because I've seen another app do precisely what I want my app to do with them. Apart from "Ask the app developer how they did it", any advice would be appreciated!

Accepted Reply

You should, in general, avoid working with URLs as if they were strings. A URL is a complex *****, and it’s easy to run into problems if you treat it as a string.

Fortunately, Swift has you covered here, with the

URLComponents
type. This allows you to parse a URL into components and also build a URL from its components. For example:
var uc = URLComponents(string: "LogeionLookUp://***")!
uc.host = "naïve"
print(uc.url!)  // -> LogeionLookUp://na%C3%AFve

where

%C3%AF
is the percent encoded UTF-8 of
ï
.

Now, whether that’s what this app is expecting… *shrug*… the only way to know that is to ask the developer.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Replies

The escapingString contains the query ?


So, I guess it should use

urlQueryAllowed
.


Parameters

allowedCharacters


The characters not replaced in the string. Typically, you specify one of the predefined character sets for a particular URL component, such as

urlPathAllowed
or
urlQueryAllowed
.


You should, in general, avoid working with URLs as if they were strings. A URL is a complex *****, and it’s easy to run into problems if you treat it as a string.

Fortunately, Swift has you covered here, with the

URLComponents
type. This allows you to parse a URL into components and also build a URL from its components. For example:
var uc = URLComponents(string: "LogeionLookUp://***")!
uc.host = "naïve"
print(uc.url!)  // -> LogeionLookUp://na%C3%AFve

where

%C3%AF
is the percent encoded UTF-8 of
ï
.

Now, whether that’s what this app is expecting… *shrug*… the only way to know that is to ask the developer.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Yes, you've given me the solution once again! Here's the code that works:


var uc = URLComponents(string: "LogeionLookUp://")!
uc.host = selectedText
if let url = uc.url {
  UIApplication.shared.open(url, options: [:], completionHandler: {success in print(url, "\n", success)})
}


I'm going to name my first-born child after you. (Harold gets mad when I change her name, but as long as I'm paying her college tuition, she'll have to put up with it.)