No such module "PackageDescription" - ios

I just started with swift 3 and made a simple app in xcode using Swift 3.0 . Now i want to add a third party library using Swift Package Manager . I am following installation method given in this link . I created Package.swift file which looks like this
import PackageDescription
let package = Package (
name : "SwiftPM",
dependencies : [
.Package(url: "https://github.com/ArtSabintsev/Siren.git", majorVersion: 1)
])
but i get error No such module "PackageDescription"

The Swift Package Manager and Xcode are orthogonal. That is, you can't expect to compile Package.swift in Xcode; it simply won't work. Instead, until Xcode supports the package manager, you need to have two distinct builds - one with the package manager and one with Xcode.
So, using the Swift Package Manager, once you've defined Package.swift and formulated your directory structure as expected by the package manger, you perform simply:
swift build
Then for Xcode, you create an Xcode project that uses your source code, but not Package.swift. You'll need to clone the Siren.git project, explicitly - into your Xcode build's source files.

Swift 3/4
Navigate to your project folder through a terminal and run these commands swift package init --type library first and then swift package generate-xcodeproj
Reference

For me the issue turned out to be Target Membership. I created the Package.swift file manually inside an iOS app project.
Solution:
Select and go to the Package.swift file
Open the File Inspector on the right side
Deselect any modules inside Target Membership section

Refer to the solution in this link:
https://forums.kodeco.com/t/server-error-no-such-module-packagedescription/177438
Command:
sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer

For other people who faced the same error as this:
Fix your Package.swift syntax
I got this error because Xcode hadn't parsed Package.swift fully yet, because of syntax errors.
In my case, I had a .target(name: "name-of-target", dependencies: [""]). As soon as I removed the empty dependency string (""), Xcode immediately parsed the file, and the error disappeared. Remember to save the file though.
Also, GoZoner and Pratiks answers are outdated:
Xcode has integration with Swift Packages now (as shown above)
swift package generate-xcodeproj is deprecated, shown by the error message when using it:
warning: Xcode can open and build Swift Packages directly. 'generate-xcodeproj' is no longer needed and will be deprecated soon.
More information about what that is, and why its gone here.

Related

Firebase Crashlytics | Swift Package Manager (SPM) Run Script?

