diff --git a/ConnectedColors-master/ConnectedColors.xcodeproj/project.pbxproj b/ConnectedColors-master/ConnectedColors.xcodeproj/project.pbxproj new file mode 100644 index 0000000..f93cbf6 --- /dev/null +++ b/ConnectedColors-master/ConnectedColors.xcodeproj/project.pbxproj @@ -0,0 +1,326 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 46; + objects = { + +/* Begin PBXBuildFile section */ + BF362D852108C80300F7397B /* ColorService.swift in Sources */ = {isa = PBXBuildFile; fileRef = BF362D842108C80300F7397B /* ColorService.swift */; }; + BF6F13F81E4E58C90018D56E /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = BF6F13F71E4E58C90018D56E /* AppDelegate.swift */; }; + BF6F13FA1E4E58C90018D56E /* ColorSwitchViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = BF6F13F91E4E58C90018D56E /* ColorSwitchViewController.swift */; }; + BF6F13FD1E4E58C90018D56E /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = BF6F13FB1E4E58C90018D56E /* Main.storyboard */; }; + BF6F13FF1E4E58C90018D56E /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = BF6F13FE1E4E58C90018D56E /* Assets.xcassets */; }; + BF6F14021E4E58C90018D56E /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = BF6F14001E4E58C90018D56E /* LaunchScreen.storyboard */; }; +/* End PBXBuildFile section */ + +/* Begin PBXFileReference section */ + BF362D842108C80300F7397B /* ColorService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ColorService.swift; sourceTree = ""; }; + BF6F13F41E4E58C90018D56E /* ConnectedColors.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = ConnectedColors.app; sourceTree = BUILT_PRODUCTS_DIR; }; + BF6F13F71E4E58C90018D56E /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; + BF6F13F91E4E58C90018D56E /* ColorSwitchViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ColorSwitchViewController.swift; sourceTree = ""; }; + BF6F13FC1E4E58C90018D56E /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; + BF6F13FE1E4E58C90018D56E /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; + BF6F14011E4E58C90018D56E /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; + BF6F14031E4E58C90018D56E /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + BF6F13F11E4E58C90018D56E /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + BF6F13EB1E4E58C90018D56E = { + isa = PBXGroup; + children = ( + BF6F13F61E4E58C90018D56E /* ConnectedColors */, + BF6F13F51E4E58C90018D56E /* Products */, + ); + sourceTree = ""; + }; + BF6F13F51E4E58C90018D56E /* Products */ = { + isa = PBXGroup; + children = ( + BF6F13F41E4E58C90018D56E /* ConnectedColors.app */, + ); + name = Products; + sourceTree = ""; + }; + BF6F13F61E4E58C90018D56E /* ConnectedColors */ = { + isa = PBXGroup; + children = ( + BF6F13F71E4E58C90018D56E /* AppDelegate.swift */, + BF6F13F91E4E58C90018D56E /* ColorSwitchViewController.swift */, + BF362D842108C80300F7397B /* ColorService.swift */, + BF6F13FB1E4E58C90018D56E /* Main.storyboard */, + BF6F13FE1E4E58C90018D56E /* Assets.xcassets */, + BF6F14001E4E58C90018D56E /* LaunchScreen.storyboard */, + BF6F14031E4E58C90018D56E /* Info.plist */, + ); + path = ConnectedColors; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + BF6F13F31E4E58C90018D56E /* ConnectedColors */ = { + isa = PBXNativeTarget; + buildConfigurationList = BF6F14061E4E58C90018D56E /* Build configuration list for PBXNativeTarget "ConnectedColors" */; + buildPhases = ( + BF6F13F01E4E58C90018D56E /* Sources */, + BF6F13F11E4E58C90018D56E /* Frameworks */, + BF6F13F21E4E58C90018D56E /* Resources */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = ConnectedColors; + productName = ConnectedColors; + productReference = BF6F13F41E4E58C90018D56E /* ConnectedColors.app */; + productType = "com.apple.product-type.application"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + BF6F13EC1E4E58C90018D56E /* Project object */ = { + isa = PBXProject; + attributes = { + LastSwiftUpdateCheck = 0820; + LastUpgradeCheck = 0910; + ORGANIZATIONNAME = Example; + TargetAttributes = { + BF6F13F31E4E58C90018D56E = { + CreatedOnToolsVersion = 8.2.1; + DevelopmentTeam = MC27HKM86M; + LastSwiftMigration = 0910; + ProvisioningStyle = Automatic; + }; + }; + }; + buildConfigurationList = BF6F13EF1E4E58C90018D56E /* Build configuration list for PBXProject "ConnectedColors" */; + compatibilityVersion = "Xcode 3.2"; + developmentRegion = English; + hasScannedForEncodings = 0; + knownRegions = ( + English, + en, + Base, + ); + mainGroup = BF6F13EB1E4E58C90018D56E; + productRefGroup = BF6F13F51E4E58C90018D56E /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + BF6F13F31E4E58C90018D56E /* ConnectedColors */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXResourcesBuildPhase section */ + BF6F13F21E4E58C90018D56E /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + BF6F14021E4E58C90018D56E /* LaunchScreen.storyboard in Resources */, + BF6F13FF1E4E58C90018D56E /* Assets.xcassets in Resources */, + BF6F13FD1E4E58C90018D56E /* Main.storyboard in Resources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXResourcesBuildPhase section */ + +/* Begin PBXSourcesBuildPhase section */ + BF6F13F01E4E58C90018D56E /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + BF362D852108C80300F7397B /* ColorService.swift in Sources */, + BF6F13FA1E4E58C90018D56E /* ColorSwitchViewController.swift in Sources */, + BF6F13F81E4E58C90018D56E /* AppDelegate.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin PBXVariantGroup section */ + BF6F13FB1E4E58C90018D56E /* Main.storyboard */ = { + isa = PBXVariantGroup; + children = ( + BF6F13FC1E4E58C90018D56E /* Base */, + ); + name = Main.storyboard; + sourceTree = ""; + }; + BF6F14001E4E58C90018D56E /* LaunchScreen.storyboard */ = { + isa = PBXVariantGroup; + children = ( + BF6F14011E4E58C90018D56E /* Base */, + ); + name = LaunchScreen.storyboard; + sourceTree = ""; + }; +/* End PBXVariantGroup section */ + +/* Begin XCBuildConfiguration section */ + BF6F14041E4E58C90018D56E /* 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_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + 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 = 10.0; + MTL_ENABLE_DEBUG_INFO = YES; + ONLY_ACTIVE_ARCH = YES; + SDKROOT = iphoneos; + SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + }; + name = Debug; + }; + BF6F14051E4E58C90018D56E /* 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_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + 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 = 10.0; + MTL_ENABLE_DEBUG_INFO = NO; + SDKROOT = iphoneos; + SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; + VALIDATE_PRODUCT = YES; + }; + name = Release; + }; + BF6F14071E4E58C90018D56E /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + DEVELOPMENT_TEAM = MC27HKM86M; + INFOPLIST_FILE = ConnectedColors/Info.plist; + LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; + PRODUCT_BUNDLE_IDENTIFIER = com.example.ConnectedColors; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 4.0; + }; + name = Debug; + }; + BF6F14081E4E58C90018D56E /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + DEVELOPMENT_TEAM = MC27HKM86M; + INFOPLIST_FILE = ConnectedColors/Info.plist; + LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; + PRODUCT_BUNDLE_IDENTIFIER = com.example.ConnectedColors; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 4.0; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + BF6F13EF1E4E58C90018D56E /* Build configuration list for PBXProject "ConnectedColors" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + BF6F14041E4E58C90018D56E /* Debug */, + BF6F14051E4E58C90018D56E /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + BF6F14061E4E58C90018D56E /* Build configuration list for PBXNativeTarget "ConnectedColors" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + BF6F14071E4E58C90018D56E /* Debug */, + BF6F14081E4E58C90018D56E /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + }; + rootObject = BF6F13EC1E4E58C90018D56E /* Project object */; +} diff --git a/ConnectedColors-master/ConnectedColors.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/ConnectedColors-master/ConnectedColors.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..36a5c22 --- /dev/null +++ b/ConnectedColors-master/ConnectedColors.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/ConnectedColors-master/ConnectedColors.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/ConnectedColors-master/ConnectedColors.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist new file mode 100644 index 0000000..18d9810 --- /dev/null +++ b/ConnectedColors-master/ConnectedColors.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist @@ -0,0 +1,8 @@ + + + + + IDEDidComputeMac32BitWarning + + + diff --git a/ConnectedColors-master/ConnectedColors.xcodeproj/project.xcworkspace/xcuserdata/andreas.xcuserdatad/UserInterfaceState.xcuserstate b/ConnectedColors-master/ConnectedColors.xcodeproj/project.xcworkspace/xcuserdata/andreas.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..4950736 Binary files /dev/null and b/ConnectedColors-master/ConnectedColors.xcodeproj/project.xcworkspace/xcuserdata/andreas.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/ConnectedColors-master/ConnectedColors.xcodeproj/xcuserdata/andreas.xcuserdatad/xcschemes/xcschememanagement.plist b/ConnectedColors-master/ConnectedColors.xcodeproj/xcuserdata/andreas.xcuserdatad/xcschemes/xcschememanagement.plist new file mode 100644 index 0000000..ba74086 --- /dev/null +++ b/ConnectedColors-master/ConnectedColors.xcodeproj/xcuserdata/andreas.xcuserdatad/xcschemes/xcschememanagement.plist @@ -0,0 +1,14 @@ + + + + + SchemeUserState + + ConnectedColors.xcscheme_^#shared#^_ + + orderHint + 0 + + + + diff --git a/ConnectedColors-master/ConnectedColors/AppDelegate.swift b/ConnectedColors-master/ConnectedColors/AppDelegate.swift new file mode 100644 index 0000000..a441369 --- /dev/null +++ b/ConnectedColors-master/ConnectedColors/AppDelegate.swift @@ -0,0 +1,72 @@ +import UIKit + +@UIApplicationMain +class AppDelegate: UIResponder, UIApplicationDelegate { + + var window: UIWindow? +let colorService = ColorService() + func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { + // Override point for customization after application launch. + + UserDefaults.standard.set("none", forKey: "status") + colorService.delegate = self + application.setMinimumBackgroundFetchInterval(1800) + return true + } +func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { + colorService.delegate = self + application.setMinimumBackgroundFetchInterval(1800) + } + 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 invalidate graphics rendering callbacks. 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 active 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:. + } + + func change(color : UIColor) { + UIView.animate(withDuration: 0.2) { + + } + } +} + + +extension AppDelegate : ColorServiceDelegate { + + func connectedDevicesChanged(manager: ColorService, connectedDevices: [String]) { + OperationQueue.main.addOperation { + print( "Connections: \(connectedDevices)") + } + } + + func colorChanged(manager: ColorService, colorString: String) { + OperationQueue.main.addOperation { + switch colorString { + case "red": + UserDefaults.standard.set("connected", forKey: "status") + case "yellow": + break + // self.change(color: .yellow) + default: + NSLog("%@", "Unknown color value received: \(colorString)") + } + } + } + +} diff --git a/ConnectedColors-master/ConnectedColors/Assets.xcassets/AppIcon.appiconset/Contents.json b/ConnectedColors-master/ConnectedColors/Assets.xcassets/AppIcon.appiconset/Contents.json new file mode 100644 index 0000000..118c98f --- /dev/null +++ b/ConnectedColors-master/ConnectedColors/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/ConnectedColors-master/ConnectedColors/Base.lproj/LaunchScreen.storyboard b/ConnectedColors-master/ConnectedColors/Base.lproj/LaunchScreen.storyboard new file mode 100644 index 0000000..fdf3f97 --- /dev/null +++ b/ConnectedColors-master/ConnectedColors/Base.lproj/LaunchScreen.storyboard @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/ConnectedColors-master/ConnectedColors/Base.lproj/Main.storyboard b/ConnectedColors-master/ConnectedColors/Base.lproj/Main.storyboard new file mode 100644 index 0000000..ddee2a0 --- /dev/null +++ b/ConnectedColors-master/ConnectedColors/Base.lproj/Main.storyboard @@ -0,0 +1,90 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/ConnectedColors-master/ConnectedColors/ColorService.swift b/ConnectedColors-master/ConnectedColors/ColorService.swift new file mode 100644 index 0000000..9941be6 --- /dev/null +++ b/ConnectedColors-master/ConnectedColors/ColorService.swift @@ -0,0 +1,120 @@ +import Foundation +import MultipeerConnectivity + +protocol ColorServiceDelegate { + + func connectedDevicesChanged(manager : ColorService, connectedDevices: [String]) + func colorChanged(manager : ColorService, colorString: String) + +} + +class ColorService : NSObject { + + // Service type must be a unique string, at most 15 characters long + // and can contain only ASCII lowercase letters, numbers and hyphens. + private let ColorServiceType = "example-color" + + private let myPeerId = MCPeerID(displayName: UIDevice.current.name) + private let serviceAdvertiser : MCNearbyServiceAdvertiser + private let serviceBrowser : MCNearbyServiceBrowser + + var delegate : ColorServiceDelegate? + + lazy var session : MCSession = { + let session = MCSession(peer: self.myPeerId, securityIdentity: nil, encryptionPreference: .required) + session.delegate = self + return session + }() + + override init() { + self.serviceAdvertiser = MCNearbyServiceAdvertiser(peer: myPeerId, discoveryInfo: nil, serviceType: ColorServiceType) + self.serviceBrowser = MCNearbyServiceBrowser(peer: myPeerId, serviceType: ColorServiceType) + + super.init() + + self.serviceAdvertiser.delegate = self + self.serviceAdvertiser.startAdvertisingPeer() + + self.serviceBrowser.delegate = self + self.serviceBrowser.startBrowsingForPeers() + } + + deinit { + self.serviceAdvertiser.stopAdvertisingPeer() + self.serviceBrowser.stopBrowsingForPeers() + } + + func send(colorName : String) { + NSLog("%@", "sendColor: \(colorName) to \(session.connectedPeers.count) peers") + + if session.connectedPeers.count > 0 { + do { + try self.session.send(colorName.data(using: .utf8)!, toPeers: session.connectedPeers, with: .reliable) + } + catch let error { + NSLog("%@", "Error for sending: \(error)") + } + } + + } + +} + +extension ColorService : MCNearbyServiceAdvertiserDelegate { + + func advertiser(_ advertiser: MCNearbyServiceAdvertiser, didNotStartAdvertisingPeer error: Error) { + NSLog("%@", "didNotStartAdvertisingPeer: \(error)") + } + + func advertiser(_ advertiser: MCNearbyServiceAdvertiser, didReceiveInvitationFromPeer peerID: MCPeerID, withContext context: Data?, invitationHandler: @escaping (Bool, MCSession?) -> Void) { + NSLog("%@", "didReceiveInvitationFromPeer \(peerID)") + invitationHandler(true, self.session) + } + +} + +extension ColorService : MCNearbyServiceBrowserDelegate { + + func browser(_ browser: MCNearbyServiceBrowser, didNotStartBrowsingForPeers error: Error) { + NSLog("%@", "didNotStartBrowsingForPeers: \(error)") + } + + func browser(_ browser: MCNearbyServiceBrowser, foundPeer peerID: MCPeerID, withDiscoveryInfo info: [String : String]?) { + NSLog("%@", "foundPeer: \(peerID)") + NSLog("%@", "invitePeer: \(peerID)") + browser.invitePeer(peerID, to: self.session, withContext: nil, timeout: 10) + } + + func browser(_ browser: MCNearbyServiceBrowser, lostPeer peerID: MCPeerID) { + NSLog("%@", "lostPeer: \(peerID)") + } + +} + +extension ColorService : MCSessionDelegate { + + func session(_ session: MCSession, peer peerID: MCPeerID, didChange state: MCSessionState) { + NSLog("%@", "peer \(peerID) didChangeState: \(state.rawValue)") + self.delegate?.connectedDevicesChanged(manager: self, connectedDevices: + session.connectedPeers.map{$0.displayName}) + } + + func session(_ session: MCSession, didReceive data: Data, fromPeer peerID: MCPeerID) { + NSLog("%@", "didReceiveData: \(data)") + let str = String(data: data, encoding: .utf8)! + self.delegate?.colorChanged(manager: self, colorString: str) + } + + func session(_ session: MCSession, didReceive stream: InputStream, withName streamName: String, fromPeer peerID: MCPeerID) { + NSLog("%@", "didReceiveStream") + } + + func session(_ session: MCSession, didStartReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, with progress: Progress) { + NSLog("%@", "didStartReceivingResourceWithName") + } + + func session(_ session: MCSession, didFinishReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, at localURL: URL?, withError error: Error?) { + NSLog("%@", "didFinishReceivingResourceWithName") + } + +} diff --git a/ConnectedColors-master/ConnectedColors/ColorSwitchViewController.swift b/ConnectedColors-master/ConnectedColors/ColorSwitchViewController.swift new file mode 100644 index 0000000..9349f10 --- /dev/null +++ b/ConnectedColors-master/ConnectedColors/ColorSwitchViewController.swift @@ -0,0 +1,66 @@ +import UIKit + +class ColorSwitchViewController: UIViewController { + + @IBOutlet weak var connectionsLabel: UILabel! + + let colorService = ColorService() + + override func viewDidLoad() { + super.viewDidLoad() + colorService.delegate = self + + let timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { timer in + let defaults = UserDefaults.standard + let status = defaults.string(forKey: "status") + print(status) + if status == "connected" { + self.view.backgroundColor = .green + } + if status == "none" { + self.view.backgroundColor = .white + } + } + } + + + @IBAction func redTapped() { + + colorService.send(colorName: "red") + } + + @IBAction func yellowTapped() { + self.change(color: .yellow) + colorService.send(colorName: "yellow") + } + + func change(color : UIColor) { + UIView.animate(withDuration: 0.2) { + self.view.backgroundColor = color + } + } + +} + +extension ColorSwitchViewController : ColorServiceDelegate { + + func connectedDevicesChanged(manager: ColorService, connectedDevices: [String]) { + OperationQueue.main.addOperation { + self.connectionsLabel.text = "Connections: \(connectedDevices)" + } + } + + func colorChanged(manager: ColorService, colorString: String) { + OperationQueue.main.addOperation { + switch colorString { + case "connected": + break + case "yellow": + break + default: + NSLog("%@", "Unknown color value received: \(colorString)") + } + } + } + +} diff --git a/ConnectedColors-master/ConnectedColors/Info.plist b/ConnectedColors-master/ConnectedColors/Info.plist new file mode 100644 index 0000000..2cf53d7 --- /dev/null +++ b/ConnectedColors-master/ConnectedColors/Info.plist @@ -0,0 +1,43 @@ + + + + + CFBundleDevelopmentRegion + en + CFBundleExecutable + $(EXECUTABLE_NAME) + CFBundleIdentifier + $(PRODUCT_BUNDLE_IDENTIFIER) + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + $(PRODUCT_NAME) + CFBundlePackageType + APPL + CFBundleShortVersionString + 1.0 + CFBundleVersion + 1 + LSRequiresIPhoneOS + + UIBackgroundModes + + fetch + processing + + UILaunchStoryboardName + LaunchScreen + UIMainStoryboardFile + Main + UIRequiredDeviceCapabilities + + armv7 + + UISupportedInterfaceOrientations + + UIInterfaceOrientationPortrait + UIInterfaceOrientationLandscapeLeft + UIInterfaceOrientationLandscapeRight + + + diff --git a/ConnectedColors-master/LICENSE b/ConnectedColors-master/LICENSE new file mode 100644 index 0000000..8ac9758 --- /dev/null +++ b/ConnectedColors-master/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2017 Ralf Ebert + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE.