How to update Swift dependencies in Xcode - swift

After I create a new swift package with dependencies I call swift package generate-xcodeproj. If my dependencies have a new version I call swift package update. The new version of my dependency gets downloaded and built but Xcode now has a bad reference to the old version of the dependency. I can call swift package generate-xcodeproj again but this will remove any custom targets I have setup.
How do I update my swift dependencies and my Xcode project without deleting my custom targets?

I just stumbled upon this question and found that the solution to "How to update swift dependencies in Xcode" has probably changed now that Swift Packages have been around for a few years.
For me, the solutions was to simply go to File -> Swift Packages -> Update to Latest Package Versions.

Many of the problems with packages not updating are because the swift package version rules limit the automatic package updates to the current major version only, i.e v3.3.1 of a package will update to v3.4.0, but will not update automatically to v4.0.1. Therefore using the update options in Xcode does not necessarily get the latest major version of a package.
To resolve, Open the project from the project panel, select the project (not the targets), then select the "Swift Packages" tab. Double click on the package you want to update and change the minimum version to the next major version.

- Update a single dependency using Xcode
This method will save a lot of resources and time. Because it won't force other packages to redownload.
Double click on the package in the tab you mentioned and change the version to anything else. It will then recheck the remote repo. The benefit of doing this is to only update the selected package. (Also, it's better to have the current using version be set in the package.)
- Update ALL dependencies using Xcode
From File -> Swift Packages -> Update to Latest Package Versions
🀨 Single package with GUI (Xcode 12 and above)
Right-click on the package from the left navigation pan and select Update Package
note that you will see Xcode saying update all packages but it will update only the selected one

Instead of trying to preserve your changes to generated project, you can manage dependencies in a separate project, like CocoaPods does.
When starting new project:
create Xcode project for your app MyApp.xcodeproj
save as a workspace MyApp.xcworkspace
create package for your dependencies
mkdir MyDeps && cd MyDeps
swift package init --type library
add dependencies to Package.swift
generate Xcode project for the dependencies package
swift package generate-xcodeproj
add generated project MyDeps.xcodeproj to your workspace MyApp.xcworkspace
add target MyDeps.framework to Linked Frameworks of your app MyApp.xcodeproj
With this setup you can freely update dependencies in Package.swift and regenerate dependent project as needed.

I had an issue with CryptoSwift using Xcode 12.5. My version was 1.3.1 and the issue was fixed in 1.4.0.
I tried :
updating by File -> Swift Packages -> Update to Latest Package Versions.
xcodebuild -resolvePackageDependencies
But both did not work.
I manually went and changed target -> Swift packages -> Version rules
and Xcode automatically installed the newer version and I was out of my error.

If the library is used by a particular target, the library won't be updated to the latest version, at least in my case (Xcode 14.2).
I was using OneSignal which requires to add a target (OneSignalNotificationServiceExtension) which depends on the OneSignal library in the "Frameworks and libraries".
So:
remove the required lib voice from the dependency in the extension,
update the library with right click -> "update package"
re-add to the extension the updated library in the "Frameworks and libraries"
Screenshot of which row to delete to let the library update properly (remember to read later).

Related

Add a local swift package in swift project in Xcode 13

I want to add a swift package in my project. It appear in packages folder (by a drag n drop) but my project doesn't see it with "import myPackage", I've no such module error
1) Go to you project target -> general -> Frameworks, Libraries, and Embedded content
2) add package with + button. ( I only added testFramework on picture below just for example)
Use .package(path:) description in Package.swift.
You can add a dependency to a package located at the given path. And you can edit the package when debugging.
As PackageDescription mentioned this description:
The Swift Package Manager uses the package dependency as-is and does not perform any source control access. Local package dependencies are especially useful during the development of a new package or when working on multiple tightly coupled packages.

Need help adding carthage dependencies to a React Native package I am authoring

I need to build a wrapper around code that has dependencies that are only available as Carthage dependencies. I have successfully added them to a standard tvOS swift project. I am authoring a react-native ui component in swift with the help of the create-react-native-library package by Callstack.
I added a cartfile in the same way as in my swift project, but this time in the iOS folder of the actual react-native package I am authoring. Here, carthage updates and builds the dependencies correctly, but they do not compile. There is no way to add the generated xc-frameworks to the package's Xcode project file (there are no options to add frameworks in the package project, only in the embedded example project).
Does anyone have any suggestion on how to proceed with this?

What's the difference between just open Package.swift & use `swift package generate-xcodeproj` then open the generated xcodeproj file?

I followed the Vapor Website Docs, created hello project.
VaporDocs
under hello project path, inputed swift build in terminal. But when open Package.swift, Xcode start to fetch vapor again.
Why? all dependencies repository is in .build path isn't? Why Xcode begains fetch Vapor over again?
swift package generate-xcodeproj can help generate xcode project, it seems like all dependencies is not package anymore. just groups.
What's the difference between just open Package.swift & use swift package generate-xcodeproj then open the generated xcodeproj file?
open Pacakge.swift with Xcode, fetch Vapor very slow. is there any better way?
like tell the Xcode everything is just in ./build path.
When you work with a project in terminal you use commands like
swift package update
swift build
swift run
swift package generate-xcodeproj
these commands works with hidden .build folder and Xcode project generated by swift package generate-xcodeproj works with that .build folder too. The only disadvantage of xcodeproj that if you change Package.swift or manually(outside of Xcode) add/delete some files from Sources folder then you have to run either just swift package generate-xcodeproj or swift package update && swift package generate-xcodeproj.
When you open project by double clicking on Package.swift it not uses classic xcodeproj anymore, now it is fully dynamic and you could edit Package.swift and files on-the-fly, doesn't matter in Xcode or in Finder, it will track all the changes. Though it sounds cool it works not perfectly and I still prefer classic xcodeproj cause it is not really hard to execute swift package generate-xcodeproj when needed. As far as I know there is no way to say Xcode to use .build folder in dynamic mode, it uses DerivedData folder to store dependencies.

