Posts

Post marked as solved
2 Replies
67 Views
I want to display a currency value in a SwiftUI app. I created a currency formatter. I created a text field, set its formatter to the currency formatter I created, and disabled the text field so people couldn't edit it. let currencyFormatter: NumberFormatter = { let formatter = NumberFormatter() formatter.numberStyle = .currency return formatter }() TextField("Tip Amount", value: $tipAmount, formatter: currencyFormatter) .disabled(true) This works well on iOS. The currency formatter works correctly. I can see the value clearly and can't change it. But on Mac the text in the disabled text field is virtually unreadable. I just want to display the value so I don't need a text field. I could use a Text view. But Text views don't support formatters as far as I can tell. How do I show a currency value with SwiftUI on Mac with readable text?
Posted
by szymczyk.
Last updated
.
Post not yet marked as solved
0 Replies
483 Views
Xcode 13.2 adds the Swift Playgrounds App project template that works with the upcoming version of Swift Playgrounds. The project template creates an iOS SwiftUI app. SwiftUI has a sprite view to show SpriteKit scenes so I tried adding a new SpriteKit scene to the project. Choosing File > New > File adds a Swift file to the project. The New File Assistant doesn't open. I can add an existing SpriteKit scene to the project. I can view and edit the scene. I can also show the scene in a sprite view so I know this project template supports SpriteKit. Adding an existing SpriteKit scene also adds a Resources folder to the project. When I choose to create a new file from the Resources folder, the New File Assistant opens. But SpriteKit Scene is not one of the file types I can add. How do I add a new SpriteKit scene file to a Swift Playgrounds App project?
Posted
by szymczyk.
Last updated
.
Post not yet marked as solved
1 Replies
212 Views
I have a multiplatform SwiftUI document app project. My app has a custom document type and can export to another type. I have both types in the document’s list of writable content types. When I save the document, the save panel on Mac has a File Format menu to save the file with an item for each writable content type. But the only type the document should save to is my custom document type. If someone chooses the exported type, they won’t be able to open the document. How do I limit the Save panel to save the document only in my custom document type? I tried removing the exported type from the list of writable content types, but when I export the file, I get the following error message in Xcode’s console: Attempting to export a document using a content type not included in its writableContentTypes. The first supported writable content type will be used instead. The export works, but the file has the extension for my custom document type. Changing the file extension in the Finder makes everything work. But I don’t want people to have to manually change file extensions. I tried taking the URL SwiftUI’s file exporter returns and changing the file extension, but I get the same error message in Xcode’s console. The exported file still has the extension for my custom document type. I also tried adding the exported type to the list of writable content types when exporting, but the writable content types array can’t be modified at runtime. What do I have to do to save documents only as my custom document type while exporting with the correct file extension?
Posted
by szymczyk.
Last updated
.
Post not yet marked as solved
5 Replies
390 Views
Many people post questions on these forums that have detailed answers in articles and blog posts from people who aren't Apple employees. It would be nice to link to these articles when answering questions on these forums. But if you try to link to a non-Apple URL, you get an error message that the domain is invalid. To post the link, you have to remove the https://www part of the link. This forces people to cut and paste the URL into the browser to read the article when they should get a clickable link. If you're worried about spam links, then either require posters to have a minimum reputation score or have a minimum number of posts to submit answers with external links.
Posted
by szymczyk.
Last updated
.
Post marked as solved
1 Replies
340 Views
I have a multiplatform document-based SwiftUI app. I have a custom document type with its own file extension that saves documents as file packages that appears as a single file in the Finder or Files app. The app exports documents as EPUB books. The document type, exported type identifier, and imported type identifier settings are the same for the iOS and Mac app targets.  I have the following code in my document struct for dealing with the document types: extension UTType { static var bookDocument: UTType { UTType(importedAs: "com.MyConpany.book")     } } static var readableContentTypes: [UTType] { [.bookDocument] } static var writableContentTypes: [UTType] { [.epub] } Everything works right on iOS. The documents appear as single files in the Files app and have my custom file extension. But on Mac the documents have the extension .epub. When I try to open a document from the Finder, the Books app launches instead of opening the document in my app. The document fails to open in the Books app because it’s not an EPUB book. Fix Attempt 1 I figured the document has the .epub extension because it’s the only writable content type for the document. I tried adding my document type to the list of writable content types. static var writableContentTypes: [UTType] { [.bookDocument, .epub]} But the document’s file extension changes to .iba, which is iBooks Author format. Opening the document in the Finder launches Pages instead of my app. From past experience the iBooks Author format is the default document format when no writable content types are supplied. Fix Attempt 2 My next attempt was to create a UTType with my custom file extension, conform to package, and add that to the writable content types array. static var bookDocumentPackage: UTType { UTType(filenameExtension: "swiftbook", conformingTo: .package)! } static var writableContentTypes: [UTType] { [.bookDocumentPackage, .epub] } But the document appears as a folder in the Finder instead of a single file. Fix Attempt 3 My next attempt was to create a UTType that exports to my custom document type and conforms to package. static var bookDocumentPackage: UTType { UTType(exportedAs: "com.MyCompany.book", conformingTo: .package) } But the document has the extension .book. Doing a Get Info on the document in the Finder shows it’s in iBooks Author format. Question What do I have to do with the UTTypes to get my document to appear as a single file in the Finder with my custom file extension?
Posted
by szymczyk.
Last updated
.
Post not yet marked as solved
1 Replies
632 Views
I'm working on a document-based SwiftUI app for Mac and iOS that uses SwiftUI's TextEditor for entering text. I want to insert markup, such as Markdown formatting, into the text editor. For example in Markdown to create bold text, I would select the text, press Cmd-B (or use a button or menu), and insert the asterisks at the start and the end of the selection. With NSTextViewand UITextView I can access the range of the selection using the selectedRange property. But looking through the documentation for TextEditor, I see no way of getting the range of the selection. Without the selection range there's no way for me to insert the markup in the correct position. How do I get the range of the selection in a SwiftUI text editor?
Posted
by szymczyk.
Last updated
.
Post marked as solved
2 Replies
350 Views
In my SwiftUI app I have a view for people to edit a book's metadata, such as its title and author. In this view I have an image view that shows the book's cover image. struct ImageView: View {     @Binding var book: Book var body: some View { // coverImage is the URL to the image file. let image = NSImage(byReferencing: book.metadata.coverFile) Image(nsImage: image) .aspectRatio(contentMode: .fit) } } When I open a book that I've set a cover image and open the metadata editor, the image view displays nothing. The metadata editor has a button to choose a cover image. When I click the button and choose an image file from the file importer, the image appears in the image view. If I close the book, reopen it, and open the metadata editor, the image appears in the image view. But if I restart the app, open the book, and open the metadata editor, the image view displays nothing. I have tried the various NSImage initializers. I tried building the image from a Data object and from NSBitmapImageRep. But when I set a breakpoint in the image view and check the image variable, it's either nil or invalid. When I check the coverfile variable, it displays the proper URL, including the .png extension. In an AppKit version of this app, I have the same code to create the NSImage, and the image displays in the image view. How do I get an existing image file to display in the image view without clicking the Choose Cover button and choosing the file again from the file importer?
Posted
by szymczyk.
Last updated
.
Post marked as solved
3 Replies
602 Views
I am trying to notarize a Mac app on Xcode 12.2. I archive the app, open the Organizer, select the archive and click the Distribute App button. I go through the steps in the wizard. Everything goes well until I click the Upload button to upload the app to Apple to notarize. When I click the Upload button, I get the following error: App Store Connect Operation Error Could not find or load main class Followed by a path to the Library.Java.Extensions folder on my Mac. I have tried cleaning the build folder and creating a fresh archive, but the upload error persists. How do I fix this error?
Posted
by szymczyk.
Last updated
.
Post marked as solved
10 Replies
1.2k Views
I'm trying to export a text document in the Documents directory and running into file permission problem in a Mac app that does not use the App Sandbox. As a workaround for the issue, I tried turning on the App Sandbox and giving read/write access to the Documents directory, but when I add the App Sandbox capability, I have the following entries for file access: User Selected File Downloads Folder Pictures Folder Music Folder Movies Folder Since the file is a text file, exporting to any of the folders in the list makes no sense. If I give User Selected File read/write access, I still get file permission errors when I export. The UI in Xcode provides no way to add folders to the File Access list. All I want to do is let someone export a file in the folder of their choice. How do I do this with the App Sandbox? UPDATE I'm developing a SwiftUI app. I was using SwiftUI's file exporter to export the document. I took robnotyou's suggestion to use NSSavePanel, but I still get the file permission error, with or without the App Sandbox. I get the following message in Xcode's console: Error: Error Domain=NSCocoaErrorDomain Code=513 "You don’t have permission to save the file If I turn on the App Sandbox, give the Downloads folder read/write permission, and export the document there, the file permissions issue goes away. I'm running macOS 11.5.2. If I open the Privacy preferences in System Preferences and select Files and Folders, I can see that some previous apps I developed appear in the list and have a checkbox selected that grants the app access to the Documents folder. The list of apps has an Add button, but it's disabled. I tried giving this app full disk access and adding it to the Developer Tools list of apps that can run software locally that does not meet the system's security policy. But the file permission error persists. I'm exporting the file in a folder inside the Documents directory. The file has read/write access for my user account and my group. The Documents folder has custom access when I get info on the folder in the file. How do I get around this file permission error?
Posted
by szymczyk.
Last updated
.
Post not yet marked as solved
8 Replies
2.1k Views
I have a toolbar in my Mac app that uses storyboards. I added a button to the toolbar. I connected the toolbar item for the button to the window controller's First Responder, with the action being an IBAction I wrote.In Interface Builder's Connections Inspector, I can see the toolbar item has a sent action to First Responder. The button itself has no sent actions.Here's the code for my IBAction.@IBAction func addTagFromToolbar(_ sender: NSToolbarItem) { let tagTitle = sender.label createTag(name: tagTitle) NotificationCenter.default.post(name: NSText.didChangeNotification, object: textView) }When I click the button, an exception is thrown on Line 2, with the following error message appearing the debug console:[NSButton label]: unrecognized selector sent to instance 0x600003508580Even though I set the type of sender to NSToolbarItem and made sure the toolbar item (and not the button) is connected to First Responder, the app is using the button as the sender.What do I have to do to make the IBAction's sender the toolbar item instead of the button?
Posted
by szymczyk.
Last updated
.
Post marked as solved
3 Replies
2.3k Views
I'm running Xcode 10. I added my Bitbucket Cloud account to Xcode. I take the following steps in Xcode:Create a new project with a local git repository.Open the Source Control navigator.Right-click on the Remotes folder and choose Create Remote to open the sheet to create the remote.Choose my Bitbucket account from the Account menu.Click the Create button.After clicking the Create button an alert opens saying that an unknown error occurred.I get this error when creating both public and private Bitbucket repositories. The error also occurs when I use HTTPS and SSH. The error also occurs if I'm signed in to Bitbucket in my web browser when creating the remote branch.I am able to push projects that are already on Bitbucket with no errors. I can also create remote branches on GitHub for new projects.What do I have to do to create a remote Bitbucket branch from Xcode?
Posted
by szymczyk.
Last updated
.
Post marked as solved
1 Replies
389 Views
On the right side of each subgroup above the list of questions is a navigation hierarchy. An example for the Cocoa Touch group is the following:Apple Developer Forums/App Frameworks/Cocoa TouchThe items in the hierarchy used to be clickable. In the Cocoa Touch example, I could click the App Frameworks part and see all the recent questions in the App Frameworks section of the forum. Now clicking the items in the navigation hierarchy does nothing. I tested on both Safari and Firefox.Is this new behavior in the navigation hierarchy expected behavior, or is there a bug in the forums?
Posted
by szymczyk.
Last updated
.
Post not yet marked as solved
0 Replies
823 Views
I have an app whose interface consists of a table view of entries and a text view to view and enter text for that entry. For a newly created entry I can enter text and scroll the text view without problems. But for other entries I have a problem with iOS 11 devices in portrait orientation. When I type enough text to force the text view to scroll, the text view does not scroll. I have to rotate the device to landscape and back to portrait to enable the text view to scroll. I do not have this issue with devices running iOS 9 and iOS 10. The text view is a vanilla text view that has scrolling enabled.As an experiment I turned off safe area layout guides for the storyboard. Doing this enabled the text view to scroll the first time I selected an entry in the table view. But if I went back to the table view and back to the text view, I got the same behavior I had with the safe area layout guides turned on. The text view would not scroll in portrait orientation unless I rotated to landscape and back to portrait.The only code I added to Apple's standard UITextView is code to show and hide the keyboard.@objc func keyboardWasShown(notification: NSNotification) { // Scroll the text view so the keyboard doesn't block what's being typed. let info = notification.userInfo if let keyboardRect = info?[UIKeyboardFrameBeginUserInfoKey] as? CGRect { let keyboardSize = keyboardRect.size textView.contentInset = UIEdgeInsetsMake(0, 0, keyboardSize.height, 0) textView.scrollIndicatorInsets = textView.contentInset } } @objc func keyboardWillBeHidden(notification: NSNotification) { textView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) textView.scrollIndicatorInsets = textView.contentInset }What do I have to do to get the text view to scroll in portrait orientation on iOS 11 devices?
Posted
by szymczyk.
Last updated
.
Post marked as solved
2 Replies
665 Views
I have a document-based Mac app that uses a storyboard. To store per-document settings, I use the metadata for the document's persistent store. I wrote a function to retrieve the metadata. The problem I am having is where to add the call to retrieve the metadata.If I add the call in the NSPersistentDocument subclass's init method, the persistent store coordinator hasn't loaded yet. The coordinator's array of stores is empty at this point, preventing me from accessing the metadata. If I add the call to the NSPersistentDocument subclass's windowControllerDidLoadNib method, the function doesn't get called. I set a breakpoint that is never hit.Where do I place the call to my function to retrieve the metadata so I can access the persistent store and its metadata?
Posted
by szymczyk.
Last updated
.