How to use frameworks imported with carthage in swift playground - swift

i have a swift project with some frameworks added via carthage. Is it possible to use those frameworks in playground inside project and how to use it, because
import Argo
doesn't work :(

This stopped working at some point. Sigh
What I do now is
Create a macOS > Command Line Tool.
Create a Cartfile with github "ReactiveX/RxSwift" and run carthage update --platform iOS
Go to the command line tool target and add the frameworks from Carthage/Build/iOS in Linked frameworks and Libraries
Add the playground files.
At this point I’m able to run the playground files.
🤷🏻‍♂️
A playground has access to external frameworks if it is part of a workspace that builds a target configured to access those frameworks.
If you want to add a playground to an existing carthage project, you only need to save the project as a workspace (File > Save as Workspace…), build the target, and you are done.
If you just want to distribute a playground with third party frameworks, you need to create a dummy workspace. Here is a step by step example for a playground with the RxSwift framework:
Create a new Xcode project of type Cross-platform > Other > Empty. Name it RxPlayground. This will create this structure RxPlayground/RxPlayground.xcodeproj and open a blank Xcode.
Download RxSwift with Carthage
Create a Cartfile with this line: github "ReactiveX/RxSwift" "swift4.0"
Run Carthage with carthage update --platform iOS.
Add a playground to the project.
Click File > New > Playground…
Choose the iOS > Blank template and name it Rx.playground
Right click the project node and choose “Add Files to RxPlayground”.
Select the Rx.playground and add it.
Create a workspace
Click File > Save as Workspace…
Save as Rx.xcworkspace
Copy the frameworks to the products directory.
Close the project and open the Rx.xcworkspace
Create a Cross-platform > Other > Aggregate. Name it RxAggregate
Create a New Run Script Phase with the following content:
cp -rv "${SRCROOT}/Carthage/Build/iOS/" "${BUILT_PRODUCTS_DIR}/${FRAMEWORKS_FOLDER_PATH}"
At this point, Xcode and the Finder look like this:
Note that Carthage/ and Cartfile.resolved appear when you run Carthage, Without them, your playground will be only a few Ks.
Lastly, build the project (⌘B). Now you can use the framework in your playground:
//: Playground - noun: a place where people can play
import RxSwift
_ = Observable<Void>.empty()
.subscribe(onCompleted: {
print("Completed")
})
Sometimes the first time you build (⌘B) Xcode doesn’t notice the new framework (sigh). What I do is click on the target and back to a source file, or re-open the project. I don’t have an explanation why this happens.

In order for the framework to work within a playground, the project that produces the framework must be included in the workspace for your project. So to make this work you need to follow these steps:
If your project is not inside a workspace, create a workspace for your project by choosing File > Save As Workspace in Xcode.
Drag the .xcodeproj file from the Carthage/Checkouts folder into your workspace.
Run the build action on your framework target.

I solved it by copying the built frameworks to the Built Products Directory, where Playgrounds in the workspace also searches for frameworks.
Note: you also need to run lipo and strip away unused architectures from the FAT binaries.
See more here:
https://github.com/richardnees/CarthagePlaygrounds

Based #Jano's great answer (thanks for that), I created a fully functional playground to interact with Carthage frameworks:
https://github.com/backslash-f/carthage-playground
I included the Charts framework as an example:
I tested with latest Xcode.
"Works on my machine." 👍🏻

Found how to do this without converting to workspace and adding .xcodeproj file from the Carthage/Checkouts folder into your project.
This works if you have some framework target that contains all sources you want to use in playground. This framework must be in "Embedded frameworks" of your main target.
Add Carthage .framework to your project from Carthage/Build/IOS
Drag and drop it from your project tree into "Embedded
frameworks" for your main target
Add Carthage .framework to your
framework target as Optional(In target Membership)
Add import
"Your framework target" into playground
That worked for me

Related

Xcode 13.1 bug editing dependency as local

