is JSONSerialization.jsonObject(with: inputStream) reliable? sometimes it works fine (e.g. with small objects) and sometimes it blocks forever (easier to get the block with big objects). yet sometimes it works ok even with big objects. tried to call it on a different queue - didn't help.
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
https://developer.apple.com/documentation/foundation/urlcache
has this:
"Although URLCache instance methods can safely be called from multiple execution contexts at the same time, be aware that methods like cachedResponse(for:) and storeCachedResponse(_:for:) have an unavoidable race condition when attempting to read or write responses for the same request."
What does it mean "unavoidable"? If I put a lock (mutex / NSLock, or similar) in my wrappers on top of "cachedResponse" / "storeCachedResponse" would that avoid the mentioned race condition?
Also, what do they mean by "the same request"? A few examples below:
let url = URL(string: "https://www.apple.com")!
let req1 = URLRequest(url: url)
let req2 = req1 // perhaps "the same"
let req3 = URLRequest(url: url) // "the same"?
let req4 = URLRequest(url: req1.url!) // "the same"?
let req5 = URLRequest(url: url, cachePolicy: req1.cachePolicy, timeoutInterval: req1.timeoutInterval) // "the same"?
let req6 = URLRequest(url: url, cachePolicy: req1.cachePolicy, timeoutInterval: 1234) // "the same"?
let req7 = URLRequest(url: url, cachePolicy: .reloadIgnoringCacheData, timeoutInterval: req1.timeoutInterval) // "the same"?
assert(req1 == req2)
assert(req1 == req3)
assert(req1 == req4)
assert(req1 == req5)
assert(req1 == req6) // this is ok
assert(req1 == req7) // this fails
In the following simplified app I want to change the status bar color from default black to white (the main iPhone theme is light) but nothing else. I'm partly succeeded doing so, but the theme of keyboard is wrong: when it is first appeared it is good (light) and as soon as i start typing it changes itself to unwanted dark. Is there a way to change just the status bar color but nothing else? Note that I want the status bar color be dynamic - sometimes white, sometimes black depending upon what the app is doing.
import SwiftUI
struct ContentView: View {
@State var string = "Hello, World"
var body: some View {
TextField("EditableText", text: $string)
.font(.largeTitle)
.frame(maxHeight: 1000)
.navigationTitle("Hello, World")
.background(
Color(red: 1, green: 0.7, blue: 0.7, opacity: 1)
)
.colorScheme(.light) // attempt to "undo" the effect
.preferredColorScheme(.dark)
}
}
@main struct NavBarTestApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
PS. I don't mind dropping to UIKit for status bar handling if it is not possible to do it in SwiftUI. The rest of app itself is SwiftUI and it is quite big, here I am showing a stripped down version.
I am struggling to see why the following low-level audio recording function - which is based on tn2091 - Device input using the HAL Output Audio Unit - (a great article, btw, although a bit dated, and it would be wonderful if it was updated to use Swift and non deprecated stuff at some point!) fails to work under macOS:
func createMicUnit() -> AUAudioUnit {
let compDesc = AudioComponentDescription(
componentType: kAudioUnitType_Output,
componentSubType: kAudioUnitSubType_HALOutput, // I am on macOS, os this is good
componentManufacturer: kAudioUnitManufacturer_Apple,
componentFlags: 0, componentFlagsMask: 0)
return try! AUAudioUnit(componentDescription: compDesc, options: [])
}
func startMic() {
// mic permision is already granted at this point, but let's check
let status = AVCaptureDevice.authorizationStatus(for: AVMediaType.audio)
precondition(status == .authorized) // yes, all good
let unit = createMicUnit()
unit.isInputEnabled = true
unit.isOutputEnabled = false
precondition(!unit.canPerformInput) // can't record yet, and know why?
print(deviceName(unit.deviceID)) // "MacBook Pro Speakers" - this is why
let micDeviceID = defaultInputDeviceID
print(deviceName(micDeviceID)) // "MacBook Pro Microphone" - this is better
try! unit.setDeviceID(micDeviceID) // let's switch device to mic
precondition(unit.canPerformInput) // now we can record
print("\(String(describing: unit.channelMap))") // channel map is "nil" by default
unit.channelMap = [0] // not sure if this helps or not
let sampleRate = deviceActualFrameRate(micDeviceID)
print(sampleRate) // 48000.0
let format = AVAudioFormat(
commonFormat: .pcmFormatFloat32, sampleRate: sampleRate,
channels: 1, interleaved: false)!
try! unit.outputBusses[1].setFormat(format)
unit.inputHandler = { flags, timeStamp, frameCount, bus in
fatalError("never gets here") // now the weird part - this is never called!
}
try! unit.allocateRenderResources()
try! unit.startHardware() // let's go!
print("mic should be working now... why it doesn't?")
// from now on the (UI) app continues its normal run loop
}
All sanity checks pass with flying colors but unit's inputHandler is not being called. Any idea why?
Thank you!
Using CoreBluetooth I am getting these values from CBCentralManagerDelegate's didDiscover peripheral delegate method:
kCBAdvDataTxPowerLevel: 12 (could be other number like 7, 0 or a small negative number)
This one is taken from advertisementData parameter. This key might be absent.
rssi: -68 (or -60, -100, etc)
this is taken from the "rssi" parameter (always present).
I am looking for a formula to calculate approximate distance based on these two numbers. Is that possible?
I know that ideally I need to know rssi0 (rssi at 1 meter), but I don't see how I can get that via CoreBluetooth API or other means (without actually measuring rssi at one meter distance which is not good for me). How could I approximate rssi0 value with "kCBAdvDataTxPowerLevel"?
Hi, is it possible to have the SwiftUI's NavigationView set to show both panels side by side in landscape mode on iPhone Plus/Max sized devices? Similar to how Messages.app does it. Thank you.
When calling CBCentralManager's connectPeripheral:options: with some Bluetooth devices I'm getting the "Bluetooth Pairing Request" alert on iOS and a similar "Connection Request from:" alert on macOS. Is there a way to determine upfront if the alert is going to be presented or not? Alternatively is there a way to prohibit presenting this alert (in which case the connect request could fail, which is totally fine)? I tried specifying these options:
var manager: CBCentralManager
...
manager.connect(
peripheral,
options: [
CBConnectPeripheralOptionNotifyOnConnectionKey: false,
CBConnectPeripheralOptionNotifyOnDisconnectionKey: false,
CBConnectPeripheralOptionNotifyOnNotificationKey: false
]
)
but those didn't help (and by the doc they shouldn't help as they relate to the use case of app running in background, which is not applicable in my case – my app runs and calls connect when it is in foreground, the unwanted alert is displayed immediately).
Posting this on behalf of my colleague, who has a project in mind that requires a huge amount of RAM. Is it true that modern Mac Pro's can only have up to 192GB of RAM which is about 8 times less than 5 years old intel based Mac Pros?
i have a list like this:
id: 1, image: image1, title: title1, badge: 0
id: 2, image: image2, title: title2, badge: 0
id: 3, image: image3, title: title3, badge: 0
...
is my understanding correct that in order to do a smooth "expected" animation when I want to change both the badge of the item and its order i have to manually split this "big" update into two smaller updates (first change then move, or vice versa)?
this is somewhat surprising, i would expect a diffable implementation to have a notion of "identity" (in the example above it's "id") and calculate the differences based on that identity plus ite equivalence check rather than just based on the hash/equality check for the whole item.
hello,
i know i could be overly paranoid at times.. but
is this the case that i need to protect my global memory location (that i read from / write to) with read/write memory barriers even if i'm only accessing that location from a single serial DispatchQueue? considering the fact that GCD can pick a different thread to run operations on that queue, and a further fact that different threads can run on different cores and thus have different L1/L2 caches, so that the barrier unprotected write to a location in one code invocation (that runs on my serial queue, that happens to be bound to thread1/core1 at the time) might not yet be visible to a different invocation of my code that runs a bit later on the same serial GCD queue but it so happens that now it runs on thread2/core2, so that the barrier unprotected read from that global memory location returns some stale data?
is the answer any different if we consider a serial (maxConcurrentCount=1) OperationQueue instead of a serial dispatch queue?
finally, is the answer any different if we consider a single NSThread / pthread instead of a serial dispatch queue? can a single thread be bound to different cores during its lifetime? (e.g. work on one core, then sleep, then awake on a different core).
thank you.
Is it possible to use network from within iOS Thumbnail Extension?
I tried - it works fine under simulator, but I'm getting this error when running on real device:
networkd_settings_read_from_file Sandbox is preventing this process from reading networkd settings file at "/Library/Preferences/com.apple.networkd.plist", please add an exception.
Adding "App Transport Security Settings / Allow Arbitrary Loads" plist entry didn't help. As the error seems to be specific access to a particular file I tried adding "com.apple.security.temporary-exception.files.absolute-path.read-only" but it didn't help and looks like it couldn't help on iOS: "Note: This chapter describes property list keys specific to the macOS implementation of App Sandbox. They are not available in iOS."
Topic:
App & System Services
SubTopic:
General
Tags:
Extensions
QuickLook Thumbnailing
App Sandbox
Network
Our app has "allow arbitrary loads" in the "App Transport Security Settings" and generally allows both "http" and "https" connections. I want to restrict basic authentication usage to secure connections only, what is the best way to do that?
I have URLSessionTaskDelegate's:
func urlSession(_ session: URLSession, task: URLSessionTask, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
where I can put the relevant logic, but how do I check if the connection attempt in question is happening over TLS or not? I can check task.currentRequest.url scheme being "http" vs "https", and port being nil vs 80 vs 443, but I hope there is a more robust check.
Given an arbitrary memory address how do I find (in runtime) the nature of memory block it belongs to?
For stack addresses I guess there's some "stack start" and "stack end" of the current thread. For other threads' stacks - I guess I'd have to enumerate all threads to get those ranges. I also found that I can use malloc_size and sometimes it gives me correct result (the size if non zero at least), although it doesn't give me the beginning of the block memory address belongs to. For anything else I have no clue at the moment.
Ideal method I am looking for:
struct MemoryBlock {
let type: MemoryBlockType // stack, heap, unmapped, etc
let start: UnsafeRawPointer
let size: Int
let attributes // e.g. red / write
}
func findMemoryBlock(_ address: UnsafeRawPointer) -> MemoryBlock
PS. the language doesn't matter (e.g. can be C) so long as this method works in a swift/obj-c app.
Where's CBAdvDataManufacturerData format documented?
It looks like there's the company code stored in little endian order in the first two bytes (referencing the companies found in company_identifiers.yaml on bluetooth site). And the rest is arbitrary?
I'd like to see the official documentation about this.
Thank you.
Why is "fork" prohibited in sandboxed apps?