Skip to content
This repository was archived by the owner on Feb 18, 2025. It is now read-only.

Commit c770c3d

Browse files
author
Kyle Swank
authored
Merge pull request #393 from agilebits/kyle/update-demos-to-swift4
Update Swift demos for Swift 4 and Xcode 9
2 parents be3f5da + fbdc9b3 commit c770c3d

File tree

9 files changed

+195
-152
lines changed

9 files changed

+195
-152
lines changed

Demos/App Demo for iOS Swift/App Demo for iOS Swift.xcodeproj/project.pbxproj

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@
115115
isa = PBXProject;
116116
attributes = {
117117
LastSwiftUpdateCheck = 0700;
118-
LastUpgradeCheck = 0700;
118+
LastUpgradeCheck = 0910;
119119
ORGANIZATIONNAME = Agilebits;
120120
TargetAttributes = {
121121
F1B11CE61ACA827600BBB963 = {
@@ -191,13 +191,21 @@
191191
CLANG_CXX_LIBRARY = "libc++";
192192
CLANG_ENABLE_MODULES = YES;
193193
CLANG_ENABLE_OBJC_ARC = YES;
194+
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
194195
CLANG_WARN_BOOL_CONVERSION = YES;
196+
CLANG_WARN_COMMA = YES;
195197
CLANG_WARN_CONSTANT_CONVERSION = YES;
196198
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
197199
CLANG_WARN_EMPTY_BODY = YES;
198200
CLANG_WARN_ENUM_CONVERSION = YES;
201+
CLANG_WARN_INFINITE_RECURSION = YES;
199202
CLANG_WARN_INT_CONVERSION = YES;
203+
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
204+
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
200205
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
206+
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
207+
CLANG_WARN_STRICT_PROTOTYPES = YES;
208+
CLANG_WARN_SUSPICIOUS_MOVE = YES;
201209
CLANG_WARN_UNREACHABLE_CODE = YES;
202210
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
203211
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
@@ -206,6 +214,7 @@
206214
ENABLE_TESTABILITY = YES;
207215
GCC_C_LANGUAGE_STANDARD = gnu99;
208216
GCC_DYNAMIC_NO_PIC = NO;
217+
GCC_NO_COMMON_BLOCKS = YES;
209218
GCC_OPTIMIZATION_LEVEL = 0;
210219
GCC_PREPROCESSOR_DEFINITIONS = (
211220
"DEBUG=1",
@@ -237,13 +246,21 @@
237246
CLANG_CXX_LIBRARY = "libc++";
238247
CLANG_ENABLE_MODULES = YES;
239248
CLANG_ENABLE_OBJC_ARC = YES;
249+
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
240250
CLANG_WARN_BOOL_CONVERSION = YES;
251+
CLANG_WARN_COMMA = YES;
241252
CLANG_WARN_CONSTANT_CONVERSION = YES;
242253
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
243254
CLANG_WARN_EMPTY_BODY = YES;
244255
CLANG_WARN_ENUM_CONVERSION = YES;
256+
CLANG_WARN_INFINITE_RECURSION = YES;
245257
CLANG_WARN_INT_CONVERSION = YES;
258+
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
259+
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
246260
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
261+
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
262+
CLANG_WARN_STRICT_PROTOTYPES = YES;
263+
CLANG_WARN_SUSPICIOUS_MOVE = YES;
247264
CLANG_WARN_UNREACHABLE_CODE = YES;
248265
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
249266
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
@@ -252,6 +269,7 @@
252269
ENABLE_STRICT_OBJC_MSGSEND = YES;
253270
ENABLE_TESTABILITY = NO;
254271
GCC_C_LANGUAGE_STANDARD = gnu99;
272+
GCC_NO_COMMON_BLOCKS = YES;
255273
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
256274
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
257275
GCC_WARN_UNDECLARED_SELECTOR = YES;
@@ -261,6 +279,7 @@
261279
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
262280
MTL_ENABLE_DEBUG_INFO = NO;
263281
SDKROOT = iphoneos;
282+
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
264283
SWIFT_VERSION = 2.3;
265284
TARGETED_DEVICE_FAMILY = "1,2";
266285
VALIDATE_PRODUCT = YES;
@@ -278,7 +297,7 @@
278297
PRODUCT_NAME = "App Demo for iOS Swift";
279298
SWIFT_OBJC_BRIDGING_HEADER = "App Demo for iOS Swift/App Demo for iOS Swift-Bridging-Header.h";
280299
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
281-
SWIFT_VERSION = 2.3;
300+
SWIFT_VERSION = 4.0;
282301
TARGETED_DEVICE_FAMILY = 1;
283302
};
284303
name = Debug;
@@ -293,7 +312,7 @@
293312
PRODUCT_BUNDLE_IDENTIFIER = "com.agilebits.$(PRODUCT_NAME:rfc1034identifier)";
294313
PRODUCT_NAME = "App Demo for iOS Swift";
295314
SWIFT_OBJC_BRIDGING_HEADER = "App Demo for iOS Swift/App Demo for iOS Swift-Bridging-Header.h";
296-
SWIFT_VERSION = 2.3;
315+
SWIFT_VERSION = 4.0;
297316
TARGETED_DEVICE_FAMILY = 1;
298317
};
299318
name = Release;

Demos/App Demo for iOS Swift/App Demo for iOS Swift/AppDelegate.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,30 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
1313

1414
var window: UIWindow?
1515

16-
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
16+
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
1717
// Override point for customization after application launch.
1818
return true
1919
}
2020

21-
func applicationWillResignActive(application: UIApplication) {
21+
func applicationWillResignActive(_ application: UIApplication) {
2222
// 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.
2323
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
2424
}
2525

26-
func applicationDidEnterBackground(application: UIApplication) {
26+
func applicationDidEnterBackground(_ application: UIApplication) {
2727
// 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.
2828
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
2929
}
3030

31-
func applicationWillEnterForeground(application: UIApplication) {
31+
func applicationWillEnterForeground(_ application: UIApplication) {
3232
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
3333
}
3434

35-
func applicationDidBecomeActive(application: UIApplication) {
35+
func applicationDidBecomeActive(_ application: UIApplication) {
3636
// 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.
3737
}
3838

39-
func applicationWillTerminate(application: UIApplication) {
39+
func applicationWillTerminate(_ application: UIApplication) {
4040
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
4141
// Saves changes in the application's managed object context before the application terminates.
4242
}

Demos/App Demo for iOS Swift/App Demo for iOS Swift/ChangePasswordViewController.swift

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,31 +21,33 @@ class ChangePasswordViewController: UIViewController {
2121
self.view.backgroundColor = UIColor(patternImage: patternImage)
2222
}
2323

24-
self.onepasswordButton.hidden = (false == OnePasswordExtension.sharedExtension().isAppExtensionAvailable())
24+
onepasswordButton.isHidden = (false == OnePasswordExtension.shared().isAppExtensionAvailable())
2525
}
2626

27-
override func preferredStatusBarStyle() -> UIStatusBarStyle {
28-
return UIStatusBarStyle.LightContent
27+
override var preferredStatusBarStyle: UIStatusBarStyle {
28+
return .lightContent
2929
}
3030

31-
@IBAction func changePasswordIn1Password(sender:AnyObject) -> Void {
32-
let changedPassword = self.freshPasswordTextField.text!
33-
let oldPassword = self.oldPasswordTextField.text!
34-
let confirmationPassword = self.confirmPasswordTextField.text!
31+
@IBAction func changePasswordIn1Password(_ sender:AnyObject) {
32+
guard let changedPassword = freshPasswordTextField.text,
33+
let oldPassword = oldPasswordTextField.text,
34+
let confirmationPassword = confirmPasswordTextField.text else {
35+
return
36+
}
3537

3638
// Validate that the new password and the old password are not the same.
37-
if (oldPassword.characters.count > 0 && oldPassword == changedPassword) {
38-
self.showChangePasswordFailedAlertWithMessage("The old and the new password must not be the same")
39+
if (oldPassword.count > 0 && oldPassword == changedPassword) {
40+
showChangePasswordFailedAlertWithMessage(message: "The old and the new password must not be the same")
3941
return
4042
}
4143

4244
// Validate that the new and confirmation passwords match.
43-
if (changedPassword.characters.count > 0 && changedPassword != confirmationPassword) {
44-
self.showChangePasswordFailedAlertWithMessage("The new passwords and the confirmation password must match")
45+
if (changedPassword.count > 0 && changedPassword != confirmationPassword) {
46+
showChangePasswordFailedAlertWithMessage(message: "The new passwords and the confirmation password must match")
4547
return
4648
}
4749

48-
let newLoginDetails:[String: AnyObject] = [
50+
let newLoginDetails:[String : Any] = [
4951
AppExtensionTitleKey: "ACME", // Optional, used for the third schenario only
5052
AppExtensionUsernameKey: "aUsername", // Optional, used for the third schenario only
5153
AppExtensionPasswordKey: changedPassword,
@@ -54,50 +56,50 @@ class ChangePasswordViewController: UIViewController {
5456
]
5557

5658
// The password generation options are optional, but are very handy in case you have strict rules about password lengths, symbols and digits.
57-
let passwordGenerationOptions:[String: AnyObject] = [
59+
let passwordGenerationOptions:[String : AnyObject] = [
5860
// The minimum password length can be 4 or more.
59-
AppExtensionGeneratedPasswordMinLengthKey: (8),
61+
AppExtensionGeneratedPasswordMinLengthKey: (8 as NSNumber),
6062

6163
// The maximum password length can be 50 or less.
62-
AppExtensionGeneratedPasswordMaxLengthKey: (30),
64+
AppExtensionGeneratedPasswordMaxLengthKey: (30 as NSNumber),
6365

6466
// If YES, the 1Password will guarantee that the generated password will contain at least one digit (number between 0 and 9). Passing NO will not exclude digits from the generated password.
65-
AppExtensionGeneratedPasswordRequireDigitsKey: (true),
67+
AppExtensionGeneratedPasswordRequireDigitsKey: (true as NSNumber),
6668

6769
// If YES, the 1Password will guarantee that the generated password will contain at least one symbol (See the list below). Passing NO will not exclude symbols from the generated password.
68-
AppExtensionGeneratedPasswordRequireSymbolsKey: (true),
70+
AppExtensionGeneratedPasswordRequireSymbolsKey: (true as NSNumber),
6971

7072
// Here are all the symbols available in the the 1Password Password Generator:
7173
// !@#$%^&*()_-+=|[]{}'\";.,>?/~`
7274
// The string for AppExtensionGeneratedPasswordForbiddenCharactersKey should contain the symbols and characters that you wish 1Password to exclude from the generated password.
73-
AppExtensionGeneratedPasswordForbiddenCharactersKey: "!@#$%/0lIO"
75+
AppExtensionGeneratedPasswordForbiddenCharactersKey: "!@#$%/0lIO" as NSString
7476
]
7577

76-
OnePasswordExtension.sharedExtension().changePasswordForLoginForURLString("https://www.acme.com", loginDetails: newLoginDetails, passwordGenerationOptions: passwordGenerationOptions, forViewController: self, sender: sender) { (loginDictionary, error) -> Void in
77-
if loginDictionary == nil {
78-
if error!.code != Int(AppExtensionErrorCodeCancelledByUser) {
79-
print("Error invoking 1Password App Extension for find login: \(error)")
78+
OnePasswordExtension.shared().changePasswordForLogin(forURLString: "https://www.acme.com", loginDetails: newLoginDetails, passwordGenerationOptions: passwordGenerationOptions, for: self, sender: sender) { (loginDictionary, error) in
79+
guard let loginDictionary = loginDictionary else {
80+
if let error = error as NSError?, error.code != AppExtensionErrorCodeCancelledByUser {
81+
print("Error invoking 1Password App Extension for find login: \(String(describing: error))")
8082
}
8183
return
8284
}
8385

84-
self.oldPasswordTextField.text = loginDictionary?[AppExtensionOldPasswordKey] as? String
85-
self.freshPasswordTextField.text = loginDictionary?[AppExtensionPasswordKey] as? String
86-
self.confirmPasswordTextField.text = loginDictionary?[AppExtensionPasswordKey] as? String
86+
self.oldPasswordTextField.text = loginDictionary[AppExtensionOldPasswordKey] as? String
87+
self.freshPasswordTextField.text = loginDictionary[AppExtensionPasswordKey] as? String
88+
self.confirmPasswordTextField.text = loginDictionary[AppExtensionPasswordKey] as? String
8789
}
8890
}
8991

9092
// Convenience function
91-
func showChangePasswordFailedAlertWithMessage(message:String) -> Void {
92-
let alertController = UIAlertController(title: "Change Password Error", message: message, preferredStyle: UIAlertControllerStyle.Alert)
93+
func showChangePasswordFailedAlertWithMessage(message:String) {
94+
let alertController = UIAlertController(title: "Change Password Error", message: message, preferredStyle: .alert)
9395

94-
let dismissAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) { (action) -> Void in
96+
let dismissAction = UIAlertAction(title: "Cancel", style: .cancel) { action in
9597
self.freshPasswordTextField.text = ""
9698
self.confirmPasswordTextField.text = ""
9799
self.freshPasswordTextField.becomeFirstResponder()
98100
}
99101

100102
alertController.addAction(dismissAction)
101-
self.presentViewController(alertController, animated: true, completion: nil)
103+
self.present(alertController, animated: true, completion: nil)
102104
}
103105
}

Demos/App Demo for iOS Swift/App Demo for iOS Swift/LoginViewController.swift

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,50 +22,49 @@ class LoginViewController: UIViewController {
2222
self.view.backgroundColor = UIColor(patternImage: patternImage)
2323
}
2424

25-
self.onepasswordButton.hidden = (false == OnePasswordExtension.sharedExtension().isAppExtensionAvailable())
25+
onepasswordButton.isHidden = (false == OnePasswordExtension.shared().isAppExtensionAvailable())
2626
}
2727

28-
override func viewDidAppear(animated: Bool) {
28+
override func viewDidAppear(_ animated: Bool) {
2929
super.viewDidAppear(animated)
30-
if OnePasswordExtension.sharedExtension().isAppExtensionAvailable() == false {
31-
let alertController = UIAlertController(title: "1Password is not installed", message: "Get 1Password from the App Store", preferredStyle: UIAlertControllerStyle.Alert)
30+
if OnePasswordExtension.shared().isAppExtensionAvailable() == false {
31+
let alertController = UIAlertController(title: "1Password is not installed", message: "Get 1Password from the App Store", preferredStyle: UIAlertControllerStyle.alert)
3232

33-
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)
33+
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
3434
alertController.addAction(cancelAction)
3535

36-
let OKAction = UIAlertAction(title: "Get 1Password", style: .Default) { (action) in UIApplication.sharedApplication().openURL(NSURL(string: "https://itunes.apple.com/app/1password-password-manager/id568903335")!)
36+
let OKAction = UIAlertAction(title: "Get 1Password", style: .default) { (action) in UIApplication.shared.openURL(URL(string: "https://itunes.apple.com/app/1password-password-manager/id568903335")!)
3737
}
3838

3939
alertController.addAction(OKAction)
40-
self.presentViewController(alertController, animated: true, completion: nil)
40+
self.present(alertController, animated: true, completion: nil)
4141
}
4242
}
4343

44-
override func preferredStatusBarStyle() -> UIStatusBarStyle {
45-
return UIStatusBarStyle.LightContent
44+
override var preferredStatusBarStyle: UIStatusBarStyle {
45+
return .lightContent
4646
}
4747

48-
@IBAction func findLoginFrom1Password(sender:AnyObject) -> Void {
49-
OnePasswordExtension.sharedExtension().findLoginForURLString("https://www.acme.com", forViewController: self, sender: sender, completion: { (loginDictionary, error) -> Void in
50-
if loginDictionary == nil {
51-
if error!.code != Int(AppExtensionErrorCodeCancelledByUser) {
52-
print("Error invoking 1Password App Extension for find login: \(error)")
48+
@IBAction func findLoginFrom1Password(_ sender:AnyObject) {
49+
OnePasswordExtension.shared().findLogin(forURLString: "https://www.acme.com", for: self, sender: sender, completion: { (loginDictionary, error) in
50+
guard let loginDictionary = loginDictionary else {
51+
if let error = error as NSError?, error.code != AppExtensionErrorCodeCancelledByUser {
52+
print("Error invoking 1Password App Extension for find login: \(String(describing: error))")
5353
}
5454
return
5555
}
5656

57-
self.usernameTextField.text = loginDictionary?[AppExtensionUsernameKey] as? String
58-
self.passwordTextField.text = loginDictionary?[AppExtensionPasswordKey] as? String
57+
self.usernameTextField.text = loginDictionary[AppExtensionUsernameKey] as? String
58+
self.passwordTextField.text = loginDictionary[AppExtensionPasswordKey] as? String
5959

60-
if let generatedOneTimePassword = loginDictionary?[AppExtensionTOTPKey] as? String {
61-
self.oneTimePasswordTextField.hidden = false
60+
if let generatedOneTimePassword = loginDictionary[AppExtensionTOTPKey] as? String {
61+
self.oneTimePasswordTextField.isHidden = false
6262
self.oneTimePasswordTextField.text = generatedOneTimePassword
6363

6464
// Important: It is recommended that you submit the OTP/TOTP to your validation server as soon as you receive it, otherwise it may expire.
65-
let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(0.5 * Double(NSEC_PER_SEC)))
66-
dispatch_after(delayTime, dispatch_get_main_queue(), { () -> Void in
67-
self.performSegueWithIdentifier("showThankYouViewController", sender: self)
68-
})
65+
let delayTime: DispatchTime = .now() + DispatchTimeInterval.milliseconds(500)
66+
DispatchQueue.main.asyncAfter(deadline: delayTime) { self.performSegue(withIdentifier: "showThankYouViewController", sender: self)
67+
}
6968
}
7069

7170
})

0 commit comments

Comments
 (0)