Firebase Dynamic Links non accorcia l'URL
Sto cercando di ottenere collegamenti dinamici per abbreviare il mio URL con il seguente codice:
guard let link = URL(string: "https://myapp.com") else { return }
let dynamicLinksDomainURIPrefix = "https://app.myapp.com/link"
let linkBuilder = DynamicLinkComponents(link: link, domainURIPrefix: dynamicLinksDomainURIPrefix)
linkBuilder?.iOSParameters = DynamicLinkIOSParameters(bundleID: "com.myapp.ios")
guard let longDynamicLink = linkBuilder?.url else { return }
print("The long URL is: \(longDynamicLink)")
let options = DynamicLinkComponentsOptions()
options.pathLength = .short
linkBuilder?.options = options
linkBuilder?.shorten() { url, warnings, error in
guard let url = url, error != nil else { return }
print("The short URL is: \(url)")
}
Stampa correttamente l'URL lungo, ma la riga seguente (per l'URL breve) non viene mai chiamata:
print("The short URL is: \(url)")
Perché urlrestituisce zero e non ho idea del perché. Niente di quello che ho trovato nelle guide o online mi ha portato nella giusta direzione.
Che cosa sto facendo di sbagliato??
Risposte
Penso che sia perché quanto segue non è corretto:
guard let url = url, error != nil else { return }
Stai dicendo che ci sia un URL non nullo e che ci sia un errore.
Penso che i documenti di Firebase siano sbagliati. Invece, vuoi:
guard let url = url, error == nil else { return }
Cosa hai fatto qui:
linkBuilder?.shorten() { url, warnings, error in
guard let url = url, error != nil else { return }
print("The short URL is: \(url)")
}
se stai scartando l'URL e controllando se l'errore contiene qualche errore, quindi stai stampando "L'URL corto è: (url)" significa che se shorten() riesce e non c'è alcun errore il tuo metodo di stampa non verrà mai eseguito.
Quello che devi fare è, prima controlla se l'errore non contiene alcun errore che chiama print()
linkBuilder?.shorten() { url, warnings, error in
guard error == nil else { return }
if let shortUrl = url {
print("The short url is \(shortUrl)")
}
}