mirror of
https://github.com/Finb/Bark.git
synced 2025-12-08 21:36:01 +00:00
调整代码
This commit is contained in:
parent
3cf72b107e
commit
294f8224f0
@ -104,80 +104,17 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
|
||||
}
|
||||
|
||||
private func notificatonHandler(userInfo: [AnyHashable: Any]) {
|
||||
let viewController = Client.shared.currentSnackbarController
|
||||
func presentController() {
|
||||
let alert = (userInfo["aps"] as? [String: Any])?["alert"] as? [String: Any]
|
||||
let title = alert?["title"] as? String
|
||||
let subtitle = alert?["subtitle"] as? String
|
||||
let body = alert?["body"] as? String
|
||||
let url: URL? = {
|
||||
if let url = userInfo["url"] as? String {
|
||||
return URL(string: url)
|
||||
}
|
||||
return nil
|
||||
}()
|
||||
|
||||
if let action = userInfo["action"] as? String, action == "none" {
|
||||
return
|
||||
}
|
||||
|
||||
// URL 直接打开
|
||||
if let url = url {
|
||||
Client.shared.openUrl(url: url)
|
||||
return
|
||||
}
|
||||
|
||||
let alertController = UIAlertController(title: title, message: body, preferredStyle: .alert)
|
||||
alertController.addAction(UIAlertAction(title: NSLocalizedString("CopyContent"), style: .default, handler: { _ in
|
||||
if let copy = userInfo["copy"] as? String {
|
||||
UIPasteboard.general.string = copy
|
||||
} else {
|
||||
UIPasteboard.general.string = body
|
||||
}
|
||||
}))
|
||||
alertController.addAction(UIAlertAction(title: NSLocalizedString("MoreActions"), style: .default, handler: { _ in
|
||||
var shareContent = ""
|
||||
if let title = title {
|
||||
shareContent += "\(title)\n"
|
||||
}
|
||||
if let subtitle = subtitle {
|
||||
shareContent += "\(subtitle)\n"
|
||||
}
|
||||
if let body = body {
|
||||
shareContent += "\(body)\n"
|
||||
}
|
||||
for (key, value) in userInfo {
|
||||
if ["aps", "title", "subtitle", "body", "url"].contains((key as? String) ?? "") {
|
||||
continue
|
||||
}
|
||||
shareContent += "\(key): \(value) \n"
|
||||
}
|
||||
var items: [Any] = []
|
||||
items.append(shareContent)
|
||||
if let url = url {
|
||||
items.append(url)
|
||||
}
|
||||
let controller = Client.shared.window?.rootViewController
|
||||
let activityController = UIActivityViewController(activityItems: items,
|
||||
applicationActivities: nil)
|
||||
if let popover = activityController.popoverPresentationController {
|
||||
popover.sourceView = controller?.view
|
||||
popover.sourceRect = CGRect(x: controller?.view.bounds.midX ?? 0, y: controller?.view.bounds.midY ?? 0, width: 0, height: 0)
|
||||
}
|
||||
controller?.present(activityController, animated: true, completion: nil)
|
||||
}))
|
||||
alertController.addAction(UIAlertAction(title: NSLocalizedString("Cancel"), style: .cancel, handler: nil))
|
||||
|
||||
viewController?.present(alertController, animated: true, completion: nil)
|
||||
if let action = userInfo["action"] as? String, action == "none" {
|
||||
return
|
||||
}
|
||||
|
||||
if let presentedController = viewController?.presentedViewController {
|
||||
presentedController.dismiss(animated: false) {
|
||||
presentController()
|
||||
}
|
||||
} else {
|
||||
presentController()
|
||||
|
||||
// URL 直接打开
|
||||
if let url = try? (userInfo["url"] as? String)?.asURL() {
|
||||
Client.shared.openUrl(url: url)
|
||||
return
|
||||
}
|
||||
|
||||
alertNotification(userInfo: userInfo)
|
||||
}
|
||||
|
||||
func applicationWillResignActive(_ application: UIApplication) {
|
||||
@ -219,3 +156,64 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
extension AppDelegate {
|
||||
func alertNotification(userInfo: [AnyHashable: Any]) {
|
||||
let alert = (userInfo["aps"] as? [String: Any])?["alert"] as? [String: Any]
|
||||
let title = alert?["title"] as? String
|
||||
let subtitle = alert?["subtitle"] as? String
|
||||
let body = alert?["body"] as? String
|
||||
let url = try? (userInfo["url"] as? String)?.asURL()
|
||||
|
||||
let alertController = UIAlertController(title: title, message: body, preferredStyle: .alert)
|
||||
alertController.addAction(UIAlertAction(title: NSLocalizedString("CopyContent"), style: .default, handler: { _ in
|
||||
if let copy = userInfo["copy"] as? String {
|
||||
UIPasteboard.general.string = copy
|
||||
} else {
|
||||
UIPasteboard.general.string = body
|
||||
}
|
||||
}))
|
||||
alertController.addAction(UIAlertAction(title: NSLocalizedString("MoreActions"), style: .default, handler: { _ in
|
||||
var shareContent = ""
|
||||
if let title = title {
|
||||
shareContent += "\(title)\n"
|
||||
}
|
||||
if let subtitle = subtitle {
|
||||
shareContent += "\(subtitle)\n"
|
||||
}
|
||||
if let body = body {
|
||||
shareContent += "\(body)\n"
|
||||
}
|
||||
for (key, value) in userInfo {
|
||||
if ["aps", "title", "subtitle", "body", "url"].contains((key as? String) ?? "") {
|
||||
continue
|
||||
}
|
||||
shareContent += "\(key): \(value) \n"
|
||||
}
|
||||
var items: [Any] = []
|
||||
items.append(shareContent)
|
||||
if let url = url {
|
||||
items.append(url)
|
||||
}
|
||||
let controller = Client.shared.window?.rootViewController
|
||||
let activityController = UIActivityViewController(activityItems: items,
|
||||
applicationActivities: nil)
|
||||
if let popover = activityController.popoverPresentationController {
|
||||
popover.sourceView = controller?.view
|
||||
popover.sourceRect = CGRect(x: controller?.view.bounds.midX ?? 0, y: controller?.view.bounds.midY ?? 0, width: 0, height: 0)
|
||||
}
|
||||
controller?.present(activityController, animated: true, completion: nil)
|
||||
}))
|
||||
alertController.addAction(UIAlertAction(title: NSLocalizedString("Cancel"), style: .cancel, handler: nil))
|
||||
|
||||
let viewController = Client.shared.currentSnackbarController
|
||||
|
||||
if let presentedController = viewController?.presentedViewController {
|
||||
presentedController.dismiss(animated: false) {
|
||||
viewController?.present(alertController, animated: true, completion: nil)
|
||||
}
|
||||
} else {
|
||||
viewController?.present(alertController, animated: true, completion: nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user