3DTouch的分享菜单是上架后系统自动加上的,其他的菜单需要我们自定义。
1.定义菜单项Model
import Foundation
enum ShortcutItem {
case scan
case send
case receive
case qrcode
var type: String {
switch self {
case .scan:
return "com.app.scan"
case .send:
return "com.app.send"
case .receive:
return "com.app.receive"
case .qrcode:
return "com.app.qrcode"
}
}
var title: String {
switch self {
case .scan:
return "Scan".localized
case .send:
return "Send".localized
case .receive:
return "Receive".localized
case .qrcode:
return "My referral code".localized
}
}
var imageName: String {
switch self {
case .scan:
return "shortcut_scan"
case .send:
return "shortcut_send"
case .receive:
return "shortcut_receive"
case .qrcode:
return "shortcut_qrcode"
}
}
var icon: UIApplicationShortcutIcon {
return UIApplicationShortcutIcon(templateImageName: self.imageName)
}
}
2.在APPDelegate中设置ShortcutItems
func createShortcutItems() {
if #available(iOS 9.1, *) {
var itemArray: [UIApplicationShortcutItem] = []
for item in [ShortcutItem.scan, ShortcutItem.send, ShortcutItem.receive, ShortcutItem.qrcode] {
itemArray.append(UIApplicationShortcutItem(type: item.type, localizedTitle: item.title, localizedSubtitle: nil, icon: item.icon, userInfo: nil))
}
UIApplication.shared.shortcutItems = itemArray
}
}
3.实现点击Item事件
func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
guard let main = appDelegate.coordinator.coordinators.first as? MainCoordinator else { return }
switch shortcutItem.type {
case ShortcutItem.scan.type:
print("scan")
case ShortcutItem.send.type:
print("send")
case ShortcutItem.receive.type:
print("receive")
case ShortcutItem.qrcode.type:
print("qrcode")
default:
break
}
}