Hello
I want to be able to save Date in @AppStorage, and it works however I was wondering what was the difference between these two extensions, which one is better and why?
extension Date: RawRepresentable {
static var dateFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateStyle = .long
return formatter
}()
public var rawValue: String {
Date.dateFormatter.string(from: self)
}
public init?(rawValue: String) {
self = Date.dateFormatter.date(from: rawValue) ?? Date()
}
}
and
extension Date: RawRepresentable {
private static let formatter = ISO8601DateFormatter()
public var rawValue: String {
Date.formatter.string(from: self)
}
public init?(rawValue: String) {
self = Date.formatter.date(from: rawValue) ?? Date()
}
}
Thank You!
Post
Replies
Boosts
Views
Activity
Hello
I am making a To-Do list app where I use CoreData and CloudKit, the problem is that when I added this line of code
container.persistentStoreDescriptions = [NSPersistentStoreDescription(url: fileContainer.appendingPathComponent("MJ.sqlite"))]
to the PersistenceController, iCloud syncing stopped working. (I need that line of code in order to permit to extensions to access the CoreData database)
Any idea to solve the problem?
This is all the PersistenceController code
struct PersistenceController {
static let shared = PersistenceController()
let container: NSPersistentCloudKitContainer
init(inMemory: Bool = false) {
container = NSPersistentCloudKitContainer(name: "MJ")
guard let fileContainer = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.name") else {
fatalError("Shared file container could not be created.")
}
container.persistentStoreDescriptions = [NSPersistentStoreDescription(url: fileContainer.appendingPathComponent("MJ.sqlite"))]
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
container.viewContext.automaticallyMergesChangesFromParent = true
container.viewContext.mergePolicy = NSMergeByPropertyStoreTrumpMergePolicy
}
}
Thank you!
Hello
How can I set up local notifications that repeat every X minutes from a certain hour to another hour.
For example, I want to receive a notification every 60 minutes starting from 12:00 AM to 10:00 PM?
func scheduleNotifications() {
let content = UNMutableNotificationContent()
content.title = "App"
content.subtitle = "App"
content.sound = UNNotificationSound.default
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: true)
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request)
}
With this code I can schedule a notification every minute, however, I cannot decide from what time it should start or when it stops.
Any ideas?
Thank you!
Hello
I am making a counter app using Core Data to store the value that the user has reached, the problem is that in the DetailView the value changes just if I close the view and reopen it, how can I change the value immediately when the user taps the button?
ContentView:
struct ContentView: View {
@Environment(\.managedObjectContext) private var viewContext
@FetchRequest(
sortDescriptors: [NSSortDescriptor(keyPath: \Item.date, ascending: true)],
animation: .default)
private var items: FetchedResults<Item>
@State private var isShown: Bool = false
var body: some View {
NavigationView{
List {
Section(header: Text("All counters")){
ForEach(items) { item in
NavigationLink(
destination:
DetailView(item: item)
.environment(\.managedObjectContext, viewContext)
,
label: {
HStack{
Text(item.name ?? "")
.font(.title3)
.fontWeight(.semibold)
Spacer()
Text("\(item.value, specifier: "%.0f")")
.font(.title3)
.fontWeight(.semibold)
}
})
}
.onDelete(perform: { indexSet in
deleteItems(offsets: indexSet)
print(items)
})
}
}
.listStyle(InsetGroupedListStyle())
.sheet(isPresented: $isShown, content: {
AddView()
.environment(\.managedObjectContext, viewContext)
})
.navigationBarTitle("Counter")
.toolbar {
Menu {
Button(action: {
isShown.toggle()
}) {
Text("Add Item")
}
EditButton()
} label: {
Image(systemName: "plus")
.font(.title)
}
}
}
}
private func deleteItems(offsets: IndexSet) {
withAnimation {
offsets.map { items[$0] }.forEach(viewContext.delete)
do {
try viewContext.save()
} catch {
print(error.localizedDescription)
}
}
}
}
DetailView:
struct DetailView: View {
@Environment(\.managedObjectContext) private var viewContext
var item: Item
var body: some View {
VStack(alignment: .center, spacing: nil, content: {
Text("\(item.value, specifier: "%.0f")")
.font(.largeTitle)
.fontWeight(.bold)
.padding(.top)
Spacer()
HStack{
Button(action: {
withAnimation{
item.value += 1
do {
try viewContext.save()
} catch {
print(error.localizedDescription)
}
}
}, label: {
Image(systemName: "plus")
})
Button(action: {
withAnimation{
item.value -= 1
do {
try viewContext.save()
} catch {
print(error.localizedDescription)
}
}
}, label: {
Image(systemName: "minus")
})
}
.foregroundColor(.primary)
Spacer()
})
.navigationBarTitle(item.name ?? "", displayMode: .inline)
}
}
Thank you
Hello
Is there a way to share data stored in CloudKit and CoreData between iOS and watchOS in SwiftUI? I am using the same CoreData file both, and I am using the same PersistenceController file both, and I am using the same CloudKit container for both.
I tried adding the App Groups capability to all the targets, but it is not working.
(I already enabled iCloud capability, Push notification capability and background Modes capability for both iOS and WatchOS)
PersistenceController:
struct PersistenceController {
static let shared = PersistenceController()
static var preview: PersistenceController = {
let result = PersistenceController(inMemory: true)
let viewContext = result.container.viewContext
do {
try viewContext.save()
} catch {
let nsError = error as NSError
fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
}
return result
}()
let container: NSPersistentCloudKitContainer
init(inMemory: Bool = false) {
container = NSPersistentCloudKitContainer(name: "Test7")
if inMemory {
container.persistentStoreDescriptions.first!.url = URL(fileURLWithPath: "/dev/null")
}
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
container.viewContext.automaticallyMergesChangesFromParent = true
container.viewContext.mergePolicy = NSMergeByPropertyStoreTrumpMergePolicy
}
}
View:
struct ContentView: View {
@FetchRequest(
entity: Item.entity(),
sortDescriptors: []
) var items: FetchedResults<Item>
@Environment(\.managedObjectContext) private var context
var body: some View {
NavigationView{
List {
ForEach(items) { item in
Text("Item at \(item.date!)")
}
.onDelete(perform: deleteItems)
}
.toolbar {
Button {
addItem()
} label: {
Image(systemName: "plus")
}
}
}
}
private func addItem() {
withAnimation {
let newItem = Item(context: context)
newItem.date = Date()
do {
try context.save()
} catch {
let nsError = error as NSError
fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
}
}
}
private func deleteItems(offsets: IndexSet) {
withAnimation {
offsets.map { items[$0] }.forEach(context.delete)
do {
try context.save()
} catch {
let nsError = error as NSError
fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
}
}
}
}
Thank you
Hello
Is there way to keep calling a function (efficiently) while the view is on screen, without using a timer, in SwiftUI?
Hello
I am developing an app with SwiftUI using CoreData and iCloudKit to sync data between platforms.
The problem is that the iCloud background update is not being triggered when staying in the application. If I make changes on both systems, the changes are being pushed, however not visible on the other device.
I need to reload the app, close the app and open again.
I already enabled iCloud capability, background notifications and push notifications.
This is my persistentContainer
var persistentContainer: NSPersistentCloudKitContainer = {
let container = NSPersistentCloudKitContainer(name: "Test7")
container.loadPersistentStores(completionHandler: {(StoreDescription, error) in
if let error = error as NSError? {
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
container.viewContext.automaticallyMergesChangesFromParent = true
container.viewContext.mergePolicy = NSMergeByPropertyStoreTrumpMergePolicy
return container
}()
func saveContext() {
let context = persistentContainer.viewContext
if context.hasChanges{
do {
try context.save()
} catch {
let nserror = error as NSError
fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
}
}
}
This is my model
class ItemsModel: ObservableObject {
init() {
readData()
}
@Published var dataInputs: [Item] = []
let context = persistentContainer.viewContext
func readData(){
let request: NSFetchRequest<Item> = Item.fetchRequest()
do {
let results = try context.fetch(request)
self.dataInputs = results
} catch {
print(error.localizedDescription)
}
}
func addItem(todo: String, date: Date){
let entity = NSEntityDescription.insertNewObject(forEntityName: "Item", into: context) as! Item
entity.todo = todo
entity.date = date
do {
try context.save()
self.dataInputs.append(entity)
} catch {
print(error.localizedDescription)
}
}
func deleteItems(indexSet: IndexSet){
for index in indexSet{
do {
let obj = dataInputs[index]
context.delete(obj)
try context.save()
let index = dataInputs.firstIndex(of: obj)
dataInputs.remove(at: index!)
} catch {
print(error.localizedDescription)
}
}
}
}
and this is my view
struct ContentView: View {
@EnvironmentObject var items: ItemsModel
var body: some View {
NavigationView{
List {
ForEach(items.dataInputs) { item in
Text("Item at \(item.date!)")
}
.onDelete(perform: items.deleteItems)
}
.toolbar {
Button {
items.addItem(todo: "Hello", date: Date())
} label: {
Image(systemName: "plus")
}
}
}
}
}
Thank you
Hello
Is there a way to sync notifications that are created locally, on CloudKit, I am using this function to create notifications and I am saving everything in an array
func scheduleNotifications(date: Date, identfier: String) {
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { success, error in
if success {
print("Success")
} else if let error = error {
print(error.localizedDescription)
}
}
let content = UNMutableNotificationContent()
content.title = "Notification"
content.body = "Notification."
content.sound = UNNotificationSound.default
var dateComponents = DateComponents()
dateComponents.hour = Int(hourFormatter.string(from: date)) ?? 0
print(hourFormatter.string(from: date))
dateComponents.minute = Int(minuteFormatter.string(from: date)) ?? 0
print(minuteFormatter.string(from: date))
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
let request = UNNotificationRequest(identifier: identfier, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request)
}
Thank you
Hello
I am trying to save some data in the Health App from my app, and it is working, the problem is that when I delete that data (already saved) from my app (using the deleteFromHealthKit function) the data is not deleted from the health app. How can I fix this?
Here is the code:
import SwiftUI
import HealthKit
struct ContentView: View {
init() {
//--------
let healthStore = HKHealthStore()
let allTypes = Set([HKObjectType.quantityType(forIdentifier: .dietaryWater)!])
healthStore.requestAuthorization(toShare: allTypes, read: allTypes) { (success, error) in
if !success {
print("success")
}
}
}
func fetchHealthData(date: Date, ml: Double) -> Void {
let healthStore = HKHealthStore()
let quantityType = HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.dietaryWater)
let waterConsumed = HKQuantitySample.init(type: quantityType!, quantity: .init(unit: HKUnit.literUnit(with: .milli), doubleValue: ml), start: date, end: date)
healthStore.save(waterConsumed) { success, error in
if (error != nil) {
print("Error: \(String(describing: error))")
}
if success {
print("Saved: \(success)")
}
}
}
@State var water: [Water] = []
@State private var value: Double = 0
func deleteFromHealthKit(date: Date, ml: Double) {
let healthStore = HKHealthStore()
let quantityType = HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.dietaryWater)
let waterConsumed = HKQuantitySample.init(type: quantityType!, quantity: .init(unit: HKUnit.literUnit(with: .milli), doubleValue: ml), start: date, end: date)
healthStore.delete(waterConsumed) { success, error in
if (error != nil) {
print("Error: \(String(describing: error))")
}
if success {
print("Saved: \(success)")
}
}
}
var body: some View {
NavigationView{
VStack{
Text("Value: \(value)")
.padding()
HStack{
Text("100 ml")
.onTapGesture {
value = 100
}
Text("200 ml")
.onTapGesture {
value = 200
}
}
Button("Add"){
water.append(Water(value: value, date: Date()))
fetchHealthData(date: Date(), ml: value)
}.disabled(value == 0 ? true : false)
.padding()
List{
ForEach(0..<water.count, id: \.self){ i in
HStack{
Text("\(water[i].value)")
Text("\(water[i].date)")
}
.onTapGesture {
deleteFromHealthKit(date: water[i].date, ml: water[i].value)
water.remove(at: i)
}
}
}
}
}
}
}
struct Water: Identifiable {
var id = UUID()
var value: Double
var date: Date
}
Thank you
Hello
I implemented the filter function in my ForEach loop, and it works just with the valori property but not with the date property , is there a way to let it filter also the date?
I tried to remove the dateFormatter but it didn't work.
Here is the code
import SwiftUI
let dateFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateStyle = .medium
return formatter
}()
struct Test4: View {
@State private var text: String = ""
var body: some View {
NavigationView{
if !lifetimes.isEmpty{
List{
Section(header: Text("")){
TextField("Search", text: $text)
}
Section(header: Text("")){
ForEach(lifetimes.filter { text.isEmpty || "\($0)".contains(text) }, id: \.id){ lifetimeInputs in
HStack{
Text("\(lifetimeInputs.valori, specifier: "%.0f")")
Spacer()
Text("\(dateFormatter.string(from: lifetimeInputs.date))")
}
}
}
}
.listStyle(InsetGroupedListStyle())
.navigationTitle("All History")
} else{
VStack{
Text("No Data")
.font(.largeTitle)
.fontWeight(.semibold)
.foregroundColor(.secondary)
}
.padding(.bottom)
.navigationTitle("All History")
}
}
}
}
struct LifetimeInputsModel: Identifiable {
var id = UUID()
var valori: Double
var date: Date
}
var lifetimes: [LifetimeInputsModel] = [
LifetimeInputsModel(valori: 300, date: Date()),
LifetimeInputsModel(valori: 200, date: Date() + 86400)
]
Thank you
Hello
import SwiftUI
enum When: String {
case Today
case Week
case Month
case Year
}
struct ChartView: View {
var when: When
@EnvironmentObject var millilitriInseritiModel: MillilitriInseritiModel
var valoriAsseX: [String]{
if when == When.Week{
return ["M", "T", "W", "T", "F", "S", "S"]
} else if when == When.Month{
return ["7", "14", "21", "28"]
} else if when == When.Year{
return ["J", "F", "M", "A", "M", "J", "J", "A", "S", "O", "N", "D"]
}
return []
}
var valoriAsseY: [Double]{
return []
}
var dates: [Date] = [Date(), Date().addingTimeInterval(86400), Date().addingTimeInterval(86400 * 2)]
@State var valori: [Double] = [1000, 2000, 3000, 1000, 2000, 1000, 2000, 3000, 1000, 2000, 3000]
var altezzaRettangolo: [Double]?{
var altezze: [Double] = []
for i in 0..<valori.count{
altezze.append(valori[i])
}
return altezze
}
@State var animation: Bool = false
var body: some View{
HStack(alignment: .bottom, spacing: 8, content: {
ForEach(valoriAsseX.indices){ i in
VStack{
RoundedRectangle(cornerRadius: 3)
.fill(LinearGradient(gradient: Gradient(colors: [Color.red, Color.blue]), startPoint: .top, endPoint: .bottom))
.frame(width: 40, height: animation ? CGFloat(altezzaRettangolo?[i] ?? 0) / 7 : 0)
.animation(.easeInOut)
Text(valoriAsseX[i])
.fontWeight(.semibold)
.multilineTextAlignment(.leading)
.onTapGesture {
withAnimation{
valori[i] += 100
}
}
}
}
})
.padding()
.onAppear {
animation = true
}
.onDisappear {
animation = false
}
}
}
struct ChartView_Previews: PreviewProvider {
static var previews: some View {
ChartView(when: When.Year)
}
}
As you might notice, in the previews I set when to When.Year, that is crashing the app because the array valoriAsseX is bigger than the array altezzaRettangolo, so when I iterate it in the ForEach loop it crashes, I can't find any way to solve this, I tried an if let but it is crashing anyway, any ideas?
Thank you
Hello
Is there a way to execute a function (even if the app is closed) at a certain time, for example 12:30 AM?
Hello
I have a Raindrop shape:
struct Raindrop: Shape {
func path(in rect: CGRect) - Path {
Path { path in
path.move(to: CGPoint(x: rect.size.width / 2, y: 0))
path.addQuadCurve(to: CGPoint(x: rect.size.width / 2, y: rect.size.height), control: CGPoint(x: rect.size.width, y: rect.size.height))
path.addQuadCurve(to: CGPoint(x: rect.size.width / 2, y: 0), control: CGPoint(x: 0, y: rect.size.height))
}
}
}
When I trim it in the ContentView, it trims from right to left, is there a way to trim it from top to bottom?
Raindrop()
.trim(from: 0.9, to: 1)
.scaledToFit()
Thank you
Hello
Is there a way to know if the detected finger or face are wrong?
I am using this function:
func authenticate() {
let context = LAContext()
var error: NSError?
if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) {
let reason = "We need to unlock your data."
context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reason) { success, authenticationError in
DispatchQueue.main.async {
if success {
self.isUnlocked = true
} else {
userPressedCancel = false
}
}
}
} else {
}
}
Hello
Why does it give me an error when I pass 'name' at line 3?
struct OtherView: View {
@State private var name: String = ""
@ObservedObject var use = Use(name: name)
var body: some View{
VStack{
}
}
}
class Use: ObservableObject {
@Published var name: String
init(name: String) {
self.name = name
}
}
Thank you