How to test a Swift Package, that uses UIKit? - ios

I've created my first Swift Package recently. It's only used in iOS currently, and if it goes anywhere else, it might be on tvOS, but that would be it.
I was having trouble getting UIKit to be used. And I saw this note over here, and that really helped solve my problem:
https://stackoverflow.com/a/58684636/1435520
The command I used (and mentioned above) is this:
swift build -Xswiftc "-sdk" -Xswiftc "`xcrun --sdk iphonesimulator --show-sdk-path`" -Xswiftc "-target" -Xswiftc "x86_64-apple-ios13.0-simulator"
However, I'd really like to have unit tests. Running swift test... with the same arguments mentioned for swift build. But then that gives me a new error:
error: module 'XCTest' was created for incompatible target x86_64-apple-macos10.14
So, I guess I'm just wondering if it's possible to do this. Like, how could I create a Swift Package, that uses UIKit, and have it be testable?

I've been using the command xcodebuild as detailed in this forum post, which requires you to also generate a .xcodeproj:
swift package generate-xcodeproj
xcodebuild build -sdk iphoneos -scheme 'MyPackage-Package'
xcodebuild test -destination 'name=iPhone 11' -scheme 'MyPackage-Package'
But if you're using SPM Resources, you'll run into issues as I detailed here: https://bugs.swift.org/browse/SR-13773

I have spent a lot of time trying to run tests of some complex framework. None of the solutions have worked for me.
I found out that generate-xcodeproj is marked as deprecated.
The reason to that I have found here.
"... starting Xcode 11, opening and building packages is directly supported by Xcode ..."
So I run xcodebuild test directly in the package folder, without creating the xcodeproj.
xcodebuild -scheme '<package name>-Package' -sdk iphonesimulator -destination 'platform=iOS Simulator,name=iPad Air (4th generation),OS=15.0' test
And it have worked.

I was trying to build and test my internal packages on CI, nothing works for me, and generating an xcodeproj will be deprecated in the future.
What works for me is to just specify the target as macOS 10.15 by doing this without anything else:
swift test -Xswiftc -target -Xswiftc x86_64-apple-macosx10.15

Related

How can I build the Xcode-generated Swift Package Workspace with xcodebuild?

When you open a Package.swift file, Xcode will generate a workspace with the path: .swiftpm/xcode/package.xcworkspace.
I'd like to use xcodebuild to build this but it does not seem to work. The error messages are not helpful, just tells me that clang failed.
The actual command I'm running is:
xcodebuild -workspace .swiftpm/xcode/package.xcworkspace -scheme MyLibrary-Package -sdk iphonesimulator OTHER_SWIFT_FLAGS="-D SWIFT_PACKAGE"
Funny enough, trying to build a scheme that describes an individual target works:
xcodebuild -workspace .swiftpm/xcode/package.xcworkspace -scheme SpecificTarget -sdk iphonesimulator OTHER_SWIFT_FLAGS="-D SWIFT_PACKAGE"
Am I missing some build flags needed to build the combination scheme? Has anyone else struggled with this and have any advice?
Note:
The ultimate goal here is to be able to verify that my Swift Package builds in CI. If there is an easier way to achieve this goal then I'm happy to go another route. (The easier might just be multiple commands to build the individual targets but this is less robust)

How to create ios Xcodeproject using terminal

Hello I wanted to write script for xcode project creation,building and running into emulator for generic development,I tried so many ways but no result,using terminal cordova(phonegap) is creating,building and running xcode project in emulator.How they are handling,please help me to resolve this.
If i create project through xcode how can i add html and image files to xcode using terminal
for building xcode project:
xcodebuild -target ‘AppName.xcodeproj’ -scheme ‘AppName’ -configuration “Debug/Release” -sdk iphoneos6.0/5.0/4.0 -arch “armv7 armv7s” CONFIGURATION_BUILD_DIR=’BuildDirectoryName‘ ONLY_ACTIVE_ARCH=NO/YES

Building project using xcodebuild fails with Xcode 7.2

I just moved to Xcode 7.2 and am trying to compile a dynamic framework using xcodebuild and lipo
xcodebuild -project ProjectName.xcodeproj -scheme ProjectName -sdk iphonesimulator
This command fails with strange errors like Unsupported architecture (in frameworks like CoreText). The issue has also been discussed here but I don't see a solution which would work. Any help?
This is due to a bug in Xcode 7.2 (and 7.2.1).
A workaround suggestion has been provided in the supplied link since this question was asked. To sum up, you can add the parameter PLATFORM_NAME=iphonesimulator, and then it magically works. For example:
xcodebuild -project ProjectName.xcodeproj -scheme ProjectName -sdk iphonesimulator -arch i386 PLATFORM_NAME=iphonesimulator build
See also my answer here (where there is also another suggestion for a workaround)

xcodebuild equivalent of Xcode's "Product > Build For > Testing"

I'm trying to write a script that submits iOS apps to AppThwack (a 'real device' UI testing service). Their guidance is to use the Xcode GUI and build the app using the Build For > Testing option in the Xcode Product menu. This works, but I haven't been able to translate this into the xcodebuild equivalent.
More generally, how do we determine what arguments Xcode is passing to xcodebuild (assuming it uses that tool).
This is now possible as of Xcode 8 (in beta at time of writing). Use build-for-testing.
Example:
xcodebuild -workspace <workspace> -scheme <scheme> -destination 'generic/platform=iOS' build-for-testing
To switch to a beta version of xcodebuild, use xcode-select:
sudo xcode-select -switch /Applications/Xcode-beta.app/Contents/Developer
xctool has a flag called build-tests that will do just that.
(As of this post, it is not currently Xcode 7 compatible, but they are working on it.)

Using jenkins to build mulitple versions of an IOS app with a single target

I have an ios app that will have two versions (using preprocessor directives to determine what code to use for which version). I would like to automatically build each version of the app using Jenkins. I would also like to use a single target in the xcode project if possible. Is the possible using a multiple-configuration project in Jenkins; and if so, I would I go about accomplishing this?
You need to change the XCode scheme in your build script.
For example, if you had two schemes on for testing, say staging and production, you would need to have to build scripts that look similar to this.
xcodebuild -configuration Debug -scheme "Test Staging" -sdk iphonesimulator5.1 -arch i386 build
xcodebuild -configuration Debug -scheme "Test Prod" -sdk iphonesimulator5.1 -arch i386 build
Your schemes can then be configured in XCode to match the preprocessor directives.

Resources