我要求在我的应用程序中打开推文,如果用户安装了Twitter,在Twitter中打开,否则会显示webview并呈现推文。
我能够通过以下方式实现这一目标。它有效,我很高兴。
但是,当最初提示在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(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
}
}
application.open有一个可选的完成处理程序:
application.open(appURL) { (success) in
print("Success \(success)")
}
您应该检查成功状态。