diff --git a/WidgetDemo/WidgetDemo.swift b/WidgetDemo/WidgetDemo.swift index 686041f..b3ba5a4 100644 --- a/WidgetDemo/WidgetDemo.swift +++ b/WidgetDemo/WidgetDemo.swift @@ -97,12 +97,16 @@ struct PizzaDeliveryActivityWidget: Widget { // Deep Linking HStack { Link(destination: URL(string: "pizza://TIM")!) { - Label("Contact driver", systemImage: "phone.circle.fill").padding() + Label("Contact driver", systemImage: "phone.circle.fill") + .font(.caption) + .padding() }.background(Color.accentColor) .clipShape(RoundedRectangle(cornerRadius: 15)) Spacer() Link(destination: URL(string: "pizza://cancelOrder")!) { - Label("Cancel Order", systemImage: "xmark.circle.fill").padding() + Label("Cancel Order", systemImage: "xmark.circle.fill") + .font(.caption) + .padding() }.background(Color.red) .clipShape(RoundedRectangle(cornerRadius: 15)) }.padding() diff --git a/WidgetDemoExtension.entitlements b/WidgetDemoExtension.entitlements index affff93..7bc2578 100644 --- a/WidgetDemoExtension.entitlements +++ b/WidgetDemoExtension.entitlements @@ -2,6 +2,8 @@ + aps-environment + development com.apple.security.application-groups group.io.startway.iOS16-Live-Activities diff --git a/iOS16-Live-Activities.xcodeproj/project.pbxproj b/iOS16-Live-Activities.xcodeproj/project.pbxproj index 60fae74..c62844e 100644 --- a/iOS16-Live-Activities.xcodeproj/project.pbxproj +++ b/iOS16-Live-Activities.xcodeproj/project.pbxproj @@ -320,7 +320,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 16.0; + IPHONEOS_DEPLOYMENT_TARGET = 16.1; MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; MTL_FAST_MATH = YES; ONLY_ACTIVE_ARCH = YES; @@ -374,7 +374,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 16.0; + IPHONEOS_DEPLOYMENT_TARGET = 16.1; MTL_ENABLE_DEBUG_INFO = NO; MTL_FAST_MATH = YES; SDKROOT = iphoneos; @@ -399,6 +399,7 @@ GENERATE_INFOPLIST_FILE = YES; INFOPLIST_FILE = "iOS16-Live-Activities/Info.plist"; INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.developer-tools"; + INFOPLIST_KEY_NSSupportsLiveActivities = YES; INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES; INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES; INFOPLIST_KEY_UILaunchScreen_Generation = YES; @@ -432,6 +433,7 @@ GENERATE_INFOPLIST_FILE = YES; INFOPLIST_FILE = "iOS16-Live-Activities/Info.plist"; INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.developer-tools"; + INFOPLIST_KEY_NSSupportsLiveActivities = YES; INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES; INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES; INFOPLIST_KEY_UILaunchScreen_Generation = YES; diff --git a/iOS16-Live-Activities/ContentView.swift b/iOS16-Live-Activities/ContentView.swift index 16c4850..48df416 100644 --- a/iOS16-Live-Activities/ContentView.swift +++ b/iOS16-Live-Activities/ContentView.swift @@ -22,11 +22,13 @@ struct ContentView: View { bgImage actionButtons } + .background(.black) .navigationTitle("SwiftPizza 🍕") .toolbar { ToolbarItemGroup(placement: .navigationBarLeading) { Text("For  Developers") .bold() + .foregroundColor(.white) } ToolbarItemGroup(placement: .navigationBarTrailing) { Button(action: { startPizzaAd() }) { @@ -38,7 +40,6 @@ struct ContentView: View { .tint(.red) } } - .preferredColorScheme(.dark) .onOpenURL(perform: { url in // MARK: Handle Widgets driver = url.absoluteString.replacingOccurrences(of: "pizza://", with: "") @@ -64,53 +65,58 @@ struct ContentView: View { image.resizable().scaledToFill() } placeholder: { ProgressView() - }.ignoresSafeArea(.all) + }.frame(width: UIScreen.main.bounds.size.width) + .ignoresSafeArea(.all) } var actionButtons: some View { - VStack(spacing:0) { + VStack { Spacer() - - Button(action: { showAllDeliveries() }) { - HStack { - Spacer() - Text("Show All Orders 🍕").font(.headline) - Spacer() - }.frame(height: 50) - }.tint(.brown) - - HStack(spacing:0) { - Button(action: { startDeliveryPizza() }) { + VStack(spacing:0) { + Button(action: { showAllDeliveries() }) { HStack { Spacer() - Text("Start Ordering 👨🏻‍🍳").font(.headline) + Text("Show All Orders 🍕").font(.headline) Spacer() - }.frame(height: 50) - }.tint(.blue) - Button(action: { updateDeliveryPizza() }) { + }.frame(height: 45) + }.tint(.brown) + + HStack(spacing:0) { + Button(action: { startDeliveryPizza() }) { + HStack { + Spacer() + Text("Start Ordering 👨🏻‍🍳").font(.headline) + Spacer() + }.frame(height: 45) + }.tint(.blue) + Button(action: { updateDeliveryPizza() }) { + HStack { + Spacer() + Text("Update Order 🫠").font(.headline) + Spacer() + }.frame(height: 45) + }.tint(.purple) + } + + Button(action: { stopDeliveryPizza() }) { HStack { Spacer() - Text("Update Order 🫠").font(.headline) + Text("Cancel Order 😞").font(.headline) Spacer() - }.frame(height: 50) - }.tint(.purple) - }.frame(maxWidth: UIScreen.main.bounds.size.width) - - Button(action: { stopDeliveryPizza() }) { - HStack { - Spacer() - Text("Cancel Order 😞").font(.headline) - Spacer() - }.frame(height: 50) - .padding(.bottom) - }.tint(.pink) + }.frame(height: 45) + }.tint(.pink) + } + .buttonStyle(.bordered) + .buttonBorderShape(.roundedRectangle(radius: 0)) + .background(.thickMaterial) + .cornerRadius(25) + .padding(.horizontal,5) } - .buttonStyle(.borderedProminent) - .buttonBorderShape(.roundedRectangle(radius: 0)) - .ignoresSafeArea(edges: .bottom) } // MARK: - Functions func startDeliveryPizza() { + print(ActivityAuthorizationInfo().areActivitiesEnabled) + let pizzaDeliveryAttributes = PizzaDeliveryAttributes(numberOfPizzas: 1, totalAmount:"$99") let initialContentState = PizzaDeliveryAttributes.PizzaDeliveryStatus(driverName: "TIM 👨🏻‍🍳", estimatedDeliveryTime: Date()...Date().addingTimeInterval(15 * 60)) @@ -119,15 +125,24 @@ struct ContentView: View { let deliveryActivity = try Activity.request( attributes: pizzaDeliveryAttributes, contentState: initialContentState, - pushType: nil) + pushType: .token) // Enable Push Notification Capability First (from pushType: nil) print("Requested a pizza delivery Live Activity \(deliveryActivity.id)") - - showAlert = true - alertMsg = "Requested a pizza delivery Live Activity \(deliveryActivity.id)" - + + // Send the push token to server + Task { + for await pushToken in deliveryActivity.pushTokenUpdates { + let pushTokenString = pushToken.reduce("") { $0 + String(format: "%02x", $1) } + print(pushTokenString) + + alertMsg = "Requested a pizza delivery Live Activity \(deliveryActivity.id)\n\nPush Token: \(pushTokenString)" + showAlert = true + } + } } catch (let error) { print("Error requesting pizza delivery Live Activity \(error.localizedDescription)") + alertMsg = "Error requesting pizza delivery Live Activity \(error.localizedDescription)" + showAlert = true } } func updateDeliveryPizza() { @@ -195,10 +210,14 @@ struct ContentView: View { let deliveryActivity = try Activity.request( attributes: pizzaAdAttributes, contentState: initialContentState, - pushType: nil) + pushType: .token) // Enable Push Notification Capability First (from pushType: nil) print("Requested a pizza ad Live Activity \(deliveryActivity.id)") + alertMsg = "Requested a pizza ad Live Activity \(deliveryActivity.id)" + showAlert = true } catch (let error) { print("Error requesting pizza ad Live Activity \(error.localizedDescription)") + alertMsg = "Error requesting pizza ad Live Activity \(error.localizedDescription)" + showAlert = true } } diff --git a/iOS16-Live-Activities/Info.plist b/iOS16-Live-Activities/Info.plist index 4792aac..15c4a10 100644 --- a/iOS16-Live-Activities/Info.plist +++ b/iOS16-Live-Activities/Info.plist @@ -17,5 +17,12 @@ NSSupportsLiveActivities + NSSupportsLiveActivitiesFrequentUpdates + + UIBackgroundModes + + fetch + remote-notification + diff --git a/iOS16-Live-Activities/iOS16-Live-Activities.entitlements b/iOS16-Live-Activities/iOS16-Live-Activities.entitlements index affff93..7bc2578 100644 --- a/iOS16-Live-Activities/iOS16-Live-Activities.entitlements +++ b/iOS16-Live-Activities/iOS16-Live-Activities.entitlements @@ -2,6 +2,8 @@ + aps-environment + development com.apple.security.application-groups group.io.startway.iOS16-Live-Activities diff --git a/iOS16-Live-Activities/iOS16_Live_ActivitiesApp.swift b/iOS16-Live-Activities/iOS16_Live_ActivitiesApp.swift index ceb6110..15dcffa 100644 --- a/iOS16-Live-Activities/iOS16_Live_ActivitiesApp.swift +++ b/iOS16-Live-Activities/iOS16_Live_ActivitiesApp.swift @@ -9,6 +9,17 @@ import SwiftUI @main struct iOS16_Live_ActivitiesApp: App { + init() { + // Set toolbar and navigation title text color white + let coloredAppearance = UINavigationBarAppearance() + coloredAppearance.configureWithOpaqueBackground() + coloredAppearance.backgroundColor = .clear + coloredAppearance.titleTextAttributes = [.foregroundColor: UIColor.white] + coloredAppearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white] + coloredAppearance.shadowColor = .clear + UINavigationBar.appearance().standardAppearance = coloredAppearance + UINavigationBar.appearance().scrollEdgeAppearance = coloredAppearance + } var body: some Scene { WindowGroup { ContentView()