Reinhard Männer
Reinhard Männer

Reputation: 15247

When tapping a custom URL link in a UITextView the delegate receives nil URL

I have a tableView with tableViewCells cell that show a textView.
textView uses an attributedString with custom URL link information, set up in tableView:cellForRowAtIndexPath: as shown in this tutorial:

NSMutableAttributedString *attributedDisplayString = [[NSMutableAttributedString alloc] initWithString:displayString];
[attributedDisplayString addAttribute:NSLinkAttributeName 
                                value:[NSString stringWithFormat:@"username://%@", userName] 
                                range:NSMakeRange(0, userName.length)];
cell.textView.attributedText = attributedDisplayString;

When I tap the link, textView:shouldInteractWithURL:inRange: is called in the delegate, thus the custom URL link has been detected and responds.

However, the supplied URL is nil.

What am I missing?

Upvotes: 1

Views: 598

Answers (1)

Reinhard Männer
Reinhard Männer

Reputation: 15247

Sorry for asking too fast. I found the problem, but maybe this helps others:

My variable userName simply contained a space, and could thus not be converted to a URL.

After removing the space, it works.

To make a string that can be used for a URL, one can use in iOS8 and earlier
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding

and in iOS9

stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]

Upvotes: 1

Related Questions