I've got a simple app delegate method to create a core data object when my push notification arrives. The save() is returning nilError and I can't figure out what I'm doing wrong.
I've verified all the elements of Message have expected data. The attributes are just String, Data, and Date types.
class AppDelegate: NSObject, UIApplicationDelegate {
@Environment(\.managedObjectContext) private var managedObjectContext
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
guard let text = userInfo["text"] as? String,
let image = userInfo["image"] as? String,
let url = URL(string: image) else {
completionHandler(.noData)
return
}
let context = self.managedObjectContext
URLSession.shared.dataTask(with: url) { data, response, error in
guard let data = data, error == nil else {
completionHandler(.failed)
return
}
context.perform {
let message = Message(context: context)
message.text = text
message.image = data
message.received = Date()
do {
try context.save()
completionHandler(.newData)
} catch {
print(error)
completionHandler(.failed)
}
}
}.resume()
}
Post
Replies
Boosts
Views
Activity
I have an attribute on my entity with a Transformable type. It's not optional, but I don't see any way to specify a default value. Since I'm using CloudKit it's failing because there's not a default value specified. How do I provide a default value since the attribute inspector doesn't give a field for that when it's a Transformable?
I'm really confused by how to use SIWA on a website. My angular web client has the button and once clicked Apple redirects to the URL I've specified, which is my Vapor webservice. That validates the token passed in with Apple's JWK, so now I know the client is authenticated. Then I'd create the token to use for subsequent Authorization headers...how do I get that back to the client now? I feel like I'm doing something wrong here.
When sending a JSON payload, sometimes it's important to know whether a null value was sent, especially for a PATCH type operation. The struct that I decode to will obviously have that as a nullable type. However, I can't see any way to know whether it's nil because the JSON contained the key with a null value, or whether it's nil because the JSON just didn't send that key. How do you handle this?
I'm trying to make a loyalty card where you get 10 punches and then $5 off your next order. I can get the card into Wallet without issues, and I have the push notifications working to send a new version of the pass to the device. What i'm not sure how to handle though is actually "punching" the card.The only thing I can come up with is to have the pass contain a barcode that the vendor scans with their phone and that would take them to some type of webpage where the vendor types in their password and "punches" the card. That seems really cumbersome though to do. Is there a better way for the vendor to update the card without having to pull out an iOS device?Same with the $5 off coupon. Do they scan a barcode on the wallet pass and then mark it as being used, and the server then pushes a $0 update for that card back?
How do I get errors from the simulator when something is wrong with the pkpass file? I have a 'good' version of my pass.json that drags into the simulator without issues. I have a different version that does nothing, so clearly something is wrong with the JSON, but I can't find what.In the simulator I've done Debug -> Open System Log but when I drag in the bad pkpass file, nothing is displayed.
I've got a simple data model that includes a PassthroughSubject like so:final class PeopleDataModel: NSObject, ObservableObject {
var didChange = PassthroughSubject<Void, Never>()
private lazy var fetchedResultsController: NSFetchedResultsController<Person> = {
...
}()
public var people: [Person] {
return fetchedResultsController.fetchedObjects ?? []
}
}
extension PeopleDataModel: NSFetchedResultsControllerDelegate {
func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
people.forEach { print($0) }
didChange.send()
}
}So my intent is when Core Data is updated, it'll send the message out. Now I try and use it in my SwiftUI view:struct ContentView : View {
@ObservedObject var peopleDataModel = PeopleDataModel()
var body: some View {
NavigationView {
List(peopleDataModel.people) { person in
NavigationLink(destination: PersonDetailView(person: person)) {
PersonRow(person: person)
}
}
.navigationBarTitle(Text("People"))
}
}
}The view doesn't actually update with the people that have been inserted into Core Data. They show properly in the print from the data model, but don't appear in the List. How do I actually "tie" these things together so that when Core Data updates the List knows that it needs to redraw itself?
I'm trying to wrap my head around Combine still, and so in my class that handles web requests I've written this method:static func downloadNumbersPublisher(datesByType: [LottoType: (start: Date, end: Date)]) -> URLSession.DataTaskPublisher {
guard let url = downloadUrl(using: datesByType) else { return }
var request = URLRequest(url: url)
request.addValue("gzip", forHTTPHeaderField: "Accept-Encoding")
let session = URLSession(configuration: .ephemeral)
return session.dataTaskPublisher(for: request)
}What I'm not clear on though is what I actually return on line 2, the guard statement.
In Apple's documentation it says this:"This method, though convenient, is inefficient if used multiple times in succession. Achieve better performance by chaining filters without asking for the outputs of individual filters."That confuses me though because I don't know how to link them together without getting the output. For example, this is my method to apply a TiltShift filter, based on the instructions from Apple's docs. When I perform the gradient filter, I have to take the outputImage of that to pass into the next filter. What's the right way to be doing this? override public var outputImage: CIImage? {
guard let inputImage = inputImage else {
return nil
}
let clamped = inputImage.clampedToExtent()
let blurredImage = clamped.applyingGaussianBlur(sigma: inputRadius)
var gradientParameters = [
"inputPoint0": CIVector(x: 0, y: 0.75 * inputImage.extent.height),
"inputColor0": CIColor(red: 0, green: 1, blue: 0, alpha: 1),
"inputPoint1": CIVector(x: 0, y: 0.5 * inputImage.extent.height),
"inputColor1": CIColor(red: 0, green: 1, blue: 0, alpha: 0)
];
guard let gradientImage = ciImage(from: "CILinearGradient", parameters: gradientParameters) else {
return nil
}
gradientParameters["inputPoint0"] = CIVector(x: 0, y: 0.25 * inputImage.extent.height)
guard let backgroundGradientImage = ciImage(from: "CILinearGradient", parameters: gradientParameters) else {
return nil
}
let maskParameters = [
kCIInputImageKey: gradientImage,
kCIInputBackgroundImageKey: backgroundGradientImage
]
guard let maskImage = ciImage(from: "CIAdditionCompositing", parameters: maskParameters) else {
return nil
}
let combinedParameters = [
kCIInputImageKey: blurredImage,
kCIInputBackgroundImageKey: clamped,
kCIInputMaskImageKey: maskImage
]
return ciImage(from: "CIBlendWithMask", parameters: combinedParameters)
}
private func ciImage(from filterName: String, parameters: [String: Any]) -> CIImage? {
guard let filtered = CIFilter(name: filterName, parameters: parameters) else {
return nil
}
return filtered.outputImage
}
Apple's docs show using NSString.localizedUserNotificationString for local push notifications in Swift. Why not just use the normal NSLocalizedString() methods? Is there something special about the former method?
I've got an NSPopupButton where I bound the Content property to my array controller's arrangedObjects, with a model key path of 'name'. If I click on the popup, it correctly shows all of the names I expect to see. When I then select one of those names, nothing happens. The list goes away as expected, but the displayed name is still the first item in the list.
When I try to re-open the storyboard I was just editing in the current production version of Xcode it says"The document Main.storyboard could not be opened. The operation couldn't be completed. (com.apple.InterfaceBuilder error -1)"What in the world do I do to fix that? I *really* don't want to go back to my last git commit for that file because there have been quite extensive changes with pretty complex autolayout settings.
I'm trying to write a method to take a string and break it up into an array where each element of the array is the next 10 characters. So a string that's 32 characters long would end up with 3 array elements containing the first 30 characters, and then the 4th array element would have the last 2 characters. I was trying to use substringWithRange for this but as soon as I advance past the str.endIndex it throws an exception, and I can't figure out how to work around that. I know it's just a silly syntax thing, but I'm stuck.
On a view controller shown as part of a UINavigationController's stack, how do I tell if the Back button was pressed? The previously suggested answer from ages ago, to check isBeingDismissed(), seems to always return a false value.