Hello everybody,
After weeks and months of searching for a solution (with interruptions) I finally have to ask the question: is there a tutorial to generate PDF with multiple pages from a macOS app with swift / swiftui?
I am programming a macOS app and want (must) save and print letters and other documents as PDFs at the touch of a button. All the examples that I have been able to find so far are only for iOS and simply turning "UIGraphicsPDFRenderer" into "NSGraphicsPDFRenderer" is not possible.
It can't be that no PDF can be created under macOS. It has to work somehow, doesn't it?
Alternatively, it would be enough for me if you could print a view from SwiftUI. But that doesn't seem to work that easily, too.
I would be very grateful for your help.
Post
Replies
Boosts
Views
Activity
I'd just like to know whether there's a bug in macOS or if I have to make changes in code with currency formatted TextFields.
My problem: just a simple TextField in SwiftUI like
TextField("amountOnDeadline",value: $startValue, formatter: Formatters.currencyFormatter)
The formatter ist pretty simple:
static let currencyFormatter: NumberFormatter = {
let formatter = NumberFormatter()
formatter.numberStyle = .currency
formatter.maximumFractionDigits = 2
return formatter
}()
Build and run!
The offered field is displayed as currency field. Great! But when I enter a number like 123, the field is reset to 0.00 € (or the value displayed on start).
Only when I exactly mark the 0.00 without the currency symbol and enter a value, the new value is taken.
This is not very user friendly. Is it something I have to live with or is there a solution for this problem? e.g. a special config in NumberFormatter.
I'd like to make / edit the contextMenu for entered text on a TextField / TextEditor. Does anybody know a good tutorial for that or can give short advice, how to make that.
Apple itself offers a nice piece of code for adding own addresses to "Contacts"-App. (see documentation for "Contacts" - Listing 1 Creating a contact)
I try to use this code. But when executing this, my app crashes with error on saving the data. (see image)
My code so far:
import AppKit
import Contacts
class ContactsClass {
func addToContacts(
lastName: String,
firstName: String,
birthDate: Date?,
mail: String = "",
phone1: String = "",
phone2: String = "",
mobile: String = "",
street: String,
city: String,
postCode: String
) {
// Create a mutable object to add to the contact
let contact = CNMutableContact()
contact.givenName = firstName
contact.familyName = lastName
if mail != "" {
let homeEmail = CNLabeledValue(label: CNLabelHome, value: mail as NSString)
contact.emailAddresses = [homeEmail]
}
contact.phoneNumbers = []
if phone1 != "" {
contact.phoneNumbers.append(CNLabeledValue(label: CNLabelPhoneNumberMain, value: CNPhoneNumber(stringValue: phone1)))
}
if phone2 != "" {
contact.phoneNumbers.append(CNLabeledValue(label: CNLabelPhoneNumberMain, value: CNPhoneNumber(stringValue: phone2)))
}
if mobile != "" {
contact.phoneNumbers.append(CNLabeledValue(label: CNLabelPhoneNumberMobile, value: CNPhoneNumber(stringValue: mobile)))
}
let homeAddress = CNMutablePostalAddress()
homeAddress.street = street
homeAddress.city = city
homeAddress.postalCode = postCode
contact.postalAddresses = [CNLabeledValue(label: CNLabelHome, value: homeAddress)]
if birthDate != nil {
let calendar = Calendar.current
let dateComponents = calendar.dateComponents([.year,.month,.day], from: birthDate!)
var birthday = DateComponents()
birthday.day = dateComponents.day
birthday.month = dateComponents.month
birthday.year = dateComponents.year // (Optional) Omit the year value for a yearless birthday
contact.birthday = birthday
}
// Save the newly created contact
let store = CNContactStore()
let saveRequest = CNSaveRequest()
saveRequest.add(contact, toContainerWithIdentifier: nil)
do {
try store.execute(saveRequest) // <<-- Error appears here!!
} catch {
print("Saving contact failed, error: \(error)")
// Handle the error
}
}
}
Console errors are:
2022-10-28 06:26:03.473439+0200 justCare[11808:992463] [api] Attempt to write notes by a pre-Fall-2022 app
2022-10-28 06:26:03.499714+0200 justCare[11808:992463] [plugin] CDX_AB_GetGroupCollectionPath: nil group
2022-10-28 06:26:03.499770+0200 justCare[11808:992463] [plugin] CDX_AB_GetGroupCollectionPath: nil group
2022-10-28 06:26:03.500823+0200 justCare[11808:992463] [ABCDContact] An ABCDRecord is being saved without having a container assignment. Assigning the contact to <CNCDContainer 0x600001f9bba0 ab>. Please remember to assign contacts to containers to avoid recurring container lookup and contact re-validation costs.
Access to Contatcs is granted. Sandboxing is properly set for Contacts. So why this error now?
Environment:
macOS Ventura
XCODE 14.0.1
I know, there are tons of post dealing this topic, but after days of searching for errors, solutions ans answers I'm done.
In the beginning, everything worked fine. Then, my App crashed suddenly because of a silly error in code. Never mind, I fixed this error, but since then, no syncing is done anymore.
Not enough, I recognized a field called "entityName" on Development AND Productivity which mustn't be there.
Simply deleting app and reinstall won't make anything different.
Logging out and in again (neither mac nor appleID) won't give a solution.
I can't delete my app from my mac with all it's related data (e.g. with AppCleaner) and reinstall it, because data isn't synced yet. So all my data will be deleted, too and I will lose my data.
I need help. Why is there a field, that must NOT be there? Why did iCloud stop syncing? How can I solve this, without losing my data?
Is there a "flag" in database, that controls syncing, so I can change it, using an SQL-Browser? Is there a hidden file, I can delete, to start syncing again? How can I get rid of this strange field, or is it a part of cloudkit and belongs to it?
Please excuse this vague information, but it's a) already difficult to describe the problem exactly and b) my English is pretty bad.
To describe it a little more:
I have a macBook Air (M2) as my main computer. My app is running here and it (should) sync the data to iCloud (both cloudkit and iCloud files).
Then there is a Mac mini (M2) that runs the same program in the same version under the same Apple ID. My employee should be able to view the data and change it if necessary. This worked fine for a few days, but then the syncing stopped.
From Apple's dashboard (https://icloud.developer.apple.com/) I can see that the data is definitely not arriving in the cloud anymore. (login as me)
Does anyone know if it is possible to integrate the iPhone signature function like in Preview into your own mac app? Has Apple created an API or kit here that one can use?