无法快速获取Firebase动态链接

时间:2019-11-29 02:52:51

标签: swift firebase firebase-dynamic-links

我为我的firebase项目设置了动态链接,每个帖子都有一个动态链接。我可以将链接复制到Safari中,它会显示标题和所有正确的信息,然后可以从链接中打开应用程序,但是由于某些原因,单击链接时我无法显示某个控制器,而我无法从下面的功能中打印出任何内容。接收链接时遇到严重麻烦。

ViewController-创建(效果很好!)

var components = URLComponents()
        components.scheme = "https"
        components.host = "www.example.com.au"
        components.path = "/posts"

        let postIdQueryItem = URLQueryItem(name: "postId", value: post.postId)
        components.queryItems = [postIdQueryItem]

        guard let linkParam = components.url else { return }

        guard let shareLink = DynamicLinkComponents(link: linkParam, domainURIPrefix: "https://example.page.link") else { return }
        let options = DynamicLinkComponentsOptions()

        options.pathLength = .short

        guard let bundleId = Bundle.main.bundleIdentifier else { return }
        shareLink.options = options
        shareLink.iOSParameters = DynamicLinkIOSParameters(bundleID: bundleId)

        shareLink.iOSParameters?.appStoreID = "1456561852"
        shareLink.socialMetaTagParameters = DynamicLinkSocialMetaTagParameters()
        shareLink.socialMetaTagParameters?.title = "\(post.title)."
        shareLink.socialMetaTagParameters?.descriptionText = post.details

        guard let longUrl = shareLink.url else { return }
        shareLink.shorten { (url, warning, error) in
            if let error = error {
                print(error.localizedDescription)
            }

            guard let shortUrl = url else { return }
            print(shortUrl)
            self.showShareSheet(url: shortUrl)
        }

AppDelegate-接收中

    func handleIncomingDynamicLink(_ dynamicLink: DynamicLink) {
    guard let url = dynamicLink.url else { return }
    print("Incoming link is \(url.absoluteString)")

    guard let components = URLComponents(url: url, resolvingAgainstBaseURL: false) else { return }
    guard let queryItems = components.queryItems else { return }

    if components.path == "/posts" {
        //Load a specific event
        if let postIdQueryItem = queryItems.first(where: {$0.name == "postId"}) {
            guard let postId = postIdQueryItem.value else { return }
            let homeController = HomeController()
            homeController.presentAlert()
        }
    }
}

func application(_ application: UIApplication, contuine userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool {
    if let incomingUrl = userActivity.webpageURL {
        print("Incoming URL is \(incomingUrl)")
        let linkHandled = DynamicLinks.dynamicLinks().handleUniversalLink(incomingUrl) { (dynamicLink, error) in

            if let error = error {
                return
            }

            if let dynamicLink = dynamicLink {
                self.handleIncomingDynamicLink(dynamicLink)
            }
        }
        return linkHandled
    }
    return false
}


func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
    if let dynamicLink = DynamicLinks.dynamicLinks().dynamicLink(fromCustomSchemeURL: url) {
        self.handleIncomingDynamicLink(dynamicLink)
        return true
    } else {
        return false
    }
}

0 个答案:

没有答案