简体   繁体   English

Firebase 动态链接不会缩短网址

[英]Firebase Dynamic Links not shortening URL

I'm trying to get Dynamic Links to shorten my URL with the following code:我正在尝试使用以下代码让动态链接缩短我的 URL:

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)")
}

It's printing the long URL fine, but the line below (for short URL) is never being called:它可以很好地打印长 URL,但永远不会调用下面的行(对于短 URL):

print("The short URL is: \(url)")

Because url returns nil and I have no idea why.因为url返回 nil 而我不知道为什么。 Nothing I've found in the guides or online has lead me in the right direction.我在指南或网上找到的任何东西都没有引导我走向正确的方向。

What am I doing wrong??我究竟做错了什么??

I think it is because the following is incorrect:我认为是因为以下不正确:

guard let url = url, error != nil else { return }

You are saying make sure there is a non nil URL and make sure there is an error.您是说确保有一个非零 URL 并确保有错误。

I think the Firebase docs are wrong.我认为 Firebase 文档是错误的。 Instead, you want:相反,您想要:

guard let url = url, error == nil else { return }

What you have done here :你在这里做了什么:

linkBuilder?.shorten() { url, warnings, error in
  guard let url = url, error != nil else { return }
  print("The short URL is: \(url)")
}

is you are unwrapping the url and checking if error contains some Error, then you are printing 'The short URL is: (url)' it means if shorten() succeeds and there's no Error your print method will never be executed.您是否正在解开 url 并检查错误是否包含一些错误,然后您正在打印“短 URL 是:(url)”,这意味着如果short ()成功并且没有错误,您的打印方法将永远不会被执行。

What you have to do is, First check if error doesn't contain any Error than call print()您需要做的是,首先检查错误是否不包含任何错误,而不是调用 print()

linkBuilder?.shorten() { url, warnings, error in
  guard error == nil else { return }
  if let shortUrl = url {
      print("The short url is \(shortUrl)")
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM