当提示打开另一个应用程序时,如何处理用户单击取消

How can I handle a user clicking cancel when prompted to open another app

我需要在我的应用程序中打开推文,如果用户安装了 Twitter,则在 Twitter 中打开,否则显示 Web 视图并呈现推文。

我基本上可以通过以下方式实现这一目标。它有效,我很满意。

但是,当最初提示在 Twitter 中打开时,如果用户单击取消,我想改为显示 webview。但是目前,如果用户点击取消,则什么也不会发生,他们需要再次点击提要中的推文项目。

如果用户在消息中单击取消,是否可以回退?

   func didSelectItemInFeed(_ selected: FeedItem) {
        switch selected.item.type {
        case .companyNews:
           ....
        case .tweet:
            guard
                let username = selected.item.tweet?.displayName,
                let appURL = URL(string: "twitter://status?id=\(selected.item.externalId)"),
                let webURL = URL(string: "https://twitter.com/\(username)/status/\(selected.item.externalId)")
                else { return }

            let application = UIApplication.shared

            if application.canOpenURL(appURL as URL) {
                application.open(appURL as URL)
            } else {
                presentWebView(webURL)
            }
        default:
            break
        }
    }

application.open 有一个可选的完成处理程序:

application.open(appURL) { (success) in
   print("Success \(success)")
}

您应该检查成功状态。

完成你的函数:application.open(appURL as URL, completionHandler: {isSuccess in})()

    func didSelectItemInFeed(_ selected: FeedItem) {
        switch selected.item.type {
        case .companyNews:
            ....
        case .tweet:
            guard
                let username = selected.item.tweet?.displayName,
                let appURL = URL(string: "twitter://status?id=\(selected.item.externalId)"),
                let webURL = URL(string: "https://twitter.com/\(username)/status/\(selected.item.externalId)")
                else { return }

            let application = UIApplication.shared

            if application.canOpenURL(appURL as URL) {
                application.open(appURL as URL, completionHandler: { isSuccess in
                    // print here does your handler open/close : check 'isSuccess'
                })()
            } else {
                presentWebView(webURL)
            }
        default:
            break
        }
    }