I want to add Crashlytics to my app using the Swift package manager.
Now, since the usual way to install Firebase is through CocoaPods, the tutorial on how to set up Crashlytics properly is also adjusted for CocoaPods.
In other words - the run script we should copy from the tutorial is not compatible with SPM, as you can see here:
${PODS_ROOT}/FirebaseCrashlytics/run
So how can I find the file location of Crashlytics using SPM to get the correct run script?
Thanks!
Turns out its inside the DerivedData folder:
~/Library/Developer/Xcode/DerivedData/YOURAPP-.../SourcePackages/checkouts/firebase-ios-sdk/Crashlytics/run
For your convenience, you can use a build variable to generalize the folder path:
${BUILD_DIR%Build/*}SourcePackages/checkouts/firebase-ios-sdk/Crashlytics/run
Finally, your run script should look something like that:
And another quite important thing if a non-default name for the firebase configuration file is used.
This flag is needed to denote it:
-gsp ${PROJECT_DIR}/PATH-TO-CONFIG/GoogleService-Info-Dev.plist
Thus final script should look like
${BUILD_DIR%Build/*}SourcePackages/checkouts/firebase-ios-sdk/Crashlytics/run -gsp ${PROJECT_DIR}/PATH-TO-CONFIG/GoogleService-Info-Dev.plist
Where PATH-TO-CONFIG is the path to the firebase configuration file in the project.
Thanks for the suggestions. I'm unable to make this work. I get a crash and the following error message:
/Users/[path-to-my-project]/SourcePackages/checkouts/firebase-ios-sdk/Crashlytics/run: No such file or directory
Command PhaseScriptExecution failed with a nonzero exit code
I don't understand why it tries to find the crashlytics script in my Xcode project folder and not in the derived data folder at runtime.
This worked for me, but I had to change Project Settings.
"${BUILD_DIR%Build/*}/SourcePackages/checkouts/firebase-ios-sdk/Crashlytics/run"
Add a package by selecting File → Add Packages… in Xcode’s menu bar.
Search for the Firebase Apple SDK using the repo's URL:
https://github.com/firebase/firebase-ios-sdk.git
Next, set the Dependency Rule to be Up to Next Major Version and specify 8.10.0 as the lower bound.
Then, select Add Package.
Choose the Firebase products that you want installed in your app.
If you've installed FirebaseAnalytics, add the -ObjC option to Other Linker Flags in the Build Settings tab.

Module 'Swift' was created for incompatible target x86_64-apple-ios13.0

I'm creating an iOS framework and I want to copy some Xcode templates from my framework directory (that are not included in my .xcproject, but are in the folder that contains the project), when my framework is installed trought Cocoapods. In other words, when a developer installs my framework with Cocoapods or manually, I want to copy the templates into his Xcode Template Files folder.
I'm trying to execute a swift script file from the build phase of Xcode like this:
swift "${SRCROOT}/Folder/Folder/installer.swift"
But I get this error when I try to build it:
/<unknown>:1:1: module 'Swift' was created for incompatible target x86_64-apple-ios13.0: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/iphonesimulator/prebuilt-modules/Swift.swiftmodule/x86_64.swiftmodule
If I execute swift installer.swift from the terminal, the script works. So, I think there is a problem with my Xcode.
My installer.swift file copies the template files to the Xcode Template Files folder.
I don't know if this way is the correct one, but I didn't find any other solution so far.
When I tried something similar I had to tell swift to compile for macOS by adding the following "shebang" comment as the first line of the swift script:
#!/usr/bin/env xcrun --sdk macosx swift

Using Swift3 Package Manager with Xcode 8 with no luck

I'm trying to integrate swift package manager in my project, but with no luck. The iOS simulator crashes at startup.
Here are the steps that I followed in order to integrate the package manager:
open xcode and create a new xcodeproject: testcpackage
from the terminal I've run the command:
swift package init to create the Package.swift file
swift package generate-xcodeproj
swift build
swift test - everything seems to be ok
open the new generated file testpackage.xcodeproj in xcode
run the application (cmd+R): build succeeded
first error given by the iOS simulator: Could not attach to pid : “9207”
I close the iOS simulator and I run the project again when I get the 2nd error:
No installable items found at /Users/xxxxxx/Library/Developer/CoreSimulator/Devices/555xxx14-xxx-xx-8E6E-C13D5AB463A0/data/Library/Caches/com.apple.mobile.installd.staging/temp.rA1vGO/extracted/Payload
Could you please help me to solve this issue?
I use the following method to get packages to work in iOS. That being said, I'm not sure if this is the "best" way to do this:
Make starting Package.swift file
swift package generate-xcodeproj
Open project in Xcode
Project : Build Settings : Base SDK ==> change to IOS
Editor : Add Target : (iOS ... Single View App)
Select your project, look at target list, find your newly added target : General : Deployment Info
On toolbar, change "active scheme" to the new target
Run
It has worked for me in the past. However, I have had to fix search paths. I'm currently suffering from that problem here: Xcode: missing required module error
Good luck

Use swift package manager on existing xcode project

im new to the swift and xcode world, so i'm having a problem trying to integrate a package to my project.
I want to add Alamofire dependency, with the following commands:
Inside my root project folder:
swift init
this creates the Package.swift file, i add the dependency inside, run then:
swift build
Everything seems to be ok, but im my project when i try to import my library:
import Alamofire
I get an error, it says that the module is not recognized.
So my question here is, what is the correct steps to integrate Package Manager and a dependency on a existing project without crashing everything.
UPDATE:
swift build
outputs:
Resolved version: 4.3.0
Compile Swift Module 'Alamofire' (17 sources)
Compile Swift Module 'Sample' (1 sources)
And my Package.swift is:
import PackageDescription
let package = Package(
name: "Sample",
dependencies: [
.Package(url: "https://github.com/Alamofire/Alamofire.git", majorVersion: 4)
]
)
If you're using an Xcode project, you don't need (and shouldn't use) a Package.swift, just click the plus icon in Swift Packages in Xcode, and add the GitHub URL of the Swift Package, and the library will also be added to your target automatically (follow the GIF below, or click Add icon in image here):
Extra info
Inconsistency problem: You can't maintain both an Xcode project and a Swift.package for the same targets. They do not synchronize, and will become inconsistent, so depending on which tools, you'll get different builds: confusing. You used to be able to create a xcodeproj based on Package.swift using swift package generate-xcodeproj, but this is deprecated. Changes you make to this Xcodeproj didn't get reflected in the original Package.swift).
xcodebuild vs. swift build: Conveniently, if there is no xcodeproj in the same directory as your Package.swift, xcodebuild will auto-generate schemes for you to use, so you don't have to use swift build. For example, run xcodebuild -list to see the list of schemes generated from your Package.swift file, then use one of these schemes. Unconveniently, there isn't a way/ config to make xcodebuild use Package.swift.
Swift Package Manager is a standalone tool which allows managing dependencies and building projects without Xcode. It can generate Xcode projects for you with swift package generate-xcodeproj.
However, at the moment, Swift Package Manager only has support for building projects for macOS and Linux platforms. The only way to build projects for iOS, tvOS and watchOS is using Xcode, which includes the SDKs needed for these platforms.
There are ways to use Swift Packages Manager to manage dependencies for iOS/tvOS/watchOS, but it is not easy and requires manual work. If you are interested, take a look at https://github.com/j-channings/swift-package-manager-ios
Other than that, I'd recommend using Carthage or CocoaPods.
Update for Xcode 11
Swift Package Manager is now integrated into Xcode 11. You can add your package by going to "File" then "Swift Packages" then "Add Package Dependency..." Paste the repository's URL into the field above then click "next". Xcode will walk you through the rest of the steps. You can learn more at this WWDC talk.
Swift Package Manager(SPM)
[iOS Dependency manager]
Consume: [SPM manage dependency]
Produce: If you are developing library/module(modularisation) you should take care of supporting it
Package.swift - manifest file.
It is a hardcoded name.
Should be placed on the same level or higher, in other cases:
target '<target_name>' in package '<package_name>' is outside the package root
Package.swift example
// swift-tools-version:5.3
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "PackageName1",
//Supported platforms
platforms: [
.iOS(.v11)
],
//Product list - executable binary
products: [
.library(
name: "LibraryName1",
targets: ["TargetName1"]),
],
//Additional SPM dependencies declaration(packages) which are used in targets
dependencies: [
//Local package path
.package(name: "PackageName2", path: "../Feature1")
//Local package URL
.package(name: "PackageName2", url: "file:///some_local_path", .branch("master"))
//Remote package URL
.package(name: "PackageName2", url: "https://github.com/user/repo", .branch("master")),
],
targets: [
//Source code location. implicitly `Sources/<target_name>`(Sources/TargetName1) or explicitly `path:`
.target(
name: "TargetName1",
dependencies: [
//using dependencies from package.targets and package.dependencies
//package.targets
"LibraryName2"
//package.dependencies
.product(name: "PM2LibraryName1", package: "PackageName2")
]),
path: "Sources/TargetName1"
]
)
Observations for Swift dependency
[SWIFT_MODULE_NAME, PRODUCT_NAME, EXECUTABLE_NAME] == package.targets.target.name
package.products.library.name is used only for Xcode representation
Library is used[About]
When target has a dependency on another target source code will be included into single library with a kind of explicit multi module from .modulemap[About] and access can be thought
import Module1
import Module2
Swift library converts into single .o format[About] file and .swiftmodule[About]
Swift library exposes .modulemap umbrella.h[About] for exposing module for Objective-C consumer thought #import SomeModule;
- You are able to work with `Package.swift` in Xcode if double click on it
.swiftpm/xcode with .xcworkspace will be generated
- When you work with `Package.swift` you are able to check it, just save this file
- When you work with `Package.swift` you should specify schema and device(platform)
- When you build `Package.swift`
- library and .swiftmodule is located:
<path_to_derived_data>/DerivedData/<folder_name_where_Package.swift_located>-<randomizer>/Build/Products/<platform>
//e.g.
/Users/alex/Library/Developer/Xcode/DerivedData/someFolder-ccivqbbjujxuvdcxtuydyqfeepfx/Build/Products/Debug-iphonesimulator
- .modulemap and umbrella.header is located:
<path_to_derived_data>/DerivedData/<folder_name_where_Package.swift_located>-<randomizer>/Build/Intermediates.noindex/<project_name>.build/<platform>/<target_name>.build/<.modulemap> and plus /Objects-normal/<arch>/<tarhet_name-Swift.h>
- When you build consumer with `Package.swift` results files will be located:
<path_to_derived_data>/DerivedData/<target_name>-<randomizer>/Build/Products/<platform>
- When you work with consumer of `Package.swift` SPM clones it into
<path_to_derived_data>/DerivedData/<target_name>-<randomizer>/SourcePackages/checkouts
package.products.library.targets
You are able to specify several targets. It means that it is possible to use several modules from single executable binary(a kind of umbrella library)
producer:
package.products.library.targets: ["TargetName1", "TargetName2"]
consumer:
1. adds single library
2. several imports
import TargetName1
import TargetName2
package.targets.target.dependencies
If your target has dependency. You will have the same effect - Umbrella library.
1.Add another target from the same package
For example Explicit Dependency[About] is used. Important: use the same names(module and SPM target). If you don't set dependency at package.targets.target.dependencies you get next error
Undefined symbol
2.Add product from another package. All targets from the other package will be exposed
2.1 Local package path
You can not use on consumer side. But it allows you to debut it at least
package <package_name_1> is required using a revision-based requirement and it depends on local package <package_name_2>, which is not supported
2.2 Local package URL
Don't use space( ) in path or you get
The URL file:///hello world/some_local_path is invalid
If you don't specify // swift-tools-version:<version> you get:
package at '<some_path>' is using Swift tools version 3.1.0 which is no longer supported; consider using '// swift-tools-version:5.3' to specify the current tools version
*Do not forget commit your changes and update[About] before testing it

Build Swift 3 in Terminal to create XCode project

It seems Apple changed this line:
swift build -X
This no longer works to create an Xcode project as described here
Doest anyone know the updated way to do this as I cannot find it online
Thanks
Now you should use the Swift Package Manager.
To create a project structure:
swift package init --type executable
or
swift package init --type library
To make a project compatible with Xcode:
swift package generate-xcodeproj

Resources