Skip to content

Instantly share code, notes, and snippets.

@protocol NSCoding
- (void)encodeWithCoder:(NSCoder *)aCoder;
- (id)initWithCoder:(NSCoder *)aDecoder; // NS_DESIGNATED_INITIALIZER
@end
xcrun simctl list | grep "Booted"
@Eluss
Eluss / GainAccessToCalendar.m
Last active August 29, 2015 14:26
Gaining access to calendar in your app
[[EKEventStore new] requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (granted) {
NSLog(@"Access to calendar granted");
} else {
NSLog(@"No access to calendar");
}
}];
@Eluss
Eluss / CalendarList.m
Created July 31, 2015 09:13
Getting calendars list from your calendar
NSArray *calendars = [[EKEventStore new] calendarsForEntityType:EKEntityTypeEvent];
@Eluss
Eluss / EventsFromCalendars.m
Last active August 29, 2015 14:26
Events from calendar
NSPredicate *predicate = [[EKEventStore new] predicateForEventsWithStartDate:startDate
endDate:endDate
calendars:calendars];
NSArray *events = [[EKEventStore new] eventsMatchingPredicate:predicate];
@Eluss
Eluss / Event.swift
Created October 2, 2015 15:22
Event class with nscoding
import Foundation
class Event : NSObject, NSCoding {
var name : String?
var details : String?
var date : NSDate?
init(name : String, details : String, date : NSDate) {
self.name = name
func application(application: UIApplication, handleWatchKitExtensionRequest userInfo: [NSObject : AnyObject]?, reply: ([NSObject : AnyObject]?) -> Void) {
let message = userInfo?["message"] as! String
let event = Event(name: "test", details: "test", date:NSDate())
NSKeyedArchiver.setClassName("Event", forClass: Event.self)
NSKeyedArchiver.archivedDataWithRootObject(event)
let dictionary = ["event" : event]
@Eluss
Eluss / InterfaceController.swift
Last active October 2, 2015 15:57
openParentApplication
@IBAction func buttonAction() {
let dictionary = ["message": "Hi, apple watch here!"]
WKInterfaceController.openParentApplication(dictionary) {
(replyInfo, error) -> Void in
let reply = replyInfo["event"] as! NSData // take our encoded event from dictionary
NSKeyedUnarchiver.setClass(Event.self, forClassName: "Event") // We are not using original class anymore, so we have to do it
let event = NSKeyedUnarchiver.unarchiveObjectWithData(reply) // decode the event
}
@Eluss
Eluss / AppDelegate.swift
Last active October 2, 2015 16:02
Appdelegate watch connection
func application(application: UIApplication, handleWatchKitExtensionRequest userInfo: [NSObject : AnyObject]?, reply: ([NSObject : AnyObject]?) -> Void) {
let message = userInfo?["message"] as! String // we can get a message, that we sent from watch
let event = Event(name: "test", details: "test", date:NSDate()) // create our Event object
NSKeyedArchiver.setClassName("Event", forClass: Event.self) // We are using 'different' classes on watch and iPhone, so we have to do it
let archivedEvent = NSKeyedArchiver.archivedDataWithRootObject(event) // archive the Event
let dictionary = ["event" : archivedEvent] // creating dictionary with our NSData
public private(set) lazy var imageView: UIImageView = {
let imageView = UIImageView()
imageView.layer.cornerRadius = Dimensions.imageSize / 2
imageView.clipsToBounds = true
imageView.contentMode = .ScaleAspectFill
return imageView
}()