├── .gitignore ├── README.md ├── UIAlertController+KeyCommands.xcodeproj ├── project.pbxproj └── project.xcworkspace │ └── contents.xcworkspacedata ├── UIAlertController+KeyCommands ├── AppDelegate.swift ├── Assets.xcassets │ └── AppIcon.appiconset │ │ └── Contents.json ├── Base.lproj │ ├── LaunchScreen.storyboard │ └── Main.storyboard ├── Info.plist ├── KeyCommandAlertController.swift └── ViewController.swift └── readme-resources └── example.gif /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | .DS_Store 4 | build/ 5 | *.pbxuser 6 | !default.pbxuser 7 | *.mode1v3 8 | !default.mode1v3 9 | *.mode2v3 10 | !default.mode2v3 11 | *.perspectivev3 12 | !default.perspectivev3 13 | xcuserdata 14 | *.xccheckout 15 | *.moved-aside 16 | DerivedData 17 | *.hmap 18 | *.ipa 19 | *.xcuserstate 20 | 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # KeyCommandAlertController 2 | This project contains wrappers for ```UIAlertController``` and ```UIAlertAction``` to easily add keyboard shortcuts (known as ```key command```). 3 | 4 | ![](readme-resources/example.gif) 5 | 6 | The following example shows how the action sheet shown above is created: 7 | 8 | ```swift 9 | let alert = KeyCommandAlertController(title: "Change background color", message: nil, preferredStyle: .actionSheet) 10 | 11 | alert.popoverPresentationController?.barButtonItem = sender 12 | 13 | let redShortcut = KeyCommandShortcut(input: "B", modifierFlags: .command) 14 | let redAction = KeyCommandAlertAction(title: "Blue", style: .default, keyShortcut: redShortcut) { (action) in 15 | self.makeBlue() 16 | } 17 | 18 | let greenShortcut = KeyCommandShortcut(input: "R", modifierFlags: .command) 19 | let greenAction = KeyCommandAlertAction(title: "Red", style: .default, keyShortcut: greenShortcut) { (action) in 20 | self.makeRed() 21 | } 22 | 23 | let cancelAction = KeyCommandAlertAction(title: "Cancel", style: .cancel) 24 | 25 | alert.addAction(redAction) 26 | alert.addAction(greenAction) 27 | alert.addAction(cancelAction) 28 | 29 | present(alert, animated: true, completion: nil) 30 | ``` 31 | 32 | 33 | ### TODO 34 | - [ ] Only show shortcuts in alert actions when external keyboard is connected -------------------------------------------------------------------------------- /UIAlertController+KeyCommands.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | BEBFA5BF1E7DD25B0099378D /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = BEBFA5BE1E7DD25B0099378D /* AppDelegate.swift */; }; 11 | BEBFA5C11E7DD25B0099378D /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = BEBFA5C01E7DD25B0099378D /* ViewController.swift */; }; 12 | BEBFA5C41E7DD25B0099378D /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = BEBFA5C21E7DD25B0099378D /* Main.storyboard */; }; 13 | BEBFA5C61E7DD25B0099378D /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = BEBFA5C51E7DD25B0099378D /* Assets.xcassets */; }; 14 | BEBFA5C91E7DD25C0099378D /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = BEBFA5C71E7DD25B0099378D /* LaunchScreen.storyboard */; }; 15 | BEBFA5D11E7DE5390099378D /* KeyCommandAlertController.swift in Sources */ = {isa = PBXBuildFile; fileRef = BEBFA5D01E7DE5390099378D /* KeyCommandAlertController.swift */; }; 16 | /* End PBXBuildFile section */ 17 | 18 | /* Begin PBXFileReference section */ 19 | BEBFA5BB1E7DD25B0099378D /* UIAlertController+KeyCommands.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "UIAlertController+KeyCommands.app"; sourceTree = BUILT_PRODUCTS_DIR; }; 20 | BEBFA5BE1E7DD25B0099378D /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 21 | BEBFA5C01E7DD25B0099378D /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; 22 | BEBFA5C31E7DD25B0099378D /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 23 | BEBFA5C51E7DD25B0099378D /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 24 | BEBFA5C81E7DD25B0099378D /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 25 | BEBFA5CA1E7DD25C0099378D /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 26 | BEBFA5D01E7DE5390099378D /* KeyCommandAlertController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = KeyCommandAlertController.swift; sourceTree = ""; }; 27 | /* End PBXFileReference section */ 28 | 29 | /* Begin PBXFrameworksBuildPhase section */ 30 | BEBFA5B81E7DD25B0099378D /* Frameworks */ = { 31 | isa = PBXFrameworksBuildPhase; 32 | buildActionMask = 2147483647; 33 | files = ( 34 | ); 35 | runOnlyForDeploymentPostprocessing = 0; 36 | }; 37 | /* End PBXFrameworksBuildPhase section */ 38 | 39 | /* Begin PBXGroup section */ 40 | BEBFA5B21E7DD25B0099378D = { 41 | isa = PBXGroup; 42 | children = ( 43 | BEBFA5BD1E7DD25B0099378D /* UIAlertController+KeyCommands */, 44 | BEBFA5BC1E7DD25B0099378D /* Products */, 45 | ); 46 | sourceTree = ""; 47 | }; 48 | BEBFA5BC1E7DD25B0099378D /* Products */ = { 49 | isa = PBXGroup; 50 | children = ( 51 | BEBFA5BB1E7DD25B0099378D /* UIAlertController+KeyCommands.app */, 52 | ); 53 | name = Products; 54 | sourceTree = ""; 55 | }; 56 | BEBFA5BD1E7DD25B0099378D /* UIAlertController+KeyCommands */ = { 57 | isa = PBXGroup; 58 | children = ( 59 | BEBFA5BE1E7DD25B0099378D /* AppDelegate.swift */, 60 | BEBFA5C01E7DD25B0099378D /* ViewController.swift */, 61 | BEBFA5D01E7DE5390099378D /* KeyCommandAlertController.swift */, 62 | BEBFA5C21E7DD25B0099378D /* Main.storyboard */, 63 | BEBFA5C51E7DD25B0099378D /* Assets.xcassets */, 64 | BEBFA5C71E7DD25B0099378D /* LaunchScreen.storyboard */, 65 | BEBFA5CA1E7DD25C0099378D /* Info.plist */, 66 | ); 67 | path = "UIAlertController+KeyCommands"; 68 | sourceTree = ""; 69 | }; 70 | /* End PBXGroup section */ 71 | 72 | /* Begin PBXNativeTarget section */ 73 | BEBFA5BA1E7DD25B0099378D /* UIAlertController+KeyCommands */ = { 74 | isa = PBXNativeTarget; 75 | buildConfigurationList = BEBFA5CD1E7DD25C0099378D /* Build configuration list for PBXNativeTarget "UIAlertController+KeyCommands" */; 76 | buildPhases = ( 77 | BEBFA5B71E7DD25B0099378D /* Sources */, 78 | BEBFA5B81E7DD25B0099378D /* Frameworks */, 79 | BEBFA5B91E7DD25B0099378D /* Resources */, 80 | ); 81 | buildRules = ( 82 | ); 83 | dependencies = ( 84 | ); 85 | name = "UIAlertController+KeyCommands"; 86 | productName = "UIAlertController+KeyCommands"; 87 | productReference = BEBFA5BB1E7DD25B0099378D /* UIAlertController+KeyCommands.app */; 88 | productType = "com.apple.product-type.application"; 89 | }; 90 | /* End PBXNativeTarget section */ 91 | 92 | /* Begin PBXProject section */ 93 | BEBFA5B31E7DD25B0099378D /* Project object */ = { 94 | isa = PBXProject; 95 | attributes = { 96 | LastSwiftUpdateCheck = 0820; 97 | LastUpgradeCheck = 0820; 98 | ORGANIZATIONNAME = "Silver Fox"; 99 | TargetAttributes = { 100 | BEBFA5BA1E7DD25B0099378D = { 101 | CreatedOnToolsVersion = 8.2.1; 102 | DevelopmentTeam = 6G5LMQ72D8; 103 | ProvisioningStyle = Automatic; 104 | }; 105 | }; 106 | }; 107 | buildConfigurationList = BEBFA5B61E7DD25B0099378D /* Build configuration list for PBXProject "UIAlertController+KeyCommands" */; 108 | compatibilityVersion = "Xcode 3.2"; 109 | developmentRegion = English; 110 | hasScannedForEncodings = 0; 111 | knownRegions = ( 112 | en, 113 | Base, 114 | ); 115 | mainGroup = BEBFA5B21E7DD25B0099378D; 116 | productRefGroup = BEBFA5BC1E7DD25B0099378D /* Products */; 117 | projectDirPath = ""; 118 | projectRoot = ""; 119 | targets = ( 120 | BEBFA5BA1E7DD25B0099378D /* UIAlertController+KeyCommands */, 121 | ); 122 | }; 123 | /* End PBXProject section */ 124 | 125 | /* Begin PBXResourcesBuildPhase section */ 126 | BEBFA5B91E7DD25B0099378D /* Resources */ = { 127 | isa = PBXResourcesBuildPhase; 128 | buildActionMask = 2147483647; 129 | files = ( 130 | BEBFA5C91E7DD25C0099378D /* LaunchScreen.storyboard in Resources */, 131 | BEBFA5C61E7DD25B0099378D /* Assets.xcassets in Resources */, 132 | BEBFA5C41E7DD25B0099378D /* Main.storyboard in Resources */, 133 | ); 134 | runOnlyForDeploymentPostprocessing = 0; 135 | }; 136 | /* End PBXResourcesBuildPhase section */ 137 | 138 | /* Begin PBXSourcesBuildPhase section */ 139 | BEBFA5B71E7DD25B0099378D /* Sources */ = { 140 | isa = PBXSourcesBuildPhase; 141 | buildActionMask = 2147483647; 142 | files = ( 143 | BEBFA5D11E7DE5390099378D /* KeyCommandAlertController.swift in Sources */, 144 | BEBFA5C11E7DD25B0099378D /* ViewController.swift in Sources */, 145 | BEBFA5BF1E7DD25B0099378D /* AppDelegate.swift in Sources */, 146 | ); 147 | runOnlyForDeploymentPostprocessing = 0; 148 | }; 149 | /* End PBXSourcesBuildPhase section */ 150 | 151 | /* Begin PBXVariantGroup section */ 152 | BEBFA5C21E7DD25B0099378D /* Main.storyboard */ = { 153 | isa = PBXVariantGroup; 154 | children = ( 155 | BEBFA5C31E7DD25B0099378D /* Base */, 156 | ); 157 | name = Main.storyboard; 158 | sourceTree = ""; 159 | }; 160 | BEBFA5C71E7DD25B0099378D /* LaunchScreen.storyboard */ = { 161 | isa = PBXVariantGroup; 162 | children = ( 163 | BEBFA5C81E7DD25B0099378D /* Base */, 164 | ); 165 | name = LaunchScreen.storyboard; 166 | sourceTree = ""; 167 | }; 168 | /* End PBXVariantGroup section */ 169 | 170 | /* Begin XCBuildConfiguration section */ 171 | BEBFA5CB1E7DD25C0099378D /* Debug */ = { 172 | isa = XCBuildConfiguration; 173 | buildSettings = { 174 | ALWAYS_SEARCH_USER_PATHS = NO; 175 | CLANG_ANALYZER_NONNULL = YES; 176 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 177 | CLANG_CXX_LIBRARY = "libc++"; 178 | CLANG_ENABLE_MODULES = YES; 179 | CLANG_ENABLE_OBJC_ARC = YES; 180 | CLANG_WARN_BOOL_CONVERSION = YES; 181 | CLANG_WARN_CONSTANT_CONVERSION = YES; 182 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 183 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 184 | CLANG_WARN_EMPTY_BODY = YES; 185 | CLANG_WARN_ENUM_CONVERSION = YES; 186 | CLANG_WARN_INFINITE_RECURSION = YES; 187 | CLANG_WARN_INT_CONVERSION = YES; 188 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 189 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 190 | CLANG_WARN_UNREACHABLE_CODE = YES; 191 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 192 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 193 | COPY_PHASE_STRIP = NO; 194 | DEBUG_INFORMATION_FORMAT = dwarf; 195 | ENABLE_STRICT_OBJC_MSGSEND = YES; 196 | ENABLE_TESTABILITY = YES; 197 | GCC_C_LANGUAGE_STANDARD = gnu99; 198 | GCC_DYNAMIC_NO_PIC = NO; 199 | GCC_NO_COMMON_BLOCKS = YES; 200 | GCC_OPTIMIZATION_LEVEL = 0; 201 | GCC_PREPROCESSOR_DEFINITIONS = ( 202 | "DEBUG=1", 203 | "$(inherited)", 204 | ); 205 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 206 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 207 | GCC_WARN_UNDECLARED_SELECTOR = YES; 208 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 209 | GCC_WARN_UNUSED_FUNCTION = YES; 210 | GCC_WARN_UNUSED_VARIABLE = YES; 211 | IPHONEOS_DEPLOYMENT_TARGET = 9.0; 212 | MTL_ENABLE_DEBUG_INFO = YES; 213 | ONLY_ACTIVE_ARCH = YES; 214 | SDKROOT = iphoneos; 215 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 216 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 217 | TARGETED_DEVICE_FAMILY = "1,2"; 218 | }; 219 | name = Debug; 220 | }; 221 | BEBFA5CC1E7DD25C0099378D /* Release */ = { 222 | isa = XCBuildConfiguration; 223 | buildSettings = { 224 | ALWAYS_SEARCH_USER_PATHS = NO; 225 | CLANG_ANALYZER_NONNULL = YES; 226 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 227 | CLANG_CXX_LIBRARY = "libc++"; 228 | CLANG_ENABLE_MODULES = YES; 229 | CLANG_ENABLE_OBJC_ARC = YES; 230 | CLANG_WARN_BOOL_CONVERSION = YES; 231 | CLANG_WARN_CONSTANT_CONVERSION = YES; 232 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 233 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 234 | CLANG_WARN_EMPTY_BODY = YES; 235 | CLANG_WARN_ENUM_CONVERSION = YES; 236 | CLANG_WARN_INFINITE_RECURSION = YES; 237 | CLANG_WARN_INT_CONVERSION = YES; 238 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 239 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 240 | CLANG_WARN_UNREACHABLE_CODE = YES; 241 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 242 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 243 | COPY_PHASE_STRIP = NO; 244 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 245 | ENABLE_NS_ASSERTIONS = NO; 246 | ENABLE_STRICT_OBJC_MSGSEND = YES; 247 | GCC_C_LANGUAGE_STANDARD = gnu99; 248 | GCC_NO_COMMON_BLOCKS = YES; 249 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 250 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 251 | GCC_WARN_UNDECLARED_SELECTOR = YES; 252 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 253 | GCC_WARN_UNUSED_FUNCTION = YES; 254 | GCC_WARN_UNUSED_VARIABLE = YES; 255 | IPHONEOS_DEPLOYMENT_TARGET = 9.0; 256 | MTL_ENABLE_DEBUG_INFO = NO; 257 | SDKROOT = iphoneos; 258 | SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; 259 | TARGETED_DEVICE_FAMILY = "1,2"; 260 | VALIDATE_PRODUCT = YES; 261 | }; 262 | name = Release; 263 | }; 264 | BEBFA5CE1E7DD25C0099378D /* Debug */ = { 265 | isa = XCBuildConfiguration; 266 | buildSettings = { 267 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 268 | DEVELOPMENT_TEAM = 6G5LMQ72D8; 269 | INFOPLIST_FILE = "UIAlertController+KeyCommands/Info.plist"; 270 | IPHONEOS_DEPLOYMENT_TARGET = 9.0; 271 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 272 | PRODUCT_BUNDLE_IDENTIFIER = "be.silverfox.UIAlertController-KeyCommands"; 273 | PRODUCT_NAME = "$(TARGET_NAME)"; 274 | SWIFT_VERSION = 3.0; 275 | }; 276 | name = Debug; 277 | }; 278 | BEBFA5CF1E7DD25C0099378D /* Release */ = { 279 | isa = XCBuildConfiguration; 280 | buildSettings = { 281 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 282 | DEVELOPMENT_TEAM = 6G5LMQ72D8; 283 | INFOPLIST_FILE = "UIAlertController+KeyCommands/Info.plist"; 284 | IPHONEOS_DEPLOYMENT_TARGET = 9.0; 285 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 286 | PRODUCT_BUNDLE_IDENTIFIER = "be.silverfox.UIAlertController-KeyCommands"; 287 | PRODUCT_NAME = "$(TARGET_NAME)"; 288 | SWIFT_VERSION = 3.0; 289 | }; 290 | name = Release; 291 | }; 292 | /* End XCBuildConfiguration section */ 293 | 294 | /* Begin XCConfigurationList section */ 295 | BEBFA5B61E7DD25B0099378D /* Build configuration list for PBXProject "UIAlertController+KeyCommands" */ = { 296 | isa = XCConfigurationList; 297 | buildConfigurations = ( 298 | BEBFA5CB1E7DD25C0099378D /* Debug */, 299 | BEBFA5CC1E7DD25C0099378D /* Release */, 300 | ); 301 | defaultConfigurationIsVisible = 0; 302 | defaultConfigurationName = Release; 303 | }; 304 | BEBFA5CD1E7DD25C0099378D /* Build configuration list for PBXNativeTarget "UIAlertController+KeyCommands" */ = { 305 | isa = XCConfigurationList; 306 | buildConfigurations = ( 307 | BEBFA5CE1E7DD25C0099378D /* Debug */, 308 | BEBFA5CF1E7DD25C0099378D /* Release */, 309 | ); 310 | defaultConfigurationIsVisible = 0; 311 | defaultConfigurationName = Release; 312 | }; 313 | /* End XCConfigurationList section */ 314 | }; 315 | rootObject = BEBFA5B31E7DD25B0099378D /* Project object */; 316 | } 317 | -------------------------------------------------------------------------------- /UIAlertController+KeyCommands.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /UIAlertController+KeyCommands/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.swift 3 | // UIAlertController+KeyCommands 4 | // 5 | // Created by Louis D'hauwe on 18/03/2017. 6 | // Copyright © 2017 Silver Fox. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | @UIApplicationMain 12 | class AppDelegate: UIResponder, UIApplicationDelegate { 13 | 14 | var window: UIWindow? 15 | 16 | 17 | func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 18 | // Override point for customization after application launch. 19 | return true 20 | } 21 | 22 | func applicationWillResignActive(_ application: UIApplication) { 23 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 24 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. 25 | } 26 | 27 | func applicationDidEnterBackground(_ application: UIApplication) { 28 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 29 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 30 | } 31 | 32 | func applicationWillEnterForeground(_ application: UIApplication) { 33 | // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. 34 | } 35 | 36 | func applicationDidBecomeActive(_ application: UIApplication) { 37 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 38 | } 39 | 40 | func applicationWillTerminate(_ application: UIApplication) { 41 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 42 | } 43 | 44 | 45 | } 46 | 47 | -------------------------------------------------------------------------------- /UIAlertController+KeyCommands/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "29x29", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "29x29", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "40x40", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "40x40", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "60x60", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "60x60", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "ipad", 35 | "size" : "29x29", 36 | "scale" : "1x" 37 | }, 38 | { 39 | "idiom" : "ipad", 40 | "size" : "29x29", 41 | "scale" : "2x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "40x40", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "size" : "40x40", 51 | "scale" : "2x" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "size" : "76x76", 56 | "scale" : "1x" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "size" : "76x76", 61 | "scale" : "2x" 62 | } 63 | ], 64 | "info" : { 65 | "version" : 1, 66 | "author" : "xcode" 67 | } 68 | } -------------------------------------------------------------------------------- /UIAlertController+KeyCommands/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /UIAlertController+KeyCommands/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /UIAlertController+KeyCommands/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | LSRequiresIPhoneOS 22 | 23 | UILaunchStoryboardName 24 | LaunchScreen 25 | UIMainStoryboardFile 26 | Main 27 | UIRequiredDeviceCapabilities 28 | 29 | armv7 30 | 31 | UISupportedInterfaceOrientations 32 | 33 | UIInterfaceOrientationPortrait 34 | UIInterfaceOrientationLandscapeLeft 35 | UIInterfaceOrientationLandscapeRight 36 | 37 | UISupportedInterfaceOrientations~ipad 38 | 39 | UIInterfaceOrientationPortrait 40 | UIInterfaceOrientationPortraitUpsideDown 41 | UIInterfaceOrientationLandscapeLeft 42 | UIInterfaceOrientationLandscapeRight 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /UIAlertController+KeyCommands/KeyCommandAlertController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // KeyCommandAlertController.swift 3 | // UIAlertController+KeyCommands 4 | // 5 | // Created by Louis D'hauwe on 18/03/2017. 6 | // Copyright © 2017 Silver Fox. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | struct KeyCommandShortcut: CustomStringConvertible { 12 | 13 | let input: String 14 | let modifierFlags: UIKeyModifierFlags 15 | 16 | var description: String { 17 | 18 | var descr = "" 19 | 20 | // TODO: complete flag handling 21 | if modifierFlags.contains(.command) { 22 | descr += "⌘" 23 | } 24 | 25 | if modifierFlags.contains(.shift) { 26 | descr += "⇧" 27 | } 28 | 29 | if modifierFlags.contains(.alternate) { 30 | descr += "⌥" 31 | } 32 | 33 | descr += "\(input)" 34 | 35 | return descr 36 | 37 | } 38 | 39 | } 40 | 41 | class KeyCommandAlertAction { 42 | 43 | let keyShortcut: KeyCommandShortcut? 44 | var uiKeyCommand: UIKeyCommand? 45 | let actionHandler: ((UIAlertAction) -> Swift.Void)? 46 | let title: String? 47 | 48 | public let alertAction: UIAlertAction 49 | 50 | init(title: String?, style: UIAlertActionStyle, keyShortcut: KeyCommandShortcut? = nil, handler: ((UIAlertAction) -> Swift.Void)? = nil) { 51 | 52 | let displayTitle: String? 53 | 54 | if let title = title { 55 | 56 | if let keyShortcut = keyShortcut { 57 | displayTitle = title + " (\(keyShortcut.description))" 58 | } else { 59 | displayTitle = title 60 | } 61 | 62 | } else { 63 | 64 | displayTitle = nil 65 | 66 | } 67 | 68 | alertAction = UIAlertAction(title: displayTitle, style: style, handler: handler) 69 | self.title = title 70 | self.keyShortcut = keyShortcut 71 | self.actionHandler = handler 72 | } 73 | 74 | } 75 | 76 | class KeyCommandAlertController: UIAlertController { 77 | 78 | private var keyCommandActions = [KeyCommandAlertAction]() 79 | 80 | func didActiveKeyCommand(_ sender: UIKeyCommand) { 81 | print("\(sender)") 82 | 83 | for keyCommand in keyCommandActions { 84 | 85 | if sender == keyCommand.uiKeyCommand { 86 | keyCommand.actionHandler?(keyCommand.alertAction) 87 | } 88 | 89 | } 90 | 91 | self.dismiss(animated: true, completion: nil) 92 | } 93 | 94 | func addAction(_ action: KeyCommandAlertAction) { 95 | self.addAction(action.alertAction) 96 | keyCommandActions.append(action) 97 | } 98 | 99 | override var keyCommands: [UIKeyCommand]? { 100 | 101 | var keyCommands = [UIKeyCommand]() 102 | 103 | for keyCommand in keyCommandActions { 104 | 105 | if let keyShortcut = keyCommand.keyShortcut, let title = keyCommand.title { 106 | 107 | let keyCmd = UIKeyCommand(input: keyShortcut.input, modifierFlags: keyShortcut.modifierFlags, action: #selector(didActiveKeyCommand(_:)), discoverabilityTitle: title) 108 | 109 | keyCommand.uiKeyCommand = keyCmd 110 | 111 | keyCommands.append(keyCmd) 112 | 113 | } 114 | 115 | } 116 | 117 | return keyCommands 118 | } 119 | 120 | } 121 | -------------------------------------------------------------------------------- /UIAlertController+KeyCommands/ViewController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.swift 3 | // UIAlertController+KeyCommands 4 | // 5 | // Created by Louis D'hauwe on 18/03/2017. 6 | // Copyright © 2017 Silver Fox. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | class ViewController: UIViewController { 12 | 13 | @IBAction func changeBgColor(_ sender: UIBarButtonItem) { 14 | 15 | let alert = KeyCommandAlertController(title: "Change background color", message: nil, preferredStyle: .actionSheet) 16 | 17 | alert.popoverPresentationController?.barButtonItem = sender 18 | 19 | let redShortcut = KeyCommandShortcut(input: "B", modifierFlags: .command) 20 | let redAction = KeyCommandAlertAction(title: "Blue", style: .default, keyShortcut: redShortcut) { (action) in 21 | 22 | self.makeBlue() 23 | } 24 | 25 | let greenShortcut = KeyCommandShortcut(input: "R", modifierFlags: .command) 26 | let greenAction = KeyCommandAlertAction(title: "Red", style: .default, keyShortcut: greenShortcut) { (action) in 27 | 28 | self.makeRed() 29 | } 30 | 31 | let cancelAction = KeyCommandAlertAction(title: "Cancel", style: .cancel) 32 | 33 | alert.addAction(redAction) 34 | alert.addAction(greenAction) 35 | alert.addAction(cancelAction) 36 | 37 | present(alert, animated: true, completion: nil) 38 | 39 | } 40 | 41 | func makeBlue() { 42 | self.view.backgroundColor = UIColor(red: 0.0, green: 0.73, blue: 0.87, alpha: 1.0) 43 | } 44 | 45 | func makeRed() { 46 | self.view.backgroundColor = UIColor(red: 1.0, green: 0.25, blue: 0.24, alpha: 1.0) 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /readme-resources/example.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/louisdh/KeyCommandAlertController/143aadb0d2d50bc47661fd49bb76fbbd41da29a5/readme-resources/example.gif --------------------------------------------------------------------------------