Skip to content

Commit

Permalink
Update for Swift 3
Browse files Browse the repository at this point in the history
  • Loading branch information
ashleymills committed Sep 29, 2016
1 parent 463ac0e commit e8f3b31
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 86 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ DerivedData
# Carthage/Checkouts

Carthage/Build
.DS_Store
17 changes: 17 additions & 0 deletions Example/Pods/Pods.xcodeproj/project.pbxproj

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 14 additions & 1 deletion Example/Simple-KeychainSwift.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -233,14 +233,16 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0730;
LastUpgradeCheck = 0720;
LastUpgradeCheck = 0800;
ORGANIZATIONNAME = CocoaPods;
TargetAttributes = {
607FACCF1AFB9204008FA782 = {
CreatedOnToolsVersion = 6.3.1;
LastSwiftMigration = 0800;
};
607FACE41AFB9204008FA782 = {
CreatedOnToolsVersion = 6.3.1;
LastSwiftMigration = 0800;
TestTargetID = 607FACCF1AFB9204008FA782;
};
};
Expand Down Expand Up @@ -439,8 +441,10 @@
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
Expand Down Expand Up @@ -484,8 +488,10 @@
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
Expand All @@ -504,6 +510,7 @@
IPHONEOS_DEPLOYMENT_TARGET = 8.3;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
VALIDATE_PRODUCT = YES;
};
name = Release;
Expand All @@ -512,6 +519,7 @@
isa = XCBuildConfiguration;
baseConfigurationReference = 08C3A10E0A405968FCAA3EA7 /* Pods-Simple-KeychainSwift_Example.debug.xcconfig */;
buildSettings = {
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ENABLE_MODULES = YES;
INFOPLIST_FILE = "Simple-KeychainSwift/Info.plist";
Expand All @@ -520,20 +528,23 @@
PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.demo.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
};
name = Debug;
};
607FACF11AFB9204008FA782 /* Release */ = {
isa = XCBuildConfiguration;
baseConfigurationReference = B9A1BECAD39E0AFE79A3A265 /* Pods-Simple-KeychainSwift_Example.release.xcconfig */;
buildSettings = {
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ENABLE_MODULES = YES;
INFOPLIST_FILE = "Simple-KeychainSwift/Info.plist";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
MODULE_NAME = ExampleApp;
PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.demo.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand All @@ -552,6 +563,7 @@
MACH_O_TYPE = mh_bundle;
PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Simple-KeychainSwift_Example.app/Simple-KeychainSwift_Example";
};
name = Debug;
Expand All @@ -567,6 +579,7 @@
MACH_O_TYPE = mh_bundle;
PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Simple-KeychainSwift_Example.app/Simple-KeychainSwift_Example";
};
name = Release;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0720"
LastUpgradeVersion = "0800"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
12 changes: 6 additions & 6 deletions Example/Simple-KeychainSwift/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,30 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?


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

