I need to be able to detect if my ios device is connected to any network and not necessarily connected to the internet.
I have this method below but returns false when i connect via WiFi Direct to a printer's wifi connection.
/// Method to check if current network is connected to the internet
/// - returns: True if has network connectivity. False otherwise.
public func isConnectedToNetwork() -> Bool {
var zeroAddress = sockaddr_in()
zeroAddress.sin_len = UInt8(MemoryLayout<sockaddr_in>.size)
zeroAddress.sin_family = sa_family_t(AF_INET)
guard let defaultRouteReachability = withUnsafePointer(to: &zeroAddress, {
$0.withMemoryRebound(to: sockaddr.self, capacity: 1) {
SCNetworkReachabilityCreateWithAddress(nil, $0)
}
}) else {
return false
}
var flags: SCNetworkReachabilityFlags = []
if !SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags) {
return false
}
let isReachable = flags.contains(.reachable)
let needsConnection = flags.contains(.connectionRequired)
return (isReachable && !needsConnection)
}
Post
Replies
Boosts
Views
Activity
0
I followed the code snippet by @iUrii from this post: https://stackoverflow.com/a/63645453/981806. However, my app crashes and gets this error on some sites:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Completion handler passed to -[WebSource.MainViewController webView:didReceiveAuthenticationChallenge:completionHandler:] was not called' terminating with uncaught exception of type NSException (lldb)
Code snippet below:
func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
var error: CFError?
if #available(iOS 12.0, *) {
DebugLog(tag: TAG, info: "WebView: didReceive challenge - ios12 or higher")
if let trust = challenge.protectionSpace.serverTrust, !SecTrustEvaluateWithError(trust, &error) {
// OK
let ok = UIAlertAction(title: "OK", style: .default) { _ in
let exceptions = SecTrustCopyExceptions(trust)
SecTrustSetExceptions(trust, exceptions)
completionHandler(.useCredential, URLCredential(trust: trust))
}
// Cancel
let cancel = UIAlertAction(title: "Cancel", style: .cancel) { _ in
completionHandler(.cancelAuthenticationChallenge, nil)
}
// Show prompt
let message = error!.localizedDescription + "\nDo you want to continue?"
let alert = UIAlertController(title: "SSL Error", message: message, preferredStyle: .alert)
alert.addAction(ok)
alert.addAction(cancel)
DispatchQueue.main.async {
self.present(alert, animated: true, completion: nil)
}
}
else {
let cred = URLCredential(trust: challenge.protectionSpace.serverTrust!)
completionHandler(.useCredential, cred)
}
} else {
DebugLog(tag: TAG, info: "WebView: didReceive challenge - fallback older versions")
let cred = URLCredential(trust: challenge.protectionSpace.serverTrust!)
completionHandler(.useCredential, cred)
}
}
On some sites like badssl.com, the alert dialog is displaying fine and it was able to wait for user action from the dialog.
Please help on what am I missing here. Thanks!
Can't seem to successfully remove the MDM config file after processing it. It still exists even after removeObject has been called.
Snippet below:
var keyToRemove = "com.apple.configuration.managed"
UserDefaults.standard.removeObject(forKey: keyToRemove)
UserDefaults.standard.synchronize()
if UserDefaults.standard.object(forKey: keyToRemove) != nil {
print("Key \(keyToRemove) still exists after removal.")
} else {
print("Key \(keyToRemove) has been removed.")
}
I'd like to delete the mdm config after processing it.