Just upgraded to Xcode 13.1 and opened a SPM project by clicking the Package.swift file that has some dependencies.
In the former versions I could simply edit one of these dependencies by dragging a local clone of the package into the Project Navigator.
Xcode was then going to ignore the checked out dependency and let me edit the local one.
Apple documents this here.
When trying this with Xcode 13.1, Xcode just copies the folder into the project folder, like it does with normal files/folders that are not SPM projects.
Did I find a bug or missed I something?
Thanks!
Update: I found out how to work around by creating a .xcodeproj file with swift package generate-xcodeproj. There I could drag in the package as always. This works but is not the way to go, as the xcodeproj generation is deprecated by apple.
It looks like there is now an Add Local... button at the bottom of the add package wizard. Selecting the package from your local filesystem yields the same result as the "drag from finder" instructions you've linked.
The wizard is accessible from:
File > Add Packages, or
Project.xcodeproj > Package Dependencies > +.
I was having this problem, too, but after seeing the first comment on the other answer, I tried this:
I used File->Add Packages->Add Local…->navigate to local clone of dependency->Copy Package, which indeed seems to do nothing, but it puts a .package dependency line onto the clipboard.
I then edited my main project’s Package.swift to replace the existing Github dependency with the contents of the clipboard. Now I can edit the dependency’s files directly in the “Package Dependencies” portion of the project contents pane (which I wish you could do for a regular Xcode project too).
This solution kinda sucks, because it forces me to modify Package.swift, which means everyone on the team has to set things up the same way. Fortunately, I'm the only one working on this project, but future me is sure to be bitten by this.

How to take framework from one project to another Swift

I want to ask how to take a framework from one project and implemented it in my project. So I am interested in using AntMedia, and I look it up in their website about how to implement it on my project, and all i get is a sample project. Right now I am having a problem at how to basically take the framework that is needed from this sample project to my project
screenshot of the sample’s project folders
In your project root, copy the Podfile from the sample and update it with your app target. I’m not sure I found the right pod, so here’s a guess of what might be in it.
target 'MyApp' do
pod 'AnyMediaToolbox', '~> 1.1'
end
Replace MyApp with your app target.
Then run a pod install inside your terminal
If you don’t have cocoapods installed, see: https://cocoapods.org
This will force you to use a workspace. See cocoapods documentation for more details
the simplest way would be to go to Project Settings, select target, choose General and under Frameworks, Libraries, and Embedded Content click on plus and in drop down menu where you will see Add Other... select Add Files and select the framework that you want to add

How to import Swift Package in Playground?

