-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcheat_sheet.swift
267 lines (192 loc) · 9.76 KB
/
cheat_sheet.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// These are answers I've found on Stack Overflow or solutions I've created myself
// I hope they can help you too
import Foundation
import AVFoundation
import CoreData
import AddressBook
import AddressBookUI
// 1) Create a background with a gradient for your view
// if you're using Storyboards, create a background color
// so you can see your other items in your view if your text is white
// on a blue background, for example. Below example is a light blue
// Put this in viewDidLoad
let gradient: CAGradientLayer = CAGradientLayer()
let arrayColors: [AnyObject] = [
UIColor(red: 0.302, green: 0.612, blue: 0.961, alpha: 1.000).CGColor,
UIColor(red: 0.247, green: 0.737, blue: 0.984, alpha: 1.000).CGColor
]
gradient.frame = view.bounds
gradient.colors = arrayColors
view.layer.insertSublayer(gradient, atIndex: 0)
// 2) Play a sound with AVFoundation
// add AVAudioPlayerDelegate to your ViewController like this:
// class ViewController: UIViewController, AVAudioPlayerDelegate { ... }
// declare the player and the error optionals
var avPlayer: AVAudioPlayer?
var error: NSError?
// first line creates a url, which you feed into the player and then play
// use self. with these because your declaration of avPlayer above attached the player
// to the ViewController which is the top-level object & has property ViewContrller.avPlayer
let fileURL: NSURL! = NSBundle.mainBundle().URLForResource("sound_name", withExtension: "wav")
self.avPlayer = AVAudioPlayer(contentsOfURL: fileURL, error: nil)
self.avPlayer?.play()
// 3) How to edit the text of a UIButton
// maybe your button has title "start"
// Normal state is when it's not pressed, when it's just chillin'
buttonName.setTitle("stop", forState: .Normal)
// 4) Setting the value of a textField a few different ways
myTextField.text = "Foo"
// you can edit the value of %.0f for different percision in the decimal, this will return 2, rather than 2.0 or 2.0000
myTextField.text = String(format: "%.0f", (Double(120) / Double(60)))
// convert integer to string
myTextField.text = (5).stringValue
// 5) removing the keyboard
// the code below should be tied to an IBAction most likely
// tap somewhere on the screen, resign first responder
myTextField.resignFirstResponder()
// 6) Focus on a text field
// this is good for submitting a form with a bad value that needs editing
myTextField.becomeFirstResponder()
// 7) Hides the status bar at the top of the screen
override func prefersStatusBarHidden() -> Bool { return true }
// 8) Get last value from CoreData
// fetch last user from coredata
// you must import CoreData at the top of your file to do this
// this assumes your CoreData model has the entity "User", User here can be anything
let appDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
let managedObjectContext = appDelegate.managedObjectContext!
let request = NSFetchRequest(entityName: "User")
var error: NSError?
// makes sure that objects are in the database before you fetch the last one
let result = managedObjectContext.executeFetchRequest(request, error: &error)
if let objects = result as? [User] {
if let lastUser = objects.last { ... }
}
// 9) A better way to set objects in CoreData
// UIApplication is our top level selector for the application
// sharedApplication is one and only one (singleton) instance,
// then we get the delegate property
let appDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
let managedObjectContext = appDelegate.managedObjectContext!
let user = NSEntityDescription.insertNewObjectForEntityForName("User", inManagedObjectContext: managedObjectContext) as User
// store everything from the session in CoreData,
user.name = "Zack Shapiro"
user.hometown = "Baltimore"
user.baseballTeam = "Orioles"
// trigger the save and account for a potential error
var error : NSError?
if !managedObjectContext.save(&error) {
println("save failed: \(error?.localizedDescription)")
}
// 10) prevent your phone from going to sleep, good if you don't want the user
// getting to the lock screen
UIApplication.sharedApplication().idleTimerDisabled = true
// 11) dismiss the keyboard when pressing return in a UITextView
func textView(textView: UITextView!, shouldChangeTextInRange: NSRange, replacementText: NSString!) {
if (replacementText == "\n") {
myTextView.resignFirstResponder()
}
}
// 12) set a maximum amount of characters in a UITextView
// add UITextViewDelegate to your ViewController like this:
// class EditViewController : UIViewController, UITextViewDelegate {
func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
let newLength = countElements(textView.text!) + countElements(text) - range.length
let maxLength = 140
//return true only if the length is at most the maxLength
if newLength <= maxLength {
return true
}
return false
}
// 13) add person with first, last name, job title and phone number to the AddressBook
func addUserToAddressBook(firstName: String, lastName: String, jobTitle: String, phoneNumber: String){
func createMultiStringRef() -> ABMutableMultiValueRef {
let propertyType: NSNumber = kABMultiStringPropertyType
return Unmanaged.fromOpaque(ABMultiValueCreateMutable(propertyType.unsignedIntValue).toOpaque()).takeUnretainedValue() as NSObject as ABMultiValueRef
}
let stat = ABAddressBookGetAuthorizationStatus()
switch stat {
case .Denied, .Restricted:
println("no access to addressbook")
case .Authorized, .NotDetermined:
var err : Unmanaged<CFError>? = nil
var addressBook : ABAddressBook? = ABAddressBookCreateWithOptions(nil, &err).takeRetainedValue()
if addressBook == nil {
println(err)
return
}
ABAddressBookRequestAccessWithCompletion(addressBook) {
(granted:Bool, err:CFError!) in
if granted {
var newContact:ABRecordRef! = ABPersonCreate().takeRetainedValue()
var success:Bool = false
//Updated to work in Xcode 6.1
var error: Unmanaged<CFErrorRef>? = nil
//Updated to error to &error so the code builds in Xcode 6.1
success = ABRecordSetValue(newContact, kABPersonFirstNameProperty, firstName, &error)
success = ABRecordSetValue(newContact, kABPersonLastNameProperty, lastName, &error)
success = ABRecordSetValue(newContact, kABPersonJobTitleProperty, jobTitle, &error)
if (phoneNumber != nil) {
let propertyType: NSNumber = kABMultiStringPropertyType
var phoneNumbers: ABMutableMultiValueRef = createMultiStringRef()
var phone = ((phoneNumber as String).stringByReplacingOccurrencesOfString(" ", withString: "") as NSString)
ABMultiValueAddValueAndLabel(phoneNumbers, phone, kABPersonPhoneMainLabel, nil)
success = ABRecordSetValue(newContact, kABPersonPhoneProperty, phoneNumbers, &error)
}
success = ABRecordSetValue(newContact, kABPersonNoteProperty, "added via iPhone App", &error)
success = ABAddressBookAddRecord(addressBook, newContact, &error)
success = ABAddressBookSave(addressBook, &error)
} else {
println(err)
}
}
}
}
// 14) Use hex color codes for UIColor
// adding this code snippet will enable you to use them like this:
// let purpleColor : UIColor = UIColor.fromRGB(0x71679B) whereas
// everything after the 0x is the hex color code
extension UIColor {
class func fromRGB(rgb:UInt32) -> UIColor {
return UIColor(
red: CGFloat((rgb & 0xFF0000) >> 16) / 255.0,
green: CGFloat((rgb & 0x00FF00) >> 8) / 255.0,
blue: CGFloat(rgb & 0x0000FF) / 255.0,
alpha: CGFloat(1.0)
)
}
}
// 15) NSUserDefaults, save and retrieve values (can store more types than just those shown in this example)
// Typically used to save user preferences for subsequent app launches
let sampleObject = "Jasdev", sampleInt = 1, sampleBool = true
let objectKey = "objectKey", intKey = "intKey", boolKey = "boolKey"
// Set defaults
NSUserDefaults.standardUserDefaults().setObject(sampleObject, forKey: objectKey)
NSUserDefaults.standardUserDefaults().setInteger(sampleInt, forKey: intKey)
NSUserDefaults.standardUserDefaults().setBool(sampleBool, forKey: boolKey)
NSUserDefaults.standardUserDefaults().synchronize()
// Retrieve defaults
let object = NSUserDefaults.standardUserDefaults().objectForKey(objectKey) as String
let int = NSUserDefaults.standardUserDefaults().integerForKey(intKey)
let bool = NSUserDefaults.standardUserDefaults().boolForKey(boolKey)
// 16) Create a NSAttributed String
// Styling a String with things like lineHeight is a bit harder
// than you might think. For example: let's take a String and
// use Avenir Light 28pt font with 6pt lineSpacing
// set the properties
let font = UIFont(name: "Avenir-Light", size: 28)
let style = NSMutableParagraphStyle()
style.lineSpacing = 6
let attributes = [NSParagraphStyleAttributeName : style, NSFontAttributeName: font!]
// Replace Example text with the prefered text or string.
var attributedString = NSAttributedString(string: "Example text", attributes:attributes)
// 17) Make a Screenshot
func makeScreenshot() -> UIImage {
UIGraphicsBeginImageContext(self.view.bounds.size)
self.view.layer.renderInContext(UIGraphicsGetCurrentContext())
var viewImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return viewImage
}
// more comming soon