diff --git a/HomeKitExample/HomeKitExample.xcodeproj/project.pbxproj b/HomeKitExample/HomeKitExample.xcodeproj/project.pbxproj new file mode 100644 index 0000000..e34c65a --- /dev/null +++ b/HomeKitExample/HomeKitExample.xcodeproj/project.pbxproj @@ -0,0 +1,331 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 46; + objects = { + +/* Begin PBXBuildFile section */ + DC149DDE1DEB3EF500A1A5FA /* AccessoryViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC149DDD1DEB3EF500A1A5FA /* AccessoryViewController.swift */; }; + DC1BDC791DEAF9FE00CCBA03 /* AccessorySearcherViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC1BDC781DEAF9FE00CCBA03 /* AccessorySearcherViewController.swift */; }; + DCE125EB1DE9D82E003EFC56 /* DevicesViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCE125EA1DE9D82E003EFC56 /* DevicesViewController.swift */; }; + E73551081DEF2A7500564166 /* Helpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = E73551071DEF2A7500564166 /* Helpers.swift */; }; + E7B636711D34EB3D00711E90 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = E7B636701D34EB3D00711E90 /* AppDelegate.swift */; }; + E7B636731D34EB3D00711E90 /* RoomsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = E7B636721D34EB3D00711E90 /* RoomsViewController.swift */; }; + E7B636761D34EB3D00711E90 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = E7B636741D34EB3D00711E90 /* Main.storyboard */; }; + E7B636781D34EB3D00711E90 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = E7B636771D34EB3D00711E90 /* Assets.xcassets */; }; + E7B6367B1D34EB3D00711E90 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = E7B636791D34EB3D00711E90 /* LaunchScreen.storyboard */; }; + E7B636841D34EB9500711E90 /* HomeKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E7B636831D34EB9500711E90 /* HomeKit.framework */; }; +/* End PBXBuildFile section */ + +/* Begin PBXFileReference section */ + DC149DDD1DEB3EF500A1A5FA /* AccessoryViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AccessoryViewController.swift; sourceTree = ""; }; + DC1BDC781DEAF9FE00CCBA03 /* AccessorySearcherViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AccessorySearcherViewController.swift; sourceTree = ""; }; + DCE125EA1DE9D82E003EFC56 /* DevicesViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DevicesViewController.swift; sourceTree = ""; }; + E73551071DEF2A7500564166 /* Helpers.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Helpers.swift; sourceTree = ""; }; + E7B6366D1D34EB3D00711E90 /* HomeKitExample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = HomeKitExample.app; sourceTree = BUILT_PRODUCTS_DIR; }; + E7B636701D34EB3D00711E90 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; + E7B636721D34EB3D00711E90 /* RoomsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RoomsViewController.swift; sourceTree = ""; }; + E7B636751D34EB3D00711E90 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; + E7B636771D34EB3D00711E90 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; + E7B6367A1D34EB3D00711E90 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; + E7B6367C1D34EB3D00711E90 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + E7B636821D34EB9500711E90 /* HomeKitExample.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.xml; path = HomeKitExample.entitlements; sourceTree = ""; }; + E7B636831D34EB9500711E90 /* HomeKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = HomeKit.framework; path = System/Library/Frameworks/HomeKit.framework; sourceTree = SDKROOT; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + E7B6366A1D34EB3D00711E90 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + E7B636841D34EB9500711E90 /* HomeKit.framework in Frameworks */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + E7B636641D34EB3D00711E90 = { + isa = PBXGroup; + children = ( + E7B636831D34EB9500711E90 /* HomeKit.framework */, + E7B6366F1D34EB3D00711E90 /* HomeKitExample */, + E7B6366E1D34EB3D00711E90 /* Products */, + ); + sourceTree = ""; + }; + E7B6366E1D34EB3D00711E90 /* Products */ = { + isa = PBXGroup; + children = ( + E7B6366D1D34EB3D00711E90 /* HomeKitExample.app */, + ); + name = Products; + sourceTree = ""; + }; + E7B6366F1D34EB3D00711E90 /* HomeKitExample */ = { + isa = PBXGroup; + children = ( + E73551071DEF2A7500564166 /* Helpers.swift */, + E7B636821D34EB9500711E90 /* HomeKitExample.entitlements */, + E7B636701D34EB3D00711E90 /* AppDelegate.swift */, + E7B636721D34EB3D00711E90 /* RoomsViewController.swift */, + DCE125EA1DE9D82E003EFC56 /* DevicesViewController.swift */, + DC1BDC781DEAF9FE00CCBA03 /* AccessorySearcherViewController.swift */, + DC149DDD1DEB3EF500A1A5FA /* AccessoryViewController.swift */, + E7B636741D34EB3D00711E90 /* Main.storyboard */, + E7B636771D34EB3D00711E90 /* Assets.xcassets */, + E7B636791D34EB3D00711E90 /* LaunchScreen.storyboard */, + E7B6367C1D34EB3D00711E90 /* Info.plist */, + ); + path = HomeKitExample; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + E7B6366C1D34EB3D00711E90 /* HomeKitExample */ = { + isa = PBXNativeTarget; + buildConfigurationList = E7B6367F1D34EB3D00711E90 /* Build configuration list for PBXNativeTarget "HomeKitExample" */; + buildPhases = ( + E7B636691D34EB3D00711E90 /* Sources */, + E7B6366A1D34EB3D00711E90 /* Frameworks */, + E7B6366B1D34EB3D00711E90 /* Resources */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = HomeKitExample; + productName = HomeKitExample; + productReference = E7B6366D1D34EB3D00711E90 /* HomeKitExample.app */; + productType = "com.apple.product-type.application"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + E7B636651D34EB3D00711E90 /* Project object */ = { + isa = PBXProject; + attributes = { + LastSwiftUpdateCheck = 0730; + LastUpgradeCheck = 0730; + ORGANIZATIONNAME = Flatstack; + TargetAttributes = { + E7B6366C1D34EB3D00711E90 = { + CreatedOnToolsVersion = 7.3.1; + DevelopmentTeam = C9ZJ96D59R; + LastSwiftMigration = 0810; + SystemCapabilities = { + com.apple.HomeKit = { + enabled = 1; + }; + }; + }; + }; + }; + buildConfigurationList = E7B636681D34EB3D00711E90 /* Build configuration list for PBXProject "HomeKitExample" */; + compatibilityVersion = "Xcode 3.2"; + developmentRegion = English; + hasScannedForEncodings = 0; + knownRegions = ( + en, + Base, + ); + mainGroup = E7B636641D34EB3D00711E90; + productRefGroup = E7B6366E1D34EB3D00711E90 /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + E7B6366C1D34EB3D00711E90 /* HomeKitExample */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXResourcesBuildPhase section */ + E7B6366B1D34EB3D00711E90 /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + E7B6367B1D34EB3D00711E90 /* LaunchScreen.storyboard in Resources */, + E7B636781D34EB3D00711E90 /* Assets.xcassets in Resources */, + E7B636761D34EB3D00711E90 /* Main.storyboard in Resources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXResourcesBuildPhase section */ + +/* Begin PBXSourcesBuildPhase section */ + E7B636691D34EB3D00711E90 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + E7B636731D34EB3D00711E90 /* RoomsViewController.swift in Sources */, + E73551081DEF2A7500564166 /* Helpers.swift in Sources */, + DCE125EB1DE9D82E003EFC56 /* DevicesViewController.swift in Sources */, + DC1BDC791DEAF9FE00CCBA03 /* AccessorySearcherViewController.swift in Sources */, + E7B636711D34EB3D00711E90 /* AppDelegate.swift in Sources */, + DC149DDE1DEB3EF500A1A5FA /* AccessoryViewController.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin PBXVariantGroup section */ + E7B636741D34EB3D00711E90 /* Main.storyboard */ = { + isa = PBXVariantGroup; + children = ( + E7B636751D34EB3D00711E90 /* Base */, + ); + name = Main.storyboard; + sourceTree = ""; + }; + E7B636791D34EB3D00711E90 /* LaunchScreen.storyboard */ = { + isa = PBXVariantGroup; + children = ( + E7B6367A1D34EB3D00711E90 /* Base */, + ); + name = LaunchScreen.storyboard; + sourceTree = ""; + }; +/* End PBXVariantGroup section */ + +/* Begin XCBuildConfiguration section */ + E7B6367D1D34EB3D00711E90 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = dwarf; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_TESTABILITY = YES; + GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_DYNAMIC_NO_PIC = NO; + GCC_NO_COMMON_BLOCKS = YES; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + IPHONEOS_DEPLOYMENT_TARGET = 9.3; + MTL_ENABLE_DEBUG_INFO = YES; + ONLY_ACTIVE_ARCH = YES; + SDKROOT = iphoneos; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + }; + name = Debug; + }; + E7B6367E1D34EB3D00711E90 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + ENABLE_NS_ASSERTIONS = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_NO_COMMON_BLOCKS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + IPHONEOS_DEPLOYMENT_TARGET = 9.3; + MTL_ENABLE_DEBUG_INFO = NO; + SDKROOT = iphoneos; + VALIDATE_PRODUCT = YES; + }; + name = Release; + }; + E7B636801D34EB3D00711E90 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + CODE_SIGN_ENTITLEMENTS = HomeKitExample/HomeKitExample.entitlements; + CODE_SIGN_IDENTITY = "iPhone Developer"; + DEVELOPMENT_TEAM = C9ZJ96D59R; + INFOPLIST_FILE = HomeKitExample/Info.plist; + LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; + PRODUCT_BUNDLE_IDENTIFIER = com.ellinakuznecova.HomeKitExample; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; + }; + name = Debug; + }; + E7B636811D34EB3D00711E90 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + CODE_SIGN_ENTITLEMENTS = HomeKitExample/HomeKitExample.entitlements; + CODE_SIGN_IDENTITY = "iPhone Developer"; + DEVELOPMENT_TEAM = C9ZJ96D59R; + INFOPLIST_FILE = HomeKitExample/Info.plist; + LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; + PRODUCT_BUNDLE_IDENTIFIER = com.ellinakuznecova.HomeKitExample; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + E7B636681D34EB3D00711E90 /* Build configuration list for PBXProject "HomeKitExample" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + E7B6367D1D34EB3D00711E90 /* Debug */, + E7B6367E1D34EB3D00711E90 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + E7B6367F1D34EB3D00711E90 /* Build configuration list for PBXNativeTarget "HomeKitExample" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + E7B636801D34EB3D00711E90 /* Debug */, + E7B636811D34EB3D00711E90 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + }; + rootObject = E7B636651D34EB3D00711E90 /* Project object */; +} diff --git a/HomeKitExample/HomeKitExample.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/HomeKitExample/HomeKitExample.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..54c416c --- /dev/null +++ b/HomeKitExample/HomeKitExample.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/HomeKitExample/HomeKitExample/AccessorySearcherViewController.swift b/HomeKitExample/HomeKitExample/AccessorySearcherViewController.swift new file mode 100644 index 0000000..4758d73 --- /dev/null +++ b/HomeKitExample/HomeKitExample/AccessorySearcherViewController.swift @@ -0,0 +1,75 @@ +// +// AccessorySearcherViewController.swift +// HomeKitExample +// +// Created by Ellina Kuznecova on 27.11.16. +// Copyright © 2016 Flatstack. All rights reserved. +// + +import UIKit +import HomeKit + +class AccessorySearcherViewController: UIViewController { + @IBOutlet weak var tableview: UITableView! + + var accessoryBrowser: HMAccessoryBrowser? + var roomToAdd: HMRoom! + var availableAccessories: [HMAccessory] = [] { + didSet { + self.tableview.reloadData() + self.tableview.isHidden = availableAccessories.count == 0 + } + } + + override func viewDidLoad() { + super.viewDidLoad() + + self.tableview.isHidden = true + + self.accessoryBrowser = HMAccessoryBrowser() + self.accessoryBrowser?.delegate = self + self.accessoryBrowser?.startSearchingForNewAccessories() + } + + override func viewWillDisappear(_ animated: Bool) { + super.viewWillDisappear(animated) + + self.accessoryBrowser?.stopSearchingForNewAccessories() + } + + @IBAction func cancelPressed(_ sender: Any) { + self.dismiss(animated: true, completion: nil) + } +} + +extension AccessorySearcherViewController: UITableViewDataSource { + func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { + return self.availableAccessories.count + } + + func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { + let cell = tableView.dequeueReusableCell(withIdentifier: "basic")! + cell.textLabel?.text = self.availableAccessories[indexPath.row].name + return cell + } +} + +extension AccessorySearcherViewController: UITableViewDelegate { + func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { + let selectedAccessory = self.availableAccessories[indexPath.row] + HomeManager.primaryHome?.addAccessory(selectedAccessory, completionHandler: {[weak self] (error) in + guard error == nil else {print(error!); return} + guard let roomToAdd = self?.roomToAdd else {return} + HomeManager.primaryHome?.assignAccessory(selectedAccessory, to: roomToAdd, completionHandler: { (error) in + guard error == nil else {print(error!); return} + self?.dismiss(animated: true, completion: nil) + }) + }) + } +} + +extension AccessorySearcherViewController: HMAccessoryBrowserDelegate { + func accessoryBrowser(_ browser: HMAccessoryBrowser, didFindNewAccessory accessory: HMAccessory) { + self.availableAccessories.append(accessory) + } +} diff --git a/HomeKitExample/HomeKitExample/AccessoryViewController.swift b/HomeKitExample/HomeKitExample/AccessoryViewController.swift new file mode 100644 index 0000000..09a4ee7 --- /dev/null +++ b/HomeKitExample/HomeKitExample/AccessoryViewController.swift @@ -0,0 +1,34 @@ +// +// AccessoryViewController.swift +// HomeKitExample +// +// Created by Ellina Kuznecova on 27.11.16. +// Copyright © 2016 Flatstack. All rights reserved. +// + +import UIKit +import HomeKit + +class AccessoryViewController: UIViewController { + @IBOutlet weak var onOffSwitch: UISwitch! + @IBOutlet weak var accessoryTitle: UILabel! + + var accessory: HMAccessory? + var onOffCharacterictic: HMCharacteristic? + + override func viewDidLoad() { + super.viewDidLoad() + self.onOffCharacterictic = self.accessory?.services.last?.characteristics.filter({$0.characteristicType == HMCharacteristicTypePowerState}).first + self.accessoryTitle.text = accessory?.name + + let onOffCharactericticState = self.onOffCharacterictic?.value as? Bool ?? false + self.onOffSwitch.setOn(onOffCharactericticState, animated: true) + } + + + @IBAction func switchValueChanged(_ sender: Any) { + self.onOffCharacterictic?.writeValue(self.onOffSwitch.isOn, completionHandler: { (error) in + if let error = error {print(error)} + }) + } +} diff --git a/HomeKitExample/HomeKitExample/AppDelegate.swift b/HomeKitExample/HomeKitExample/AppDelegate.swift new file mode 100644 index 0000000..417714a --- /dev/null +++ b/HomeKitExample/HomeKitExample/AppDelegate.swift @@ -0,0 +1,47 @@ +// +// AppDelegate.swift +// HomeKitExample +// +// Created by Ellina Kuznetcova on 12.07.16. +// Copyright © 2016 Flatstack. All rights reserved. +// + +import UIKit +import HomeKit + +@UIApplicationMain +class AppDelegate: UIResponder, UIApplicationDelegate { + + var window: UIWindow? + + + func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { + // Override point for customization after application launch. + return true + } + + 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) { + // 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) { + // 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) { + // 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) { + // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. + } + + +} + diff --git a/HomeKitExample/HomeKitExample/Assets.xcassets/AppIcon.appiconset/Contents.json b/HomeKitExample/HomeKitExample/Assets.xcassets/AppIcon.appiconset/Contents.json new file mode 100644 index 0000000..118c98f --- /dev/null +++ b/HomeKitExample/HomeKitExample/Assets.xcassets/AppIcon.appiconset/Contents.json @@ -0,0 +1,38 @@ +{ + "images" : [ + { + "idiom" : "iphone", + "size" : "29x29", + "scale" : "2x" + }, + { + "idiom" : "iphone", + "size" : "29x29", + "scale" : "3x" + }, + { + "idiom" : "iphone", + "size" : "40x40", + "scale" : "2x" + }, + { + "idiom" : "iphone", + "size" : "40x40", + "scale" : "3x" + }, + { + "idiom" : "iphone", + "size" : "60x60", + "scale" : "2x" + }, + { + "idiom" : "iphone", + "size" : "60x60", + "scale" : "3x" + } + ], + "info" : { + "version" : 1, + "author" : "xcode" + } +} \ No newline at end of file diff --git a/HomeKitExample/HomeKitExample/Base.lproj/LaunchScreen.storyboard b/HomeKitExample/HomeKitExample/Base.lproj/LaunchScreen.storyboard new file mode 100644 index 0000000..2e721e1 --- /dev/null +++ b/HomeKitExample/HomeKitExample/Base.lproj/LaunchScreen.storyboard @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/HomeKitExample/HomeKitExample/Base.lproj/Main.storyboard b/HomeKitExample/HomeKitExample/Base.lproj/Main.storyboard new file mode 100644 index 0000000..cdb8a7e --- /dev/null +++ b/HomeKitExample/HomeKitExample/Base.lproj/Main.storyboard @@ -0,0 +1,259 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/HomeKitExample/HomeKitExample/DevicesViewController.swift b/HomeKitExample/HomeKitExample/DevicesViewController.swift new file mode 100644 index 0000000..edbd3bb --- /dev/null +++ b/HomeKitExample/HomeKitExample/DevicesViewController.swift @@ -0,0 +1,81 @@ +// +// AccessoriesViewController.swift +// HomeKitExample +// +// Created by Ellina Kuznecova on 26.11.16. +// Copyright © 2016 Flatstack. All rights reserved. +// + +import UIKit +import HomeKit + +class AccessoriesViewController: UITableViewController { + struct AccessoryAddSegue { + static let indentifier = "toAccessoryAdding" + } + + struct DetailAccessorySegue { + static let indentifier = "toDetailAccessory" + + var accessory: HMAccessory + + init(accessory: HMAccessory) { + self.accessory = accessory + } + } + + var room: HMRoom? + var accessories: [HMAccessory] = [] { + didSet { + self.tableView.reloadData() + } + } + + override func viewDidLoad() { + super.viewDidLoad() + + HomeManager.delegate = self + + self.accessories = self.room?.accessories ?? [] + } + + override func prepare(for segue: UIStoryboardSegue, sender: Any?) { + guard let identifier = segue.identifier else {return} + switch identifier { + case AccessoryAddSegue.indentifier: + let destNavigationController = segue.destination as? UINavigationController + (destNavigationController?.topViewController as? AccessorySearcherViewController)?.roomToAdd = self.room + return + case DetailAccessorySegue.indentifier: + guard let segueData = sender as? DetailAccessorySegue else {return} + (segue.destination as? AccessoryViewController)?.accessory = segueData.accessory + return + default: + return + } + } + + @IBAction func addPressed(_ sender: Any) { + self.performSegue(withIdentifier: AccessoryAddSegue.indentifier, sender: nil) + } +} + +extension AccessoriesViewController { + override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { + return self.accessories.count + } + + override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { + let cell = tableView.dequeueReusableCell(withIdentifier: "basic")! + cell.textLabel?.text = self.accessories[indexPath.row].name + return cell + } + + override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { + self.performSegue(withIdentifier: DetailAccessorySegue.indentifier, sender: DetailAccessorySegue(accessory: self.accessories[indexPath.row])) + } +} + +extension AccessoriesViewController: HMHomeManagerDelegate { + +} diff --git a/HomeKitExample/HomeKitExample/Helpers.swift b/HomeKitExample/HomeKitExample/Helpers.swift new file mode 100644 index 0000000..ad11080 --- /dev/null +++ b/HomeKitExample/HomeKitExample/Helpers.swift @@ -0,0 +1,16 @@ +// +// Helpers.swift +// HomeKitExample +// +// Created by Ellina Kuznecova on 26.11.16. +// Copyright © 2016 Flatstack. All rights reserved. +// + +import Foundation +import HomeKit + +public func FSDispatch_after_short (_ delay:Double, block:@escaping ()->()) { + DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + Double(Int64(delay * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC), execute: block); +} + +let HomeManager = HMHomeManager() diff --git a/HomeKitExample/HomeKitExample/HomeKitExample.entitlements b/HomeKitExample/HomeKitExample/HomeKitExample.entitlements new file mode 100644 index 0000000..1e4b40d --- /dev/null +++ b/HomeKitExample/HomeKitExample/HomeKitExample.entitlements @@ -0,0 +1,8 @@ + + + + + com.apple.developer.homekit + + + diff --git a/HomeKitExample/HomeKitExample/Info.plist b/HomeKitExample/HomeKitExample/Info.plist new file mode 100644 index 0000000..79aa209 --- /dev/null +++ b/HomeKitExample/HomeKitExample/Info.plist @@ -0,0 +1,42 @@ + + + + + NSHomeKitUsageDescription + test + CFBundleDevelopmentRegion + en + CFBundleExecutable + $(EXECUTABLE_NAME) + CFBundleIdentifier + $(PRODUCT_BUNDLE_IDENTIFIER) + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + $(PRODUCT_NAME) + CFBundlePackageType + APPL + CFBundleShortVersionString + 1.0 + CFBundleSignature + ???? + CFBundleVersion + 1 + LSRequiresIPhoneOS + + UILaunchStoryboardName + LaunchScreen + UIMainStoryboardFile + Main + UIRequiredDeviceCapabilities + + armv7 + + UISupportedInterfaceOrientations + + UIInterfaceOrientationPortrait + UIInterfaceOrientationLandscapeLeft + UIInterfaceOrientationLandscapeRight + + + diff --git a/HomeKitExample/HomeKitExample/RoomsViewController.swift b/HomeKitExample/HomeKitExample/RoomsViewController.swift new file mode 100644 index 0000000..7c9f8a4 --- /dev/null +++ b/HomeKitExample/HomeKitExample/RoomsViewController.swift @@ -0,0 +1,102 @@ +// +// RoomsViewController.swift +// HomeKitExample +// +// Created by Ellina Kuznetcova on 12.07.16. +// Copyright © 2016 Flatstack. All rights reserved. +// + +import UIKit +import HomeKit + +class RoomsViewController: UITableViewController { + + struct AccessoriesSegue { + static let identifier = "toAccessories" + + var selectedRoom: HMRoom + + init(selectedRoom: HMRoom) { + self.selectedRoom = selectedRoom + } + } + + var rooms : [HMRoom] = [] { + didSet { + self.tableView.reloadData() + } + } + + override func viewDidLoad() { + super.viewDidLoad() + + HomeManager.delegate = self + + FSDispatch_after_short(1.0, block: {[weak self] in + self?.rooms = HomeManager.primaryHome?.rooms ?? [] + }) + } + + @IBAction func addButtonPressed(sender: AnyObject) { + let alertController = UIAlertController(title: "Choose an object to cteate", message: nil, preferredStyle: .actionSheet) + + alertController.addAction(UIAlertAction(title: "Home", style: .default, handler: { [weak self] (action) in + let homeAlertController = UIAlertController(title: "Enter home name", message: nil, preferredStyle: .alert) + homeAlertController.addTextField(configurationHandler: nil) + homeAlertController.addAction(UIAlertAction(title: "OK", style: .default, handler: { [weak homeAlertController] (alertAction) in + guard let homeName = homeAlertController?.textFields?.first?.text else {return} + HomeManager.addHome(withName: homeName, completionHandler: { (home, error) in + guard let error = error else {return} + print(error) + }) + })) + self?.showDetailViewController(homeAlertController, sender: nil) + })) + + alertController.addAction(UIAlertAction(title: "Room", style: .default, handler: { [weak self] (action) in + let roomAlertController = UIAlertController(title: "Enter room name", message: nil, preferredStyle: .alert) + roomAlertController.addTextField(configurationHandler: nil) + roomAlertController.addAction(UIAlertAction(title: "OK", style: .default, handler: { [weak roomAlertController] (alertAction) in + guard let roomName = roomAlertController?.textFields?.first?.text else {return} + HomeManager.primaryHome?.addRoom(withName: roomName, completionHandler: { (room, error) in + guard let lRoom = room, error == nil else {print(error!); return} + self?.rooms.append(lRoom) + }) + })) + self?.showDetailViewController(roomAlertController, sender: nil) + })) + + alertController.addAction(UIAlertAction(title: "Cancel", style: .destructive, handler: nil)) + self.showDetailViewController(alertController, sender: nil) + } + + override func prepare(for segue: UIStoryboardSegue, sender: Any?) { + guard let accessoriesSegue = sender as? AccessoriesSegue else {return} + (segue.destination as? AccessoriesViewController)?.room = accessoriesSegue.selectedRoom + } +} + +extension RoomsViewController { + override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { + return self.rooms.count + } + + override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { + let cell = tableView.dequeueReusableCell(withIdentifier: "basic")! + cell.textLabel?.text = self.rooms[indexPath.row].name + return cell + } + + override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { + self.performSegue(withIdentifier: AccessoriesSegue.identifier, sender: AccessoriesSegue(selectedRoom: self.rooms[indexPath.row])) + } +} + +extension RoomsViewController: HMHomeManagerDelegate { + func homeManagerDidUpdateHomes(_ manager: HMHomeManager) { +// self.rooms = manager.homes +// print(self.homes) +// print(self.homes[0].rooms) + } +} +