Foundation

RSS for tag

Access essential data types, collections, and operating-system services to define the base layer of functionality for your app using Foundation.

Posts under Foundation tag

200 Posts
Sort by:

Post

Replies

Boosts

Views

Activity

Networking Resources
General: TN3151 Choosing the right networking API Networking Overview document — Despite the fact that this is in the archive, this is still really useful. TLS for App Developers DevForums post Choosing a Network Debugging Tool documentation WWDC 2019 Session 712 Advances in Networking, Part 1 — This explains the concept of constrained networking, which is Apple’s preferred solution to questions like How do I check whether I’m on Wi-Fi? TN3135 Low-level networking on watchOS Adapt to changing network conditions tech talk Foundation networking: DevForums tags: Foundation, CFNetwork URL Loading System documentation — NSURLSession, or URLSession in Swift, is the recommended API for HTTP[S] on Apple platforms. Network framework: DevForums tag: Network Network framework documentation — Network framework is the recommended API for TCP, UDP, and QUIC on Apple platforms. Network Extension (including Wi-Fi on iOS): See Network Extension Resources Wi-Fi Fundamentals Wi-Fi on macOS: DevForums tag: Core WLAN Core WLAN framework documentation Wi-Fi Fundamentals Secure networking: DevForums tags: Security Apple Platform Security support document Preventing Insecure Network Connections documentation — This is all about App Transport Security (ATS). Available trusted root certificates for Apple operating systems support article Requirements for trusted certificates in iOS 13 and macOS 10.15 support article About upcoming limits on trusted certificates support article Apple’s Certificate Transparency policy support article Technote 2232 HTTPS Server Trust Evaluation Technote 2326 Creating Certificates for TLS Testing QA1948 HTTPS and Test Servers Miscellaneous: More network-related DevForums tags: 5G, QUIC, Bonjour On FTP DevForums post Using the Multicast Networking Additional Capability DevForums post Investigating Network Latency Problems DevForums post Local Network Privacy FAQ DevForums post Extra-ordinary Networking DevForums post Share and Enjoy — Quinn “The Eskimo!” @ Developer Technical Support @ Apple let myEmail = "eskimo" + "1" + "@" + "apple.com"
0
0
2.1k
Feb ’24
where is macOS APIs including user space file system support?
Hi there, From "Platforms State of the Union" Video macOS section I know macOS has new API of user space file system and iPhone mirroring, and delivers new APIs including user space file system support and major improvements to MapKit. But I lookup the API diff, I don't find any added API. Where can I find the user space file system API ? I really want to develop an APP which need user space file system API. Platforms State of the Union Video corresponding timeline detail: https://youtu.be/YJZ5YcMsgD4?t=3153
3
1
33
2h
The DateFormatter is returning wrong date format 2024-04-23T7:52:49.352 AMZ, 2024-05-23T11:16:24.706 a.m.Z
import Foundation let formatter = DateFormatter() let displayLocalFormat = true or false let timeZone = UTC let dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" let currentDate = Date() formatter.locale = displayLocalFormat ? Locale.current : Locale(identifier: "en_US_POSIX") formatter.dateFormat = dateFormat formatter.timeZone = timeZone formatter.string(from: date) // This function returns date format 2024-05-23T11:16:24.706 a.m.Z
4
0
129
2h
HKObserverQuery not working in background
I'm developing a single target watchOS app that obtains HealthKit information. I have the "Background Delivery" option checked under "Signing & Capabilities" for the watch target. The app does HKObserverQueries in the foreground that work as I would expect. But when I click the Digital Crown to return to clock face, the HKObserverQuery activity stops. I'm using Xcode 15.4, on Mac 14.5 and a Apple Watch Series 4 running 10.5.
2
0
103
2d
Crash objc_retain_x0
Attaching several crash traces: 2024-02-29_22-48-33.6864_-0600-3f948243e21b4c68d77a38d9cf1cecfdfe2c1565.crash 2024-03-04_15-00-02.9335_-0600-75000cd5acd63ba1434f2ffb3648b97259dddb88.crash 2024-03-05_08-55-47.2097_-0500-f682b25663107ad46f091d65f402f2be31f3f3c6.crash 2024-03-11_08-09-00.4057_-0400-e37d1a635d51afbb67ac38b42dd79c1718a408e8.crash 2024-03-15_16-20-22.6446_-0600-d4ebccf455e8305038ca564a39a5661a1dce6231.crash The final code: - (NSObject*)objectAtIndex:(NSUInteger)index { if (index < self.count) { return [self.embeddedArray objectAtIndex:index]; } else { [PNDErrorReporting reportError:PNDErrorReasonTypeSafeCollectionCrashPrevented message:@"Error msg"]; return nil; } } We subclass NSMutableArray to prevent potential crashes. but we encounter a new crash in our sdk for one of the clients. Also we noticed the stack trace skipped one of the frames (stack calls) in the crash report, in which cases the stack trace wont be identical to the actual code (beside inline)?
7
0
96
34m
NSFilePresenter Does Not Seem to Work on watchOS on Device
Hi, I submitted a Feedback Report (FB13820685) but I thought I would ask here as well because maybe I am using the framework wrong. I am using NSFilePresenter to monitor changes to a folder. On macOS, iOS (simulator), iOS (device), and watchOS (simulator) it works fine. However when running on watchOS 10.5 on device, it does not appear to work at all. I created a sample project that reproduces this problem. Am I doing something wrong? It seems like this is too basic of a problem for it to be actually broken on all Apple Watches. https://github.com/jeffreybergier/NSFilePresenterBugSampleProject
0
0
109
4d
NSString.getBytes does not crash even when an invalid range is passed.
When we pass some special words, NSString.getBytes does not crash even when we pass an invalid range. It seems a bug. The below code is an example. func testNSStringGetBytes() { let originalString: String = "􁜁あ" let bufferSize = 256 var buffer = [UInt8](repeating: 0, count: bufferSize) var usedLength = 0 // An invalid range is passed let range = NSRange(location: 0, length: originalString.count + 1) var remainingRange = NSRange() (originalString as NSString) .getBytes( &buffer, maxLength: bufferSize, usedLength: &usedLength, encoding: String.Encoding.utf8.rawValue, options: [], range: range, remaining: &remainingRange ) print("Used Length: \(usedLength)") print("Buffer: \(buffer[0..<usedLength])") if remainingRange.length > 0 { print("Did not convert the whole string. Remaining range: \(remainingRange)") } else { print("Entire string was converted successfully.") } }
1
0
164
6d
URLSessionWebSocketTask - sendPing's pongReceiveHandler called twice
Hello, I'm working on adding a URLSessionWebSocketTask based web socket connection to have live data in a single view. When the user navigates to it, the connection is established and live data is received. The task is being cancelled when this view disappears: task.cancel(with: .goingAway, reason: nil) After calling resume() on the task, I ping the server to see if the connection works before sending any messages. I opt to use async API instead of closure based wherever possible. Foundation provides both APIs for most URLSessionWebSocketTask's methods, but for some reason it misses async version of sendPing. Such method, however, is available in swift-corelibs-foundation project here. So I've added a similar code locally: extension URLSessionWebSocketTask { func sendPing() async throws { let _: Void = try await withCheckedThrowingContinuation { continuation in sendPing { error in if let error { continuation.resume(throwing: error) } else { continuation.resume() } } } } } The issue that I have is that if the user navigates from the view after sendPing was called, but before pong is received, pongReceiveHandler is called twice with error: Error Domain=NSPOSIXErrorDomain Code=53 "Software caused connection abort" UserInfo={NSDescription=Software caused connection abort} This results in an exception: Fatal error: SWIFT TASK CONTINUATION MISUSE: sendPing() tried to resume its continuation more than once, throwing Error Domain=NSPOSIXErrorDomain Code=53 "Software caused connection abort" UserInfo={NSDescription=Software caused connection abort}! There are no issues when the task is cancelled after successful ping. The documentation does not state that pongReceiveHandler is always called only once, but by looking at the code in swift-corelibs-foundation I think that it should be the case. Am I misusing sendPing, or is it a bug in the Foundation? Perhaps there is no func sendPing() async throws for some reason? I use Xcode 15.3 with Swift 5.10. Great thanks for any help. Best Regards, Michal Pastwa
3
1
153
22h
loaded array from plist. call using AppDelegate() returns empty array.
In app delegate I'm trying to load an array with strings from a plist. I print the plist it prints fine... func loadTypesArray() { guard let path = Bundle.main.path(forResource: "Types", ofType: "plist") else {return} let url = URL(fileURLWithPath: path) let data = try! Data(contentsOf: url) guard let plist = try! PropertyListSerialization.propertyList(from: data, options: .mutableContainers, format: nil) as? [String] else {return} print(plist) typesArray = plist // print(typesArray) } But when I try and access it from a different part of the app using let typesArray = AppDelegate().typesArray the array I get is an empty array! any help?
2
0
209
1w
CGRectMake: symbol not found when using dlopen/dlsym
The following program demonstrates the issue. It prints: y = 2.000000 CGRectMake symbol not found What would cause the symbol not to be found when using the dlopen/dlsym functions? #import <CoreGraphics/CoreGraphics.h> #import <dlfcn.h> int main(int argc, const char * argv[]) { CGRect rect = CGRectMake(1.0, 2.0, 3.0, 4.0); printf("y = %f\n", rect.origin.y); void * handle = dlopen("/System/Library/Frameworks/CoreGraphics.framework/Versions/A/CoreGraphics", RTLD_LAZY); if (handle == NULL) { printf("handle == NULL\n"); } else if (dlsym(handle, "CGRectMake") == NULL) { printf("CGRectMake symbol not found\n"); } return 0; } I am observing this issue with other symbols as well. What is special about them?
1
0
122
1w
I'm seeing inconsistent results with different types of Binding<>
I've created a UserDefaults extension to generate custom bindings. extension UserDefaults { func boolBinding(for defaultsKey: String) -> Binding<Bool> { return Binding ( get: { return self.bool(forKey: defaultsKey) }, set: { newValue in self.setValue(newValue, forKey: defaultsKey) }) } func cardPileBinding(for defaultsKey: String) -> Binding<CardPile> { return Binding ( get: { let rawValue = self.object(forKey: defaultsKey) as? String ?? "" return CardPile(rawValue: rawValue) ?? .allRandom }, set: { newValue in self.setValue(newValue.rawValue, forKey: defaultsKey) }) } } For the sake of completeness, here is my enum enum CardPile: String, CaseIterable { case allRandom case numbers case numbersRandom case daysMonths case daysMonthsRandom } I've also created UI elements that use these bindings: var body: some View { VStack { Toggle("Enable", isOn: UserDefaults.standard.boolBinding(for: "enable")) Picker("Card Pile", selection: UserDefaults.standard.cardPileBinding(for: "cardPile")) { ForEach(CardPile.allCases, id: \.self) { Text("\($0.rawValue)") .tag($0.rawValue) } } } } When I tap the toggle, it updates correctly. However when I tap the picker and select a different value, the binding setter gets called, but the view does not refreshed to reflect the change in value. (If I force quit the app and re-run it, the I see the change.) I would like to find out why the Binding works as I'd expected (ie updates the UI when the value changes) but the Binding behaves differently. any/all guidance very much appreciated. Note: I get the same behavior when the enum use Int as its rawValue
0
0
152
1w
sem_t in sandbox app
Hello, For educational purpose, I try to use a POSIX semaphore sem_t instead of a dispatch_semaphore_t in a sandbox macOS Obj-C app. When using sandbox, the semaphore code creation : sem_t * _unixSemaphore; char nameSemaphore[64] = {0}; snprintf(nameSemaphore, 22, "/UnixSemaphore_sample"); _unixSemaphore = sem_open(nameSemaphore, O_CREAT, 0644, 0); fails, receiving SEM_FAILED and the errno is 78 (not implemented) However, the sem_t _unixSemaphore is created and works fine when I disable sandbox I my entitlements. Is there a way to fix this? Thank you in advance Jean Marie
4
0
188
5d
Archiving my macOS target app fails with BOOL error
I'm building a macOS target for my App (which also has some Obj-C code). Building and running the app is fine but when I archive the app in XCode, the process / build fails with the following error Type 'BOOL' (aka ;Int32') cannot be used as a boolean;test for '!=0' instead It happens in a couple of places, one of the places being private func getRootDirectory(createIfNotExists: Bool = true) throws -> URL { return try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true) } where it complains that create: true is not acceptable and throws the above error. If I comment out this line, the archive works successfully. When i Cmd + click the definition of Filemanager.default.url , i get this @available(macOS 10.6, *) open func url(for directory: FileManager.SearchPathDirectory, in domain: FileManager.SearchPathDomainMask, appropriateFor url: URL?, create shouldCreate: BOOL) throws -> URL This looks fishy since it it says create shouldCreate: BOOL whereas the documentation says it should be just Bool func url( for directory: FileManager.SearchPathDirectory, in domain: FileManager.SearchPathDomainMask, appropriateFor url: URL?, create shouldCreate: Bool ) throws -> URL My minimum deployment target is macOS 13.0 I'm quite stumped at this error - which happens only while archiving. Does anybody know why?
4
0
230
1w
DANGEROUS BUG User Data is getting randomly deleted
[quote='751689021, Vlobe42, /thread/751689, /profile/Vlobe42'] this is an email I have sent to Apple with no luck: Dear Apple Developer Support Team, I am writing to seek urgent assistance with a persistent issue I have been encountering with Xcode. For several months now, every time I connect my iPhone to Xcode for development purposes, it automatically overwrites the user data of my apps with an old, seemingly random container. This issue is severely impacting my ability to continue development, as I cannot test new changes effectively. This occurs since a few months in every iOS and Xcode/macOS Version. I tried it with different Apps and Devices. Sometimes the entire Container (Documents) gets read only access so no new data can be created or changed by the user. I frequently used the replace container feature on Xcode so maybe this has something to do with it. This problem persists despite numerous attempts to resolve it on my end. I am at a critical point in my development timeline, and it is crucial for me to resolve this as soon as possible. Could you please advise on the next steps I should take to address this issue? If there are any logs or further information you require, I am more than willing to provide them. Thank you for your attention to this matter. I look forward to your prompt response and hope for a resolution soon. Best regards, Victor Lobe [/quote]
1
0
187
1w
Device Volume Changes After Setting AVAudioSession Category
Hi there, I am encountering an issue in my project which utilizes a speech recognizer and occasionally plays audio files. The problem arises when I configure the AVAudioSession and enable voice processing. The system volume changes unexpectedly and becomes uncontrollable. Specifically, the volume is excessively loud on iPhone but quite low on iPad let audioSession = AVAudioSession.sharedInstance() try audioSession.setCategory(.playAndRecord, mode: .default, options: [.defaultToSpeaker, .allowBluetooth, .interruptSpokenAudioAndMixWithOthers]) try audioSession.setActive(true, options: .notifyOthersOnDeactivation) try audioEngine.inputNode.setVoiceProcessingEnabled(true) try audioEngine.outputNode.setVoiceProcessingEnabled(true) I have provided a sample project here: Sample Project. To reproduce the issue, please follow these steps on a real device: Click on "Play recording" to hear the sound at normal volume. Click on "Start recording" to set up the category and speech recognizer. Click on "Stop recording" to stop the recording. Click on "Play recording" again and observe that the sound volume has changed. Thank you for your assistance.
0
0
175
1w
IPadOS/iOS
How do I switch a thread in my app to use ARM’s big endian mode? The thread code is complied in with the app using Xcode; however other ways of compiling/linking such as libraries, support threads or even cooperative apps might work. Open to ideas. I need to get the iPad processor into big endian mode. Please feel free to ask for any additional information. Best, BR.
1
0
165
1w
How to customize the display name of an app in the MacOS dock?
We would like to be able to modify the display name of our app in the dock and finder (etc) but not change the name of the .app bundle. We've tried modifying CFBundleName and CFBundleDisplayName in Info.plist, but this doesn't seem to have an effect. Is there any way to have the displayed name be different from the base name of the app bundle? We want this to apply for all languages. Thanks!
1
0
176
1w
strange behavior when App Lifecycle Events on an iPhone 15 Pro Max when turning off the screen using the side button in iOS 17.5.1
Hello fellow iOS developers! Using a simple app with only print lines in the SceneDelegate class, all other tested devices running iOS 17.5.1 (iPad Pro M4, iPad Pro 3rd Generation, iPhone XR) exhibit this behavior when turning off the screen using the side button: 2024-05-31 21:21:13.0260 --sceneWillResignActive 2024-05-31 21:21:13.0290 --sceneDidEnterBackground This is the same as when putting the app in the background. However, the iPhone 15 Pro Max running iOS 17.5.1 does this: 2024-05-31 9:08:28.4580 PM --sceneWillResignActive 2024-05-31 9:08:29.8310 PM --sceneDidBecomeActive 2024-05-31 9:08:29.8490 PM --sceneWillResignActive 2024-05-31 9:08:29.8510 PM --sceneDidEnterBackground I’ve also submitted this as a potential bug in the Feedback Assistant. Does anyone know why sceneDidBecomeActive() is invoked when turning off the screen with the side button in this specific case? I’m aware that using Face ID causes the app to briefly experience sceneDidBecomeActive() followed by sceneWillResignActive() during biometric authentication, and then switches back to sceneDidBecomeActive() once authentication completes. But why does this odd behavior occur when turning the screen off in simple app? Is there a way to detect that the side button has been pressed to turn off the screen? Thank you in advance, --SalCat
1
1
166
1w
Can't get the server response data while Using NSURLSession for background file upload
I am using NSURLSession for file upload and need to satisfy the following scenarios: Large file uploads Upload tasks should not be interrupted when the app is running in the background After the file upload is completed, the server returns a JSON data to inform the app of some information. When I debug with the my code attached below, I found that the urlSession(_:task:didCompleteWithError:) method is correctly called after the upload is completed, BUT the urlSession(_:dataTask:didReceive:) method is never called. Since I need to read the response data from the server after a successful file upload, if the urlSession(_:dataTask:didReceive:) method is not called, where should I get the server's response data from? PS: When I change URLSessionConfiguration.background to let config = URLSessionConfiguration.default, I can create the upload task with URLSession.uploadTask(with: request, fromFile: fileURL, completionHandler:) and get the server response data in the completionHandler. import Foundation class FileUploader: NSObject, URLSessionDelegate, URLSessionTaskDelegate, URLSessionDataDelegate { public typealias ProgressHandler = (_ bytesSent: Int64, _ totalBytes: Int64) -> Void public typealias CompletionHandler = (Error?, Data?) -> Void private var session: URLSession! private var responedData: Data?; // to hold data responsed from sever private var progressHandler: ProgressHandler? private var completionHandler: CompletionHandler? override init() { super.init() let config = URLSessionConfiguration.background(withIdentifier: "com.example.LABackgroundSession") session = URLSession(configuration: config, delegate: self, delegateQueue: OperationQueue.main) } func upload(fileURL: URL, to url: URL) { var request = URLRequest(url: url) request.httpMethod = "POST" let uploadTask = session.uploadTask(with: request, fromFile: fileURL) uploadTask.resume() } // MARK: - URLSessionDataDelegate methods public func urlSession(_ session: URLSession, task: URLSessionTask, didSendBodyData bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64) { self.progressHandler?(totalBytesSent, totalBytesExpectedToSend); } //This method never called, and there is no other way i can get the response data. func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) { self.responedData?.append(data); } func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) { if let error = error { self.completionHandler?(error, self.responedData) } else { self.completionHandler?(nil, self.responedData); } self.responedData = nil; } }
3
0
175
1w
NSarray crashes with different exceptions from the same place
It seems that that all the crashes are coming from the same place BUT the error is slightly different. Attaching the code that responsible for the crash: static NSString * const kDelimiter = @"#$@"; + (PNDArray *)getObjectsFromData:(NSData *)data { NSString *dataStr = [[NSString alloc] initWithData:data encoding:encoding]; dataStr = [dataStr stringByReplacingOccurrencesOfString:@"\\u0000" withString:@""]; NSArray *components = [dataStr componentsSeparatedByString:kDelimiter]; NSMutableArray *result = [NSMutableArray array]; for (NSString *jsonStr in components) { if (jsonStr != nil && jsonStr.length != 0 && ![jsonStr hasPrefix:kBatchUUID]) { [result addObject:jsonStr]; } } return [PNDArray arrayWithArray:result]; } 2024-04-16_17-15-34.1922_-0600-dfa2faecf702f23e3f6558bea986de4f62851761.crash 2024-04-24_04-56-53.4664_-0500-6b125d3d03b7e497b6be339c2abb52f29658824b.crash 2024-04-25_11-13-53.1326_-0700-bfe370be3eae8d65f465eac714905dd3d13aa665.crash 2024-05-03_11-47-36.6085_-0500-2793587e7ed1c02b0e4334bbc3aa0bd7f7a0cf3d.crash 2024-05-05_10-49-40.5969_-0700-4d86636b0877fceb8c0cdb9586ee16dfb0a9c934.crash
15
0
296
2d
Sometime Crash CFNetwork CFURLRequestSetHTTPRequestBody + 36
There are several crash logs Crashed: com.apple.root.default-qos EXC_BAD_ACCESS KERN_INVALID_ADDRESS 0x0000000000000038 0 CFNetwork 0x1e98 CFURLRequestSetHTTPRequestBody + 36 1 *** 0x104f7d0 -[XXXRequest getURLRequest] + 66 (XXXRequest.m:66) 2 *** 0x1051328 -[XXXRequestManager processHTTPRequest:] + 152 (XXXRequestManager.m:152) 3 *** 0x79748c __47-[XXXLog __submit:]_block_invoke + 277 (XXXLog.m:277) 4 FBLPromises 0x5138 __56-[FBLPromise chainOnQueue:chainedFulfill:chainedReject:]_block_invoke.18 + 52 5 libdispatch.dylib 0x63094 _dispatch_call_block_and_release + 24 6 libdispatch.dylib 0x64094 _dispatch_client_callout + 16 7 libdispatch.dylib 0x6924 _dispatch_queue_override_invoke + 924 8 libdispatch.dylib 0x13b94 _dispatch_root_queue_drain + 340 9 libdispatch.dylib 0x1439c _dispatch_worker_thread2 + 172 10 libsystem_pthread.dylib 0x1dc4 _pthread_wqthread + 224 11 libsystem_pthread.dylib 0x192c start_wqthread + 8
4
0
156
1w