I would like to be able to build an Amazon-Affiliate link, when a "normal" Amazon URL
is given.
I tried using this structure:
http://www.amazon.com/dp/{ASIN}/?tag={trackingId}
as suggested here.
This is how I try to create the URL
:
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 = "https://www.amazon.com/dp/(asin)/?tag=(partnerId)"
return URL(string: affiliateLink)
} else {
return nil
}
}
But this gives me this URL
for example:
And as you can see for yourself, it is not working...
What am I missing here? How can I build affiliate links?
question from:https://stackoverflow.com/questions/65837717/build-amazon-affiliate-link-swift