How to detect iOS11 programmatically in Swift? - swift

I saw in the WWDC video that there's some new swift function to detect iOS 11 availability.
How do I detect iOS11 with Swift?

Swift 3:
if #available(iOS 11.0, *) {
// Running iOS 11 OR NEWER
} else {
// Earlier version of iOS
}
More information is available in Declaration Attributes section of Apple's Swift Programming Guide.

Objective C:
if (#available(iOS 11, *)) {
// Running iOS 11 OR NEWER
} else {
// Earlier version of iOS
}

Related

UITest tap not working on iOS 12.4 using iPhone X (works on iPhone 7)

Could this be related to SafeArea issues in iOS12.4 (and actually 12.2 as well)?
I use the following function to tap a view during my UITests.
func tapAtLocation(_ element: XCUIElement) -> Self {
let loc = element.coordinate(withNormalizedOffset: .init(dx: 0, dy: 0))
loc.tap()
return self
}
I try to tap at the location of a specific image.
So I get the image and trigger a tap
let myImage = App.images[myImageViewIdentifier].firstMatch
tapAtLocation(myImage)
It works on new iOS versions and also on iPhone 7 iOS 12.4 but not on iPhone X.
And I need it to work on iPhone X :)
What do you propose me to do? Maybe you have a nice debugging trick to see exactly where it tries to tap ?
ok ! After a day of fail and retry, a simple solution worked out!
func tapAtLocation(_ element: XCUIElement) -> Self {
let loc = element.coordinate(withNormalizedOffset: .init(dx: 0.5, dy: 0.5))
loc.tap()
return self
}
This works on all devices (iPhone X, 7 and 11) with iOS 12.4 and 13+

Swift if #available not working as expected?

I have a shared framework that runs in both iOS and MacOS, and I've run into a situation where in MacOS Catalina I need to make an extra check. I thought that by using the swift #available this would work:
if #available(OSX 10.15, *){
/// My Catalina Code
}
The truth is that this code passes in iOS. Would this be a bug, or is it not expected to be used this way ?
If you look through Apple documentation it should be like that.
if #available(macOS 10.15, *){
/// My Catalina Code
}
it should look like this:
if #available(macCatalyst 10.15, *) {
// use 10.15
} else {
}
if your using Catalyst.
other than that there is nothing that shows why your code snippet doesn't work.
You may need to use conditional compiling together with #available:
#if os(macOS)
if #available(macOS 15.0, *) {
//...
}
#endif
Or an ugly workaround:
if #available(macOS 15.0, iOS 9999.99, *) {
//...
}

swift: ios13 status bar setting error occurs in SideMenu library

I introduced SideMenu in CocoaPods, but an error occurs in the ios13 part of the status bar.But I don't know the correct code.
Would you please teach me?
var statusBarFrame: CGRect {
if #available(iOS 13.0, *) {
// How to do for iOS 13??
} else {
return UIApplication.shared.statusBarFrame
}
}
statusBarFrame has been deprecated in iOS 13,
for iOS 13 you can use this
var statusBarFrame: CGRect {
if #available(iOS 13.0, *) {
return UIApplication.shared.keyWindow!.windowScene!.statusBarManager!.statusBarFrame
} else {
return UIApplication.shared.statusBarFrame
}
}

Negate #available in Swift

iOS 13 brings in some handy extensions that I have quite a while now; I'd like to retain my code for iOS 11 and 12 and use built-in for iOS 13:
extension CIImage {
// if iOS 13 not available
open class var black: CIImage { return CIImage(color: .black) }
}

Experiencing limit in number of UILabels added as subviews

Unable to add more than three UILabels to a UIViewController’s view in the IPad Playgrounds app. Is it me or the system?
Simplified code to show the issue. Hardware is a 2018 iPad Pro running iOS 12.3.1 Playgrounds app 3.0. Using UIViews up to five can be added successfully.
import UIKit
import PlaygroundSupport
class MyViewController : UIViewController {
let square50 = CGSize(width: 50, height: 50)
override func viewDidLoad() {
var myFrame = CGRect(origin: .zero, size: square50)
for index in 0...4 {
view.addSubview(UIView(frame: myFrame)) // fails at 6th!
(view.subviews[index] as! UIView).backgroundColor = .red
myFrame.origin.y += 80
}
}
}
PlaygroundPage.current.liveView = MyViewController()
With the index range set as shown the code worked as expected, displaying the set number of coloured rectangles. With the range increased to 0...5 the runtime stopped with the message “There was a problem encountered while running this playground. Check your code for mistakes”.
Just been able to test the code on Xcode 10.3 under OS X 10.16.6 on my iMac Retina 5k 27" late 2015. There is NO problem with the code and there is no short-range limit on the number of sub-views that can be created.
The problem rests with iOS Swift Playgrounds 3.0 running on an iPad Pro 12.9 inch 3rd-gen using iOS 12.3.1. This is therefore a bug!