├── Podfile ├── README.md ├── firestoreapp.xcodeproj ├── project.pbxproj ├── project.xcworkspace │ └── contents.xcworkspacedata └── xcuserdata │ └── brianadvent.xcuserdatad │ └── xcschemes │ └── xcschememanagement.plist └── firestoreapp ├── AppDelegate.swift ├── Assets.xcassets └── AppIcon.appiconset │ └── Contents.json ├── Base.lproj ├── LaunchScreen.storyboard └── Main.storyboard ├── Info.plist ├── Sweet.swift ├── TableViewController.swift └── ViewController.swift /Podfile: -------------------------------------------------------------------------------- 1 | # Uncomment the next line to define a global platform for your project 2 | # platform :ios, '9.0' 3 | 4 | target 'firestoreapp' do 5 | # Comment the next line if you're not using Swift and don't want to use dynamic frameworks 6 | use_frameworks! 7 | 8 | # Pods for firestoreapp 9 | pod 'Firebase/Core' 10 | pod 'Firebase/Auth' 11 | pod 'Firestore', :podspec => 'https://storage.googleapis.com/firebase-preview-drop/ios/firestore/0.7.0/Firestore.podspec.json' 12 | end 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # iOS Swift Tutorial: App like Twitter with Firestore Cloud Database 2 | 3 | In this tutorial you are going to create a simple app like Twitter. You will use the new cloud database Firestore from Google. You’ll learn how to add data, how to perform simple queries and how to listen for realtime updates. 4 | 5 | ➡️ [Video on YouTube](https://youtu.be/XwXEsKRYUXU) 6 | 7 | [![Video](https://img.youtube.com/vi/XwXEsKRYUXU/0.jpg)](https://www.youtube.com/watch?v=XwXEsKRYUXU) 8 | 9 | ## Learn the basics first 10 | If you are new to Cloud Firestore, you might want to check out my basics video about firestore before getting started on this one: 11 | 12 | ➡️ [Video on YouTube](https://youtu.be/PdLzuwBrmSg) 13 | 14 | [![Video](https://img.youtube.com/vi/PdLzuwBrmSg/0.jpg)](https://www.youtube.com/watch?v=PdLzuwBrmSg) 15 | -------------------------------------------------------------------------------- /firestoreapp.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 48; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 523D149C1F82A76400DE989A /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 523D149B1F82A76400DE989A /* AppDelegate.swift */; }; 11 | 523D149E1F82A76400DE989A /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 523D149D1F82A76400DE989A /* ViewController.swift */; }; 12 | 523D14A11F82A76400DE989A /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 523D149F1F82A76400DE989A /* Main.storyboard */; }; 13 | 523D14A31F82A76400DE989A /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 523D14A21F82A76400DE989A /* Assets.xcassets */; }; 14 | 523D14A61F82A76400DE989A /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 523D14A41F82A76400DE989A /* LaunchScreen.storyboard */; }; 15 | /* End PBXBuildFile section */ 16 | 17 | /* Begin PBXFileReference section */ 18 | 523D14981F82A76400DE989A /* firestoreapp.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = firestoreapp.app; sourceTree = BUILT_PRODUCTS_DIR; }; 19 | 523D149B1F82A76400DE989A /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 20 | 523D149D1F82A76400DE989A /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; 21 | 523D14A01F82A76400DE989A /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 22 | 523D14A21F82A76400DE989A /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 23 | 523D14A51F82A76400DE989A /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 24 | 523D14A71F82A76400DE989A /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 25 | /* End PBXFileReference section */ 26 | 27 | /* Begin PBXFrameworksBuildPhase section */ 28 | 523D14951F82A76400DE989A /* Frameworks */ = { 29 | isa = PBXFrameworksBuildPhase; 30 | buildActionMask = 2147483647; 31 | files = ( 32 | ); 33 | runOnlyForDeploymentPostprocessing = 0; 34 | }; 35 | /* End PBXFrameworksBuildPhase section */ 36 | 37 | /* Begin PBXGroup section */ 38 | 523D148F1F82A76400DE989A = { 39 | isa = PBXGroup; 40 | children = ( 41 | 523D149A1F82A76400DE989A /* firestoreapp */, 42 | 523D14991F82A76400DE989A /* Products */, 43 | ); 44 | sourceTree = ""; 45 | }; 46 | 523D14991F82A76400DE989A /* Products */ = { 47 | isa = PBXGroup; 48 | children = ( 49 | 523D14981F82A76400DE989A /* firestoreapp.app */, 50 | ); 51 | name = Products; 52 | sourceTree = ""; 53 | }; 54 | 523D149A1F82A76400DE989A /* firestoreapp */ = { 55 | isa = PBXGroup; 56 | children = ( 57 | 523D149B1F82A76400DE989A /* AppDelegate.swift */, 58 | 523D149D1F82A76400DE989A /* ViewController.swift */, 59 | 523D149F1F82A76400DE989A /* Main.storyboard */, 60 | 523D14A21F82A76400DE989A /* Assets.xcassets */, 61 | 523D14A41F82A76400DE989A /* LaunchScreen.storyboard */, 62 | 523D14A71F82A76400DE989A /* Info.plist */, 63 | ); 64 | path = firestoreapp; 65 | sourceTree = ""; 66 | }; 67 | /* End PBXGroup section */ 68 | 69 | /* Begin PBXNativeTarget section */ 70 | 523D14971F82A76400DE989A /* firestoreapp */ = { 71 | isa = PBXNativeTarget; 72 | buildConfigurationList = 523D14AA1F82A76400DE989A /* Build configuration list for PBXNativeTarget "firestoreapp" */; 73 | buildPhases = ( 74 | 523D14941F82A76400DE989A /* Sources */, 75 | 523D14951F82A76400DE989A /* Frameworks */, 76 | 523D14961F82A76400DE989A /* Resources */, 77 | ); 78 | buildRules = ( 79 | ); 80 | dependencies = ( 81 | ); 82 | name = firestoreapp; 83 | productName = firestoreapp; 84 | productReference = 523D14981F82A76400DE989A /* firestoreapp.app */; 85 | productType = "com.apple.product-type.application"; 86 | }; 87 | /* End PBXNativeTarget section */ 88 | 89 | /* Begin PBXProject section */ 90 | 523D14901F82A76400DE989A /* Project object */ = { 91 | isa = PBXProject; 92 | attributes = { 93 | LastSwiftUpdateCheck = 0900; 94 | LastUpgradeCheck = 0900; 95 | ORGANIZATIONNAME = "Brian Advent"; 96 | TargetAttributes = { 97 | 523D14971F82A76400DE989A = { 98 | CreatedOnToolsVersion = 9.0; 99 | ProvisioningStyle = Automatic; 100 | }; 101 | }; 102 | }; 103 | buildConfigurationList = 523D14931F82A76400DE989A /* Build configuration list for PBXProject "firestoreapp" */; 104 | compatibilityVersion = "Xcode 8.0"; 105 | developmentRegion = en; 106 | hasScannedForEncodings = 0; 107 | knownRegions = ( 108 | en, 109 | Base, 110 | ); 111 | mainGroup = 523D148F1F82A76400DE989A; 112 | productRefGroup = 523D14991F82A76400DE989A /* Products */; 113 | projectDirPath = ""; 114 | projectRoot = ""; 115 | targets = ( 116 | 523D14971F82A76400DE989A /* firestoreapp */, 117 | ); 118 | }; 119 | /* End PBXProject section */ 120 | 121 | /* Begin PBXResourcesBuildPhase section */ 122 | 523D14961F82A76400DE989A /* Resources */ = { 123 | isa = PBXResourcesBuildPhase; 124 | buildActionMask = 2147483647; 125 | files = ( 126 | 523D14A61F82A76400DE989A /* LaunchScreen.storyboard in Resources */, 127 | 523D14A31F82A76400DE989A /* Assets.xcassets in Resources */, 128 | 523D14A11F82A76400DE989A /* Main.storyboard in Resources */, 129 | ); 130 | runOnlyForDeploymentPostprocessing = 0; 131 | }; 132 | /* End PBXResourcesBuildPhase section */ 133 | 134 | /* Begin PBXSourcesBuildPhase section */ 135 | 523D14941F82A76400DE989A /* Sources */ = { 136 | isa = PBXSourcesBuildPhase; 137 | buildActionMask = 2147483647; 138 | files = ( 139 | 523D149E1F82A76400DE989A /* ViewController.swift in Sources */, 140 | 523D149C1F82A76400DE989A /* AppDelegate.swift in Sources */, 141 | ); 142 | runOnlyForDeploymentPostprocessing = 0; 143 | }; 144 | /* End PBXSourcesBuildPhase section */ 145 | 146 | /* Begin PBXVariantGroup section */ 147 | 523D149F1F82A76400DE989A /* Main.storyboard */ = { 148 | isa = PBXVariantGroup; 149 | children = ( 150 | 523D14A01F82A76400DE989A /* Base */, 151 | ); 152 | name = Main.storyboard; 153 | sourceTree = ""; 154 | }; 155 | 523D14A41F82A76400DE989A /* LaunchScreen.storyboard */ = { 156 | isa = PBXVariantGroup; 157 | children = ( 158 | 523D14A51F82A76400DE989A /* Base */, 159 | ); 160 | name = LaunchScreen.storyboard; 161 | sourceTree = ""; 162 | }; 163 | /* End PBXVariantGroup section */ 164 | 165 | /* Begin XCBuildConfiguration section */ 166 | 523D14A81F82A76400DE989A /* Debug */ = { 167 | isa = XCBuildConfiguration; 168 | buildSettings = { 169 | ALWAYS_SEARCH_USER_PATHS = NO; 170 | CLANG_ANALYZER_NONNULL = YES; 171 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 172 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 173 | CLANG_CXX_LIBRARY = "libc++"; 174 | CLANG_ENABLE_MODULES = YES; 175 | CLANG_ENABLE_OBJC_ARC = YES; 176 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 177 | CLANG_WARN_BOOL_CONVERSION = YES; 178 | CLANG_WARN_COMMA = YES; 179 | CLANG_WARN_CONSTANT_CONVERSION = YES; 180 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 181 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 182 | CLANG_WARN_EMPTY_BODY = YES; 183 | CLANG_WARN_ENUM_CONVERSION = YES; 184 | CLANG_WARN_INFINITE_RECURSION = YES; 185 | CLANG_WARN_INT_CONVERSION = YES; 186 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 187 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 188 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 189 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 190 | CLANG_WARN_STRICT_PROTOTYPES = YES; 191 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 192 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 193 | CLANG_WARN_UNREACHABLE_CODE = YES; 194 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 195 | CODE_SIGN_IDENTITY = "iPhone Developer"; 196 | COPY_PHASE_STRIP = NO; 197 | DEBUG_INFORMATION_FORMAT = dwarf; 198 | ENABLE_STRICT_OBJC_MSGSEND = YES; 199 | ENABLE_TESTABILITY = YES; 200 | GCC_C_LANGUAGE_STANDARD = gnu11; 201 | GCC_DYNAMIC_NO_PIC = NO; 202 | GCC_NO_COMMON_BLOCKS = YES; 203 | GCC_OPTIMIZATION_LEVEL = 0; 204 | GCC_PREPROCESSOR_DEFINITIONS = ( 205 | "DEBUG=1", 206 | "$(inherited)", 207 | ); 208 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 209 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 210 | GCC_WARN_UNDECLARED_SELECTOR = YES; 211 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 212 | GCC_WARN_UNUSED_FUNCTION = YES; 213 | GCC_WARN_UNUSED_VARIABLE = YES; 214 | IPHONEOS_DEPLOYMENT_TARGET = 11.0; 215 | MTL_ENABLE_DEBUG_INFO = YES; 216 | ONLY_ACTIVE_ARCH = YES; 217 | SDKROOT = iphoneos; 218 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 219 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 220 | }; 221 | name = Debug; 222 | }; 223 | 523D14A91F82A76400DE989A /* Release */ = { 224 | isa = XCBuildConfiguration; 225 | buildSettings = { 226 | ALWAYS_SEARCH_USER_PATHS = NO; 227 | CLANG_ANALYZER_NONNULL = YES; 228 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 229 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 230 | CLANG_CXX_LIBRARY = "libc++"; 231 | CLANG_ENABLE_MODULES = YES; 232 | CLANG_ENABLE_OBJC_ARC = YES; 233 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 234 | CLANG_WARN_BOOL_CONVERSION = YES; 235 | CLANG_WARN_COMMA = YES; 236 | CLANG_WARN_CONSTANT_CONVERSION = YES; 237 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 238 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 239 | CLANG_WARN_EMPTY_BODY = YES; 240 | CLANG_WARN_ENUM_CONVERSION = YES; 241 | CLANG_WARN_INFINITE_RECURSION = YES; 242 | CLANG_WARN_INT_CONVERSION = YES; 243 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 244 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 245 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 246 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 247 | CLANG_WARN_STRICT_PROTOTYPES = YES; 248 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 249 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 250 | CLANG_WARN_UNREACHABLE_CODE = YES; 251 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 252 | CODE_SIGN_IDENTITY = "iPhone Developer"; 253 | COPY_PHASE_STRIP = NO; 254 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 255 | ENABLE_NS_ASSERTIONS = NO; 256 | ENABLE_STRICT_OBJC_MSGSEND = YES; 257 | GCC_C_LANGUAGE_STANDARD = gnu11; 258 | GCC_NO_COMMON_BLOCKS = YES; 259 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 260 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 261 | GCC_WARN_UNDECLARED_SELECTOR = YES; 262 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 263 | GCC_WARN_UNUSED_FUNCTION = YES; 264 | GCC_WARN_UNUSED_VARIABLE = YES; 265 | IPHONEOS_DEPLOYMENT_TARGET = 11.0; 266 | MTL_ENABLE_DEBUG_INFO = NO; 267 | SDKROOT = iphoneos; 268 | SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; 269 | VALIDATE_PRODUCT = YES; 270 | }; 271 | name = Release; 272 | }; 273 | 523D14AB1F82A76400DE989A /* Debug */ = { 274 | isa = XCBuildConfiguration; 275 | buildSettings = { 276 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 277 | CODE_SIGN_STYLE = Automatic; 278 | INFOPLIST_FILE = firestoreapp/Info.plist; 279 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 280 | PRODUCT_BUNDLE_IDENTIFIER = com.brianadvent.firestoreapp; 281 | PRODUCT_NAME = "$(TARGET_NAME)"; 282 | SWIFT_VERSION = 4.0; 283 | TARGETED_DEVICE_FAMILY = "1,2"; 284 | }; 285 | name = Debug; 286 | }; 287 | 523D14AC1F82A76400DE989A /* Release */ = { 288 | isa = XCBuildConfiguration; 289 | buildSettings = { 290 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 291 | CODE_SIGN_STYLE = Automatic; 292 | INFOPLIST_FILE = firestoreapp/Info.plist; 293 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 294 | PRODUCT_BUNDLE_IDENTIFIER = com.brianadvent.firestoreapp; 295 | PRODUCT_NAME = "$(TARGET_NAME)"; 296 | SWIFT_VERSION = 4.0; 297 | TARGETED_DEVICE_FAMILY = "1,2"; 298 | }; 299 | name = Release; 300 | }; 301 | /* End XCBuildConfiguration section */ 302 | 303 | /* Begin XCConfigurationList section */ 304 | 523D14931F82A76400DE989A /* Build configuration list for PBXProject "firestoreapp" */ = { 305 | isa = XCConfigurationList; 306 | buildConfigurations = ( 307 | 523D14A81F82A76400DE989A /* Debug */, 308 | 523D14A91F82A76400DE989A /* Release */, 309 | ); 310 | defaultConfigurationIsVisible = 0; 311 | defaultConfigurationName = Release; 312 | }; 313 | 523D14AA1F82A76400DE989A /* Build configuration list for PBXNativeTarget "firestoreapp" */ = { 314 | isa = XCConfigurationList; 315 | buildConfigurations = ( 316 | 523D14AB1F82A76400DE989A /* Debug */, 317 | 523D14AC1F82A76400DE989A /* Release */, 318 | ); 319 | defaultConfigurationIsVisible = 0; 320 | defaultConfigurationName = Release; 321 | }; 322 | /* End XCConfigurationList section */ 323 | }; 324 | rootObject = 523D14901F82A76400DE989A /* Project object */; 325 | } 326 | -------------------------------------------------------------------------------- /firestoreapp.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /firestoreapp.xcodeproj/xcuserdata/brianadvent.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | firestoreapp.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /firestoreapp/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.swift 3 | // firestoreapp 4 | // 5 | // Created by Brian Advent on 02.10.17. 6 | // Copyright © 2017 Brian Advent. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | @UIApplicationMain 12 | class AppDelegate: UIResponder, UIApplicationDelegate { 13 | 14 | var window: UIWindow? 15 | 16 | 17 | func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 18 | // Override point for customization after application launch. 19 | return true 20 | } 21 | 22 | func applicationWillResignActive(_ application: UIApplication) { 23 | // 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. 24 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. 25 | } 26 | 27 | func applicationDidEnterBackground(_ application: UIApplication) { 28 | // 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. 29 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 30 | } 31 | 32 | func applicationWillEnterForeground(_ application: UIApplication) { 33 | // 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. 34 | } 35 | 36 | func applicationDidBecomeActive(_ application: UIApplication) { 37 | // 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. 38 | } 39 | 40 | func applicationWillTerminate(_ application: UIApplication) { 41 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 42 | } 43 | 44 | 45 | } 46 | 47 | -------------------------------------------------------------------------------- /firestoreapp/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "20x20", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "20x20", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "29x29", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "29x29", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "40x40", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "40x40", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "iphone", 35 | "size" : "60x60", 36 | "scale" : "2x" 37 | }, 38 | { 39 | "idiom" : "iphone", 40 | "size" : "60x60", 41 | "scale" : "3x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "20x20", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "size" : "20x20", 51 | "scale" : "2x" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "size" : "29x29", 56 | "scale" : "1x" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "size" : "29x29", 61 | "scale" : "2x" 62 | }, 63 | { 64 | "idiom" : "ipad", 65 | "size" : "40x40", 66 | "scale" : "1x" 67 | }, 68 | { 69 | "idiom" : "ipad", 70 | "size" : "40x40", 71 | "scale" : "2x" 72 | }, 73 | { 74 | "idiom" : "ipad", 75 | "size" : "76x76", 76 | "scale" : "1x" 77 | }, 78 | { 79 | "idiom" : "ipad", 80 | "size" : "76x76", 81 | "scale" : "2x" 82 | }, 83 | { 84 | "idiom" : "ipad", 85 | "size" : "83.5x83.5", 86 | "scale" : "2x" 87 | } 88 | ], 89 | "info" : { 90 | "version" : 1, 91 | "author" : "xcode" 92 | } 93 | } -------------------------------------------------------------------------------- /firestoreapp/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /firestoreapp/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /firestoreapp/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | LSRequiresIPhoneOS 22 | 23 | UILaunchStoryboardName 24 | LaunchScreen 25 | UIMainStoryboardFile 26 | Main 27 | UIRequiredDeviceCapabilities 28 | 29 | armv7 30 | 31 | UISupportedInterfaceOrientations 32 | 33 | UIInterfaceOrientationPortrait 34 | UIInterfaceOrientationLandscapeLeft 35 | UIInterfaceOrientationLandscapeRight 36 | 37 | UISupportedInterfaceOrientations~ipad 38 | 39 | UIInterfaceOrientationPortrait 40 | UIInterfaceOrientationPortraitUpsideDown 41 | UIInterfaceOrientationLandscapeLeft 42 | UIInterfaceOrientationLandscapeRight 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /firestoreapp/Sweet.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Sweet.swift 3 | // firestoreapp 4 | // 5 | // Created by Brian Advent on 02.10.17. 6 | // Copyright © 2017 Brian Advent. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | import Firestore 11 | 12 | protocol DocumentSerializable { 13 | init?(dictionary:[String:Any]) 14 | } 15 | 16 | 17 | struct Sweet { 18 | var name:String 19 | var content:String 20 | var timeStamp:Date 21 | 22 | var dictionary:[String:Any] { 23 | return [ 24 | "name":name, 25 | "content" : content, 26 | "timeStamp" : timeStamp 27 | ] 28 | } 29 | 30 | } 31 | 32 | extension Sweet : DocumentSerializable { 33 | init?(dictionary: [String : Any]) { 34 | guard let name = dictionary["name"] as? String, 35 | let content = dictionary["content"] as? String, 36 | let timeStamp = dictionary ["timeStamp"] as? Date else {return nil} 37 | 38 | self.init(name: name, content: content, timeStamp: timeStamp) 39 | } 40 | } 41 | 42 | 43 | -------------------------------------------------------------------------------- /firestoreapp/TableViewController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // TableViewController.swift 3 | // firestoreapp 4 | // 5 | // Created by Brian Advent on 02.10.17. 6 | // Copyright © 2017 Brian Advent. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | import Firestore 11 | 12 | class TableViewController: UITableViewController { 13 | 14 | var db:Firestore! 15 | 16 | var sweetArray = [Sweet]() 17 | 18 | override func viewDidLoad() { 19 | super.viewDidLoad() 20 | 21 | db = Firestore.firestore() 22 | loadData() 23 | checkForUpdates() 24 | 25 | 26 | } 27 | 28 | func loadData() { 29 | db.collection("sweets").getDocuments() { 30 | querySnapshot, error in 31 | if let error = error { 32 | print("\(error.localizedDescription)") 33 | }else{ 34 | self.sweetArray = querySnapshot!.documents.flatMap({Sweet(dictionary: $0.data())}) 35 | DispatchQueue.main.async { 36 | self.tableView.reloadData() 37 | } 38 | } 39 | } 40 | 41 | 42 | } 43 | 44 | func checkForUpdates() { 45 | db.collection("sweets").whereField("timeStamp", isGreaterThan: Date()) 46 | .addSnapshotListener { 47 | querySnapshot, error in 48 | 49 | guard let snapshot = querySnapshot else {return} 50 | 51 | snapshot.documentChanges.forEach { 52 | diff in 53 | 54 | if diff.type == .added { 55 | self.sweetArray.append(Sweet(dictionary: diff.document.data())!) 56 | DispatchQueue.main.async { 57 | self.tableView.reloadData() 58 | } 59 | } 60 | } 61 | 62 | } 63 | } 64 | 65 | 66 | @IBAction func composeSweet(_ sender: Any) { 67 | 68 | let composeAlert = UIAlertController(title: "New Sweet", message: "Enter your name and message", preferredStyle: .alert) 69 | 70 | composeAlert.addTextField { (textField:UITextField) in 71 | textField.placeholder = "Your name" 72 | } 73 | 74 | composeAlert.addTextField { (textField:UITextField) in 75 | textField.placeholder = "Your message" 76 | } 77 | 78 | composeAlert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil)) 79 | 80 | composeAlert.addAction(UIAlertAction(title: "Send", style: .default, handler: { (action:UIAlertAction) in 81 | 82 | if let name = composeAlert.textFields?.first?.text, let content = composeAlert.textFields?.last?.text { 83 | 84 | let newSweet = Sweet(name: name, content: content, timeStamp: Date()) 85 | 86 | var ref:DocumentReference? = nil 87 | ref = self.db.collection("sweets").addDocument(data: newSweet.dictionary) { 88 | error in 89 | 90 | if let error = error { 91 | print("Error adding document: \(error.localizedDescription)") 92 | }else{ 93 | print("Document added with ID: \(ref!.documentID)") 94 | } 95 | 96 | } 97 | 98 | } 99 | 100 | })) 101 | 102 | self.present(composeAlert, animated: true, completion: nil) 103 | 104 | 105 | } 106 | 107 | 108 | 109 | // MARK: - Table view data source 110 | 111 | override func numberOfSections(in tableView: UITableView) -> Int { 112 | 113 | return 1 114 | } 115 | 116 | override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 117 | 118 | return sweetArray.count 119 | } 120 | 121 | 122 | override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 123 | let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) 124 | 125 | let sweet = sweetArray[indexPath.row] 126 | 127 | cell.textLabel?.text = "\(sweet.name): \(sweet.content)" 128 | cell.detailTextLabel?.text = "\(sweet.timeStamp)" 129 | 130 | return cell 131 | } 132 | 133 | 134 | 135 | 136 | } 137 | -------------------------------------------------------------------------------- /firestoreapp/ViewController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.swift 3 | // firestoreapp 4 | // 5 | // Created by Brian Advent on 02.10.17. 6 | // Copyright © 2017 Brian Advent. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | class ViewController: UIViewController { 12 | 13 | override func viewDidLoad() { 14 | super.viewDidLoad() 15 | // Do any additional setup after loading the view, typically from a nib. 16 | } 17 | 18 | override func didReceiveMemoryWarning() { 19 | super.didReceiveMemoryWarning() 20 | // Dispose of any resources that can be recreated. 21 | } 22 | 23 | 24 | } 25 | 26 | --------------------------------------------------------------------------------