func applicationWillResignActive(application: UIApplication) {
func applicationWillResignActive(_ application: UIApplication) {
// 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.
// 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.
}

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

func applicationWillEnterForeground(application: UIApplication) {
func applicationWillEnterForeground(_ application: UIApplication) {
// 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.
}

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

func applicationWillTerminate(application: UIApplication) {
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

Expand Down
60 changes: 30 additions & 30 deletions Example/Simple-KeychainSwift/Classes/Keychain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,23 @@ import Foundation
class Keychain {

// MARK: - *** Public methods ***
class func set(value:String, forKey key:String) -> Bool {
class func set(_ value:String, forKey key:String) -> Bool {
if valueExists(forKey: key) {
return update(value, forKey: key)
} else {
return create(value, forKey: key)
}
}

class func set(bool:Bool, forKey key:String) -> Bool {
class func set(_ bool:Bool, forKey key:String) -> Bool {
let value = bool ? "true" : "false"
return set(value, forKey: key)
}

class func value(forKey key: String) -> String? {
guard let valueData = valueData(forKey: key) else { return nil }

return NSString(data: valueData, encoding: NSUTF8StringEncoding) as? String
return NSString(data: valueData, encoding: String.Encoding.utf8.rawValue) as? String
}

class func bool(forKey key: String) -> Bool {
Expand All @@ -41,56 +41,56 @@ class Keychain {
class func reset() -> Bool {

let searchDictionary = basicDictionary()
let status = SecItemDelete(searchDictionary)
let status = SecItemDelete(searchDictionary as CFDictionary)
return status == errSecSuccess
}

// MARK: - *** methods ***
private class func valueExists(forKey key: String) -> Bool {
fileprivate class func valueExists(forKey key: String) -> Bool {
return valueData(forKey: key) != nil
}

private class func create(value: String, forKey key: String) -> Bool {
fileprivate class func create(_ value: String, forKey key: String) -> Bool {
var dictionary = newSearchDictionary(forKey: key)

dictionary[kSecValueData as String] = value.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
dictionary[kSecValueData as String] = value.data(using: String.Encoding.utf8, allowLossyConversion: false) as AnyObject?

let status = SecItemAdd(dictionary, nil)
let status = SecItemAdd(dictionary as CFDictionary, nil)
return status == errSecSuccess
}

private class func update(value: String, forKey key: String) -> Bool {
fileprivate class func update(_ value: String, forKey key: String) -> Bool {

let searchDictionary = newSearchDictionary(forKey: key)
var updateDictionary = [String: AnyObject]()

updateDictionary[kSecValueData as String] = value.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
updateDictionary[kSecValueData as String] = value.data(using: String.Encoding.utf8, allowLossyConversion: false) as AnyObject?

let status = SecItemUpdate(searchDictionary, updateDictionary)
let status = SecItemUpdate(searchDictionary as CFDictionary, updateDictionary as CFDictionary)

return status == errSecSuccess
}

private class func deleteValue(forKey key: String) -> Bool {
fileprivate class func deleteValue(forKey key: String) -> Bool {
let searchDictionary = newSearchDictionary(forKey: key)
let status = SecItemDelete(searchDictionary)
let status = SecItemDelete(searchDictionary as CFDictionary)

return status == errSecSuccess
}

private class func valueData(forKey key: String) -> NSData? {
fileprivate class func valueData(forKey key: String) -> Data? {

var searchDictionary = newSearchDictionary(forKey: key)

searchDictionary[kSecMatchLimit as String] = kSecMatchLimitOne
searchDictionary[kSecReturnData as String] = kCFBooleanTrue

var retrievedData: AnyObject?
let status = SecItemCopyMatching(searchDictionary as CFDictionaryRef, &retrievedData)
let status = SecItemCopyMatching(searchDictionary as CFDictionary, &retrievedData)

var data: NSData?
var data: Data?
if status == errSecSuccess {
data = retrievedData as? NSData
data = retrievedData as? Data
}

return data
Expand All @@ -107,12 +107,12 @@ class Keychain {
var retrievedAttributes: AnyObject?
var retrievedData: AnyObject?

var status = SecItemCopyMatching(searchDictionary as CFDictionaryRef, &retrievedAttributes)
var status = SecItemCopyMatching(searchDictionary as CFDictionary, &retrievedAttributes)
if status != errSecSuccess {
return nil
}

status = SecItemCopyMatching(searchDictionary as CFDictionaryRef, &retrievedData)
status = SecItemCopyMatching(searchDictionary as CFDictionary, &retrievedData)
if status != errSecSuccess {
return nil
}
Expand All @@ -121,30 +121,30 @@ class Keychain {

var allValues = [[String : String]]()
for attributeDict in attributeDicts {
guard let keyData = attributeDict[kSecAttrAccount as String] as? NSData else { continue }
guard let valueData = attributeDict[kSecValueData as String] as? NSData else { continue }
guard let key = NSString(data: keyData, encoding: NSUTF8StringEncoding) as? String else { continue }
guard let value = NSString(data: valueData, encoding: NSUTF8StringEncoding) as? String else { continue }
guard let keyData = attributeDict[kSecAttrAccount as String] as? Data else { continue }
guard let valueData = attributeDict[kSecValueData as String] as? Data else { continue }
guard let key = NSString(data: keyData, encoding: String.Encoding.utf8.rawValue) as? String else { continue }
guard let value = NSString(data: valueData, encoding: String.Encoding.utf8.rawValue) as? String else { continue }
allValues.append([key: value])
}

return allValues
}

private class func newSearchDictionary(forKey key: String) -> [String: AnyObject] {
let encodedIdentifier = key.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
fileprivate class func newSearchDictionary(forKey key: String) -> [String: AnyObject] {
let encodedIdentifier = key.data(using: String.Encoding.utf8, allowLossyConversion: false)

var searchDictionary = basicDictionary()
searchDictionary[kSecAttrGeneric as String] = encodedIdentifier
searchDictionary[kSecAttrAccount as String] = encodedIdentifier
searchDictionary[kSecAttrGeneric as String] = encodedIdentifier as AnyObject?
searchDictionary[kSecAttrAccount as String] = encodedIdentifier as AnyObject?

return searchDictionary
}

private class func basicDictionary() -> [String: AnyObject] {
fileprivate class func basicDictionary() -> [String: AnyObject] {

let serviceName = NSBundle(forClass: self).infoDictionary![kCFBundleIdentifierKey as String] as! String
let serviceName = Bundle(for: self).infoDictionary![kCFBundleIdentifierKey as String] as! String

return [kSecClass as String : kSecClassGenericPassword, kSecAttrService as String : serviceName]
return [kSecClass as String : kSecClassGenericPassword, kSecAttrService as String : serviceName as AnyObject]
}
}
Loading

0 comments on commit e8f3b31

Please # to comment.