I have a weird problem with HTTPS connection.
Task <A19A5441-F5CD-4F8C-8C88-73FC679D8AE0>.<1> finished with error [-1200] Error Domain=NSURLErrorDomain Code=-1200 "An SSL error has occurred and a secure connection to the server cannot be made."
I am trying to bypass server certificate of my website because it's self-signed.
The following code works in a test app, but not in another app. They have exactly have the same entitlements:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.security.app-sandbox</key>
<true/>
<key>com.apple.security.files.user-selected.read-write</key>
<true/>
<key>com.apple.security.network.client</key>
<true/>
</dict>
</plist>
func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge,
completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void)
{
let protectionSpace = challenge.protectionSpace
guard protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust,
protectionSpace.host.contains("mywebsite.net") else {
completionHandler(.performDefaultHandling, nil)
return
}
guard let serverTrust = protectionSpace.serverTrust else {
completionHandler(.performDefaultHandling, nil)
return
}
let credential = URLCredential(trust: serverTrust)
completionHandler(.useCredential, credential)
}
@IBAction func testMenuItem_select(_ sender: Any) {
print("\(sender)")
Preferences.instance.openTipShowed = false
testURLSession()
func testURLSession() {
let session = URLSession(configuration: URLSessionConfiguration.ephemeral,
delegate: self, delegateQueue: nil)
let url2 = "https://www.mywebsite.net/spiders.txt"
let url3 = "https://www.apple.com/"
let url = URL(string: url2)!
var request = URLRequest(url: url)
let task = session.dataTask(with: request) { data, response, error in
if let error { print(error) }
if let data {
let text = String(data: data, encoding: .utf8)
print("HTTP response object:", response ?? "")
print("HTTP resonse text:", text ?? "<empty response>")
}
}
task.resume()
}
}
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
In other languages, I am able to get current function's name using some kind of so-called reflection API. Does Swift provide similar API?
I am still on Xcode 14.3 and my macOS is version 12.7 (21G816).
Today I am surprised to find out that FileMerge tool won't run when I invoke it from Xcode "Open Developer Tool" menu.
Is there a standalone download for this tool? Or is there any better alternatives to it?
// The builtin encoding does not support GBK/GB2312
String(data: data, encoding: .GBK)
How do I convert data which is encoded in GBK/GB2312 (or anything else) to a string instance?
I have a function that computes MD5 hash of a file:
func ComputeMD5(ofFile path: String) -> [UInt8]? {
if let data = try? Data(contentsOf: URL(fileURLWithPath: path)) {
var digest = [UInt8](repeating: 0, count: 16)
data.withUnsafeBytes {
_ = CC_MD5($0.baseAddress, UInt32(data.count), &digest)
}
return digest
}
return nil
}
Now I wonder/worry what happens if the file is very huge. Does the runtime perform disk memory paging?
I cannot get any clue on the differences between these 2 functions of Array type.
Can anyone explain by examples?
I am aware Swift deliberately hides details (the actual index number) for safety, by introducing this verbose construct.
But I just got curious - is it possible to convert Index back to its underlying number?
In C++, I can write 123457890ull to imply it's an unsigned long long integer. Does Swift provide similar language construct?
Per the docs, NSImage.imageTypes returns a list UTI's, something like below:
com.adobe.pdf
com.apple.pict
com.adobe.encapsulated-postscript
public.jpeg
public.png
com.compuserve.gif
com.canon.tif-raw-image
...
What I need is get file extensions of a UTI. For example, public.jpeg picture file may have several file extensions, say .jpg,.jpeg,.jfif.
Does Cocoa provide any API to query for this information?
I vaguely remember I came across some classes about file packages. Just cannot recall the exact names. Can anyone help?
I am trying to add rows to GridView and not able to get expected row height correctly.
override func viewDidLoad() {
super.viewDidLoad()
// Remove the row in IB designer
gridView.removeRow(at: 0)
let image = NSImage(named: NSImage.colorPanelName)!
//image.size = NSMakeSize(80, 80)
let imageView = NSImageView(image: image)
imageView.imageFrameStyle = .grayBezel
imageView.imageScaling = .scaleAxesIndependently
imageView.frame = NSMakeRect(0, 0, 80, 80)
let label = NSTextField(labelWithString: "test text")
let row = gridView.addRow(with: [imageView, label])
row.height = 80
}
What was wrong with my code?
I have a single-line label whose purpose is display file path, possibly very long.
Is there any way to shorten/compact the path string (with ellipse ...) so that the label still displays full path even it's too long?
Like below:
/some/very/long/path/to/some/filename.txt
to
/some/.../filename.txt
I am reluctant to admit that I only came to know that Swift provides a builtin documentation markup syntax just a few months ago.
/** Test func
Some description here.
- Parameters:
- b:Test
- d: Test
- f: Test
- Returns: Bool
*/
func myMethod(a b:Int, c d:Int, e f:Int) -> Bool { b > d }
It seems the markup is pretty simple and has only a few keywords. But, I want to read through the complete reference. Any useful pointers?
Does Swift provide such shorthand/sugar syntax for commonly used thread synchronization?
I want to optimize file reading performance. I believe DispatchIO is the solution. Can anyone give some good pointers?