swift package manager link dynamic lib

i used swift on linux, made the Package.swift file including some libraries, but when i built the project with swift build command, the libraries were static linked with the executable file. i tried to generated a Xcode project used the swift package generate-xcodeproj command, built the project in Xcode, then opened the Product directory, i found that the libraries were dynamic linked with the executable file, what did the Xcode doing? and how can i build it as a dynamic link use swift build command, could you help meπŸ˜”πŸ˜”
SwiftPM version 3 was only able to build executables with statically linked libraries. In SwiftPM version 4 (currently in beta, will be released this fall) you can also build with dynamically linked libraries. See SE-0146 for more information.

Could not install package '--'. You are trying to install this package into a project that targets '.NETFramework,Version=v4.5'

I created this package, I need it in a project but couldn't install it, this error appears:
Could not install package 'Mshwf.NiceLinq 1.0.9'. You are trying to
install this package into a project that targets
'.NETFramework,Version=v4.5', but the package does not contain any
assembly references or content files that are compatible with that
framework. For more information, contact the package author
I don't know why this happen, in another project (Console) I changed the framework to 4.6 and other versions and it wasn't a problem, but this only happen in this project (MVC and Web API):
this is the nuspec file:
<?xml version="1.0"?>
<package >
<metadata>
<id>Mshwf.NiceLinq</id>
<version>1.0.9</version>
<title>Nice LINQ</title>
<authors>MShawaf</authors>
<owners>Mshawaf</owners>
<projectUrl>https://github.com/mshwf/NiceLinq</projectUrl>
<iconUrl>https://raw.githubusercontent.com/mshwf/NiceLinq/master/logo.png</iconUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>See it as: WHERE ID IN (1, 2, 3, 7, 9, 22, 30, 101)</description>
<releaseNotes>Minor changes.</releaseNotes>
<copyright>Copyright 2016</copyright>
<tags>LINQ IEnumerable Where Contains Search Filter</tags>
</metadata>
</package>
Your package targets NETFramework,Version=v4.5.2. That means the assembly folder is lib\net452. You are trying to install the package to a higher framework (greater than 4.5) project. Create a project with framework 4.5.2 or rename the package's lib folder from net452 to the version which you want to target like net45 or net46.
You can target multiple framework too from a single package.
Edit your csproj file like this:
<TargetFrameworks>netstandard2.0;net45</TargetFrameworks>
Refer here: https://learn.microsoft.com/en-us/nuget/create-packages/supporting-multiple-target-frameworks
For a complete, up-to-date list of target framework names, refer to https://learn.microsoft.com/en-us/nuget/reference/target-frameworks#supported-frameworks
Go to the folder:
C:\Users\[username]\.nuget\packages\[package name]\1.0.0.4\lib
Rename the folder with the .net version of your project.
Suppose I am using .net framework 4.6.1 my folder name should be net461
You can install earlier version like : Install-Package AutoMapper -Version 7.0.1
The .NET Target Framework version is most likely too old. I have VS 2017 and my default Target Framework for a new .NET Framework project was 4.0. I initially tried to install the RestSharp package but it failed with an almost identical message as in this post 'Could not install package ...' However, when I went to Project | Properties | and Application I was able to change the Target Framework to .NET Framework 4.6.1 and then it installed fine.
In my case, I was trying to install the package into a WPF App (.Net Framework) while my co-workers prepared the package for .Net Core Project.
Try creating a .Net Core Project(or the type of the targeted project type) and install the packages same way:
Solution Explorer>Right click to Solution>Manage Nuget Packages for solution>Search for the needed package>Install
Checking this before other solutions may save time.
In my case, I was trying to insert a package of our own making that we put into our own Nuget feed, so I had access to the project.
This is what I did to clear the issue up. In the project solution window, click on the packages.config. In the properties window, change build action to "Content." After pushing it out to our feed again I was able to install it into the main project.
I had similar issue which i fixed by removing the packages.config(you can edit the file if you don't want to remove) file and then made sure both the package i was using was built using the same .net version as the Project i was using it in(for me the package was built using 4.6 while my console project was targeting earlier version of .net).
Update the Application .NET target framework could solve the problem. In my case, I'm installing EmguCv 4.2 package. I get this same error when trying to build with 4.5.1 .NET target framework. After updated the .NET target framework to 4.6.2, I can install the EmguCv 4.2 Nuget package successfully.
Try installing .net SDK here and then right-click on your solution/project and "restore nuget packages"
I had this problem with a package I had created and hosted on our own private nuget server, but even after trying others suggestions here such as making sure that the package supported the correct .net framework version, it still wouldn't work.
I got someone else to try and they managed to add it with no problem.
Today, messing around in VS's options, I found a fix for this:
Options -> NuGet Package Manager -> General -> Clear All NuGet Cache(s)
How to clear nuget cache in visual studio
Hopefully this helps someone who was as confused as I was :D
In my case I tried to install Itext7 package my project .NET framework was 4.5
I changed my project .NET framework to .NET 4.6.1 by using the following steps then I installed the package successfully :
1- right click the project and click properties
2- Under application tab change the Target framework to the required framework for your package
I could not install OpenTK package in .NETFramwork app but i had success with .NET Core project
Check the supported version from "C:\Users \ [username] \.nuget\packages
[package name]\1.0.0.4\lib".
Go to Project > Right click and select Properties > Application > Target Framework
Change the Target Framework to the supported version
In my case supported version is net462 for the library