Embedding a Swift Framework into another Framework Xcode - ios

I have basically two frameworks running on Xcode. "ResearchKit" and "AppMethods". While all works fine, the "AppMethods" framework utilizes code within "ResearchKit". In fact it is there to abstract out more methods into framework. A superclass of sorts.
When using them in code, I have to import both frameworks
import ResearchKit
import AppMethods
Is there a way to embed ResearchKit within AppMethods such that I only need to import AppMethods. Without ResearchKit, AppMethods would not exist.

It really depends on the way you implement and use these frameworks within the app.
Generally speaking, iOS doesn't support nested frameworks - please read the follwoing.
But imports can be done within each framework - i.e you can import ResearchKit directly from AppMethods and just "tell" Xcode that the code will be there on compile time, you will have to link the frameworks within the App level. Another useful guide I found
It is quite simple to achieve with CoacoaPods and SPM

Your "stack" is this:
App
|
AppMethods (AM for short)
|
ResearchKit (RK for short).
Within your classes in the app, where you dont explicitly call a RK API (class or method) you dont have to import RK. Otherwise, you have to import RK.
Please note you can embed one library inside another (in this case RK inside AM), and maybe make sure that nobody outside AM knows anything about RK. But once you start explicitly calling RK api within your app, there is no other way than importing RK explicitly along AM.

Related

How to include libsignal-protocol-c in my Swift iOS app?

I would like to write something like import SignalProtocol at the top of one of my Swift files and then be able to call the C functions from libsignal-protocol-c.
libsignal-protocol-c's README says: "When integrating into actual applications, you should not need anything beyond CMake. Alternatively, you may integrate the code using a build system of your choice."
I think I'd like to use the Swift Package Manager to integrate libsignal-protocol-c into my Swift iOS app. Is there a way to automatically generate a manifest file, ie, Package.swift, and a module map, ie, a module.modulemap file, from libsignal-protocol-c's CMake files? Or if I need to create these files manually, what should they include? Otherwise, how would I do this with CMake?
How to import and use libsignal-protocol-c in an existing .xcodeProj? is a similar question but for Objective-C projects.
I'm not sure its an answer but I'm going to start using https://github.com/christophhagen/LibSignalProtocolSwift. Seems like a good start.

How to import 3rd party framework privately for your Swift framework

If I'm building a framework A and import Reachability, maybe via Carthage of Swift PM, although the app using framework A would never be able to use A.Reachability, import Reachability still appears in interface of final built of A.
How to avoid this?
Swift doesn't support internal/private imports at the moment. All of your imports are forwarded to anyone using your framework. If you look at Foundation for example it pulls in a lot of other frameworks such as GCD and Darwin.
If you need to hide it then you are currently limited to copying the code into your framework and making sure it is all internal/private so people importing your framework don't see it.

Add custom framework to Standard Library in Xcode

I've looked everywhere and no article gives me exactly the solution I'm looking for.
Is there a way to "install" a custom framework into Xcode?
For example, let's say I've created a framework called 'MyAwesomeFramework' which is really reusable (for example contains a lot of useful UIView extensions). Now what I want to do is be able to just create any new project and type import MyAwesomeFramework to use it, instead of having to add the respective Xcode Project to the project I want to use it in.
Apple Documentation
There are a few different options. You can manually add them via drag and drop or use one of many framework managers such as Carthage or Cocoapods.
Whatever method you choose has a few steps and can be a bit daunting initially so I’d recommend following a tutorial of some sort(I used YouTube). If done correctly you will be able to just import like you would do any other library.

Swift: How do you use an Objective-C function from a linked library?

Background
I'm new to iOS development and playing with Swift for the sake of learning. As a small challenge, I'm trying to get the SSID of network to which a device is currently connected.
This is well-covered ground on Stack using Objective-C: iPhone get SSID without private library, for example ... but the Objective-C / Swift dance is causing me some conceptual challenges.
Specifically, the suggested solution (discussed in the above-linked post) is to call the function CNCopyCurrentNetworkInfo() -- but, according to Apple's documentation, this function is not available in Swift.
So far
I've included (I think correctly) SystemConfiguration.framework by adding it to the Linked Frameworks and Libraries of the project. I've also brought it into the View Controller using import SystemConfiguration.
Questions
Do I need to also use import SystemConfiguration, or is that already done due to including the framework with my project?
Is there another / better way to include the SystemConfiguration library?
The big one: How, once the required library is imported, do you call the Objective-C function from the library?
Thank you!
I know this is not directly answering your question, but it's an answer to your problem.
You can do everything in swift in your case.
After importing the library,
import SystemConfiguration.CaptiveNetwork
You can immediately call:
CNCopySupportedInterfaces()
and it will work.
Confirmed in Xcode 6.3.2.
Apple's interoperability book (from Understanding the Swift Import Process):
Any Objective-C framework (or C library) that’s accessible as a module
can be imported directly into Swift. This includes all of the
Objective-C system frameworks—such as Foundation, UIKit, and
SpriteKit—as well as common C libraries supplied with the system.
Regarding your specific questions:
The opposite is the case: you do not need to manually include Apple's frameworks. Xcode will automatically make them available to you given a valid import statement (such as import SystemConfiguration in your case), even – or rather: especially – in a playground!
ditto...
Given the above import statement, SystemConfiguration is indeed imported since you can call its functions (e.g. SCCopyLastError() // --> __NSCFError) and access its constants (e.g. kCFErrorDomainSystemConfiguration // --> com.apple.SystemConfiguration). Unfortunately, CaptiveNetwork does not seem to be imported with it (e.g. CNCopySupportedInterfaces() // --> Use of unresolved identifier).
You should be able, however, to use this framework on the Objective C side and simply call your own wrapper functions from Swift. You just need to remember to include them among the imports listed in your bridging header (see Swift and Objective-C in the Same Project for more about bridging headers).

How to create and use an iOS Framework for sharing code with Extensions

How does the minimal setup and usage of a custom iOS framework look like? I starting to look into this in order to share code with a Today Extension.
Here's what I did so far
Added a new Target choosing the Cocoa Touch Framework template and called it TesterKit
Inside the framework I created a new class TestClass.swift
Inside TestClass.swift I created a simple class method class fund tester() printing a string for testing
Looking at my app I can see that TesterKit has been added as Embedded Binaries and Linked Frameworks and Libraries, however it is red
In order to use this new Framework in the app, I added import TesterKit to the top of my AppDelegate
Then I tried to call the class method from the Framework using TestClass.tester(). But instead of showing the log message I get a …
"Use of unresolved identifier"
→ What am I doing wrong? Any wrong assumptions here?
Note: I already watched the WWDC sessions 416 "Building Modern Frameworks" and found the Framework Programmign Guide. If there are any example projects showing how to use such new custom iOS Frameworks, ideally using Swift + integrating this with Today Extensions, that might be helpful, too.
It seems like your import is not failing which means the framework is actually set up correctly...try making the function public.
public func tester() { print("tester()" }

Resources