Skip to content

Instantly share code, notes, and snippets.

@sunnycyk
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sunnycyk/103fefd0de041c222b94 to your computer and use it in GitHub Desktop.
Save sunnycyk/103fefd0de041c222b94 to your computer and use it in GitHub Desktop.
my IOS notes
// open it
var error: Unmanaged<CFError>?
addressBook = ABAddressBookCreateWithOptions(nil, &error)?.takeRetainedValue()
if self.addressBook == nil {
println(error?.takeRetainedValue())
return
}
// check if access is granted
ABAddressBookRequestAccessWithCompletion(addressBook) {
granted, error in
if !granted {
return
}
// This cause segmentation fault if converting to NSArray. Use [ABRecordRef] Instead.
let people:[ABRecordRef] = ABAddressBookCopyArrayOfAllPeople(self.addressBook)?.takeRetainedValue() as [ABRecordRef]
}
Objective C
[UIApplication sharedApplication].idleTimerDisabled = YES; // Turn off sleep mode
[UIApplication sharedApplication].idleTimerDisabled = NO; // Turn on
Swift
UIApplication.sharedApplication().idleTimerDisabled = true // Turn off sleep mode
UIApplication.sharedApplication().idleTimerDisabled = false // Turn
// Set up Actions for notifications
let viewDetailAction:UIMutableUserNotificationAction = UIMutableUserNotificationAction()
viewDetailAction.identifier = "VIEW_ACTION"
viewDetailAction.title = "View Detail"
viewDetailAction.activationMode = UIUserNotificationActivationMode.Foreground
viewDetailAction.destructive = false
viewDetailAction.authenticationRequired = true
let cancelAction:UIMutableUserNotificationAction = UIMutableUserNotificationAction()
cancelAction.identifier = "CANCEL_ACTION"
cancelAction.title = "Cancel"
cancelAction.activationMode = UIUserNotificationActivationMode.Background
cancelAction.destructive = true
cancelAction.authenticationRequired = false
// set up category
let category = UIMutableUserNotificationCategory()
category.identifier = "FIRST_CATEGORY" // id to be to reference later
// add actions to the array
let defaultAction:NSArray = [viewDetailAction, cancelAction]
category.setActions(defaultAction, forContext: UIUserNotificationActionContext.Default)
// For minimal (if there are more than two actions, use UIUserNotificationActionContext.Minimal)
// put category in to NSSet; we only have one category now
let categories = NSSet(object: category)
// set type of notification will be using
let types = UIUserNotificationType.Badge | UIUserNotificationType.Alert | UIUserNotificationType.Sound
// create UIUserNotificationSettings
let settings = UIUserNotificationSettings(forTypes: types, categories: categories)
// Finally, register setting
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
// Now create UILocalNotification
let notification = UILocalNotification()
notification.category = "FIRST_CATEGORY" // we will using this category that we defined earlier
notification.alertBody = "This is first Notice!"
notification.soundName = UILocalNotificationDefaultSoundName // we will use default sound
// if you want to schedule, set the time in notification.fireDate and scheduleLocalNotification()
UIApplication.sharedApplication().presentLocalNotificationNow(notification) // this will show notification now
// -(void) someFunction(unsigned char *) data length:(int) length
func someFunctionBridgingFromOjectiveC(data: UnsafeMutablePointer<UInt8>, length: Int32) {
var s:NSString! = NSString(bytes: data, length: Int(length), encoding: NSUTF8StringEncoding)
data.destroy()
NSLog(s!)
}
@zackshapiro
Copy link

Where's the best place to put UIApplication.sharedApplication().idleTimerDisabled in the ViewController? Inside of ViewDidLoad or somewhere else?

@sunnycyk
Copy link
Author

sunnycyk commented Jan 9, 2015

It should put inside your control flow where you do not want the app to go to sleep, and turn on sleep mode while the task is finished. If you want to keep the ViewController always on until it is removed, then yea, you can do it in ViewDidLoad, but just remember to turn it back on before the ViewController is removed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment