-
Notifications
You must be signed in to change notification settings - Fork 478
/
AppDelegate.swift
124 lines (109 loc) · 3.75 KB
/
AppDelegate.swift
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//
// AppDelegate.swift
// Finding Values in the Keychain
//
// Created by Vandad Nahavandipoor on 7/7/14.
// Copyright (c) 2014 Pixolity Ltd. All rights reserved.
//
// These example codes are written for O'Reilly's iOS 8 Swift Programming Cookbook
// If you use these solutions in your apps, you can give attribution to
// Vandad Nahavandipoor for his work. Feel free to visit my blog
// at http://vandadnp.wordpress.com for daily tips and tricks in Swift
// and Objective-C and various other programming languages.
//
// You can purchase "iOS 8 Swift Programming Cookbook" from
// the following URL:
// http://shop.oreilly.com/product/0636920034254.do
//
// If you have any questions, you can contact me directly
// at vandad.np@gmail.com
// Similarly, if you find an error in these sample codes, simply
// report them to O'Reilly at the following URL:
// http://www.oreilly.com/catalog/errata.csp?isbn=0636920034254
///* 1 */
//import UIKit
//
//@UIApplicationMain
//class AppDelegate: UIResponder, UIApplicationDelegate {
//
// var window: UIWindow?
//
// func application(application: UIApplication,
// didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {
//
// let keyToSearchFor = "Full Name"
// let service = NSBundle.mainBundle().bundleIdentifier!
//
// let query = [
// kSecClass as String :
// kSecClassGenericPassword as String,
//
// kSecAttrService as String : service,
// kSecAttrAccount as String : keyToSearchFor,
// kSecReturnAttributes as String : kCFBooleanTrue,
// ] as NSDictionary
//
//
// var valueAttributes: Unmanaged<AnyObject>? = nil
// let results = Int(SecItemCopyMatching(query, &valueAttributes))
//
// if results == Int(errSecSuccess){
//
// let attributes = valueAttributes!.takeRetainedValue() as! NSDictionary
//
// let key = attributes[kSecAttrAccount as String]
// as! String
//
// let accessGroup = attributes[kSecAttrAccessGroup as String] as! String
//
// let creationDate = attributes[kSecAttrCreationDate as NSString] as! NSDate
//
// let modifiedDate = attributes[
// kSecAttrModificationDate as NSString] as! NSDate
//
// let serviceValue = attributes[kSecAttrService as String] as! String
//
// print("Key = \(key)")
// print("Access Group = \(accessGroup)")
// print("Creation Date = \(creationDate)")
// print("Modification Date = \(modifiedDate)")
// print("Service = \(serviceValue)")
//
// } else {
// print("Error happened with code: \(results)", appendNewline: false)
// }
//
// return true
// }
//
//}
//
///* 2 */
import UIKit
import Security
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {
let keyToSearchFor = "Full Name"
let service = NSBundle.mainBundle().bundleIdentifier!
let query = [
kSecClass as String :
kSecClassGenericPassword as String,
kSecAttrService as String : service,
kSecAttrAccount as String : keyToSearchFor,
kSecReturnData as String : kCFBooleanTrue,
]
var returnedData: Unmanaged<AnyObject>? = nil
let results = Int(SecItemCopyMatching(query, &returnedData))
if results == Int(errSecSuccess){
let data = returnedData!.takeRetainedValue() as! NSData
let value = NSString(data: data, encoding: NSUTF8StringEncoding)
print("Value = \(value)")
} else {
print("Error happened with code: \(results)")
}
return true
}
}