Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 当提示用户打开另一个应用程序时,如何处理单击“取消”的用户_Ios_Swift_Uiapplication - Fatal编程技术网

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

Ios 当提示用户打开另一个应用程序时,如何处理单击“取消”的用户,ios,swift,uiapplication,Ios,Swift,Uiapplication,我需要在我的应用程序中打开一条tweet,如果用户安装了twitter,请在twitter中打开,否则请显示一个webview并呈现该tweet 我基本上可以通过以下方法实现这一点。它起作用了,我很高兴 但是,当最初提示用户在Twitter中打开时,如果用户单击“取消”,我会显示webview。但是,当前如果用户单击“取消”,则不会发生任何事情,他们需要再次点击那里提要中的tweet项目 如果用户在消息中单击“取消”,是否可能有回退 func didSelectItemInFeed(_ s

我需要在我的应用程序中打开一条tweet,如果用户安装了twitter,请在twitter中打开,否则请显示一个webview并呈现该tweet

我基本上可以通过以下方法实现这一点。它起作用了,我很高兴

但是,当最初提示用户在Twitter中打开时,如果用户单击“取消”,我会显示webview。但是,当前如果用户单击“取消”,则不会发生任何事情,他们需要再次点击那里提要中的tweet项目

如果用户在消息中单击“取消”,是否可能有回退

   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作为URL,completionHandler:{issuccessin})(

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