Can i import Swift Package to my Playground that is in my workspace? I've already read the answer, but i think it's outdated, because now we can use Swift Packages in iOS projects.
In Xcode 12, you can use Swift Packages with Xcode Playgrounds.
Open Xcode
Create a new Workspace (File > New > Workspace...)
Add the desired Swift package via File > Add Files to "Workspace Name"... selecting the package directory.
Create a new Playground via File > New > Playground.... Ensure that you select the Workspace we just created as the "Add to" and "Group" option during Playground creation (in the assistant where you select the Playground's location on disk).
You can now import the Package target into your Playground and starting exploring the Package's functionality.
You can find additional details and a sample project in this WWDC20 session: Explore Packages and Projects with Xcode Playgrounds
Ensure to check the new "Build active scheme" checkbox in the inspector for your existing Playgrounds.
Check out Arena, it allows you to create playgrounds with SPM dependencies embedded ready for use.
All it takes is
brew install finestructure/tap/arena
arena alamofire/alamofire
Update: The Swift Package Index has since gained Arena integration and you can create a playground for a package by clicking the "Try in a Playground" button on a package page.
Still now, Playground does not support the Swift Package Manager.
When we try to add the Swift PM from Xcode using
File > Swift Packages > Add Package Dependency
the option is disabled.
An option is to use nef - it is an open-source project an lets you import third-party libraries in your Playgrounds. For now, it is compatible with Cocoapods but nef are working on open it for other dependency managers like Carthage and SPM.
About SPM is not an easy task, and it will be a bit different as Apple has prepared Xcode11 but I have good feelings and I think it could be ready for Sept-Oct.
I couldn't get #wolfrevo's answer working. However, I have a solution inspired by his:
Open Xcode
Create a New Project (File > New > Project)
Choose Muliplatform > App > Next
Project Name: Whatever > Next
Create
Now add the dependency (File > Swift Packages > Add Package Dependency)
Paste package git url: https://github.com/maxhumber/Carlo.git > Next
Select rules, or skip > Next
Finish
Create a new Playground file in this project by short-cutting Cmd+N
Search Playground > Next
Create

Importing a Swift project from Linux to Mac

I used CLion and its Swift plugin to start work on a little Swift project. I uploaded the current status to github and now want to work on that on my mac (I followed this tutorial: https://blog.jetbrains.com/clion/2015/12/swift-plugin-for-clion/). How do i import that project on mac now, though? I have AppCode and XCode installed but cant really get it to work. I used AppCode's "import from github" feature but that leaves me with a project that does show the CMakeLists.txt and Package.swift, but not my Sources/ folder at all. And in XCode i can only start new projects for applications, not a static command line lib without any GUI like this project is supposed to be.
So: How do i import them on mac in AppCode (preferred!) or XCode?
Thanks!
EDIT: Oh an when trying to simply checkout my github repo in AppCode it states "no project file found".
It sounds like you've created a SwiftPM package, since you have a Sources directory and a Package.swift file. If that is correct, you can create an Xcode project with the following command:
swift package generate-xcodeproj
AppCode needs xcodeproj to open the project, without it you cannot open it in AppCode. But you can use the CLion on Mac.

Sparkle 10.9 Linking Error

Having an issue where when trying to add Sparkle to my project with Xcode 5 running 10.9.
I have followed the tutorial to a T, but when I get to the point where I need to copy the framework and set its copy location that is where there seems to be no way to set the copy destination in the copy bundle resources section and it just puts the framework int the /Resources folder.
I have ready through this tread thoroughly and have tried all the suggestions here.
http://www.lists.apple.com/archives/xcode-users/2012/Aug/msg00249.html
I recompiled Sparkle from the latest source and set the Installation Directory to #executable_path/../Frameworks from #loader_path/../Frameworks I get the following error.
dyld: Library not loaded: #executable_path/../Frameworks/Sparkle.framework/
when I revert and try to re add the framework I get this error.
dyld: Library not loaded: #loader_path/../Frameworks/Sparkle.framework/
If I remove the framework from the link framework from binaries altogether and just focus on the Copy Bundle Resources then I get this error.
Unknown class 'SUUpdater', using 'NSObject' instead.
The tutorial is written for Xcode 4 I know. One step says to use Copy File Location but that seems to be gone in Xcode 5 any help is appreciated.
UPDATE
I found a work around but its very messy.
Add the framework to the "Add files to build" build step.
Unlink the sparkle framework.
Open Derived Data folder and then open the App Build, then browse to Resources. Move the sparkle.framework folder to the root of the app and place the framework into a folder named Framework in the root level of the app.
Remove the framework from the "Add files to build" step.
Re-Link the framework.
Rebuild the App and voila it works.
Basically your adding the framework in a Framework folder manually and then linking it up at the end. What makes it messy is that the build changes occasionally in the Derived Data folder.
I have the same problem. And I think I have solved it.
It happens because the Sparkle.framework isn't copied to Frameworks folder(where it should be), it is copied to Resources folder when you build the application. To solved it:
Download the source of Sparkle from GIT (https://github.com/sparkle-project/Sparkle) and open the project in Xcode 5;
Go to Build Settings of every targets, then find Base SDK, change the value to macosx.
Go to Sparkle.framework's Build Settings. Find Installation Directory, Change its value from #loader_path/../Frameworks to #loader_path/../Resources under Sparkle.framework's Build Settings.
Build.
Copy Sparkle.framework under Products group in Project Navigator to your project.
Say YES and take a self highfive.
I was suffering from the same problem, then I found this:
http://www.lantean.co/sparkle-in-sandbox-mode/
The key is to add the Sparkle.framework to the Frameworks folder using the "Copy Files" section from "Build Phases".