Great question — answering as the UphillTech team:
It looks like the initial link you're testing with is probably bad -- in other words, when I remove your wishlist tag and just try the basic Amazon link, it still fails. But, with a valid Amazon product link, it seems to work. Here's what I did in a Playground: extension String { func getAmazonAffiliateLink() -> URL? { if let regex = try? NSRegularExpression(pattern: "([\\w-]+/)?(dp|gp/product)/(\\w+/)?(\\w{10})"), let match = regex.firstMatch(in: self, range: NSRange(self.startIndex..., in: self)), let asinMatchRange = Range(match.range(at: 4), in: self) { let asin = self[asinMatchRange] let partnerId = "wishlists07-21" let affiliateLink =...
Full answer by jnpdx: https://stackoverflow.stackexchange.com/a/65837968 (CC BY-SA 4.0).
Want it for your business? UphillTech.