How to distribute Swift Library without exposing the source code?
Swift is beta now, and even for 1.0 Apple has been pretty clear they're after a restricted feature set -- better to do a small number of things well than to try to do everything.
So for now, there's no way to distribute binary static libraries. Presumably that'll change sometime after Swift 1.0. For now, you can:
- Distribute source
- Ship a binary framework (instead of a library) if you're okay with the ABI being fragile
- Use ObjC for library code
You can always combine approaches, too: e.g., implement the critical (secret) details of your library in ObjC, and ship Swift source that wraps it in a nice Swift API.
Obfuscating code written in a language that's very much subject to change sounds like a recipe for a maintenance nightmare.
Swift packages: sharing internal code between libraries but not library users
I solved this by doing the first method I listed in the question (4 modules):
- Core: Code shared by library A and B, which should be importable by the user.
- Internal: Code that library users should not be able to import. Depends on Core. Also depends on other libraries which shouldn't be exposed to the library user.
- Library A (e.g. Human): One library the user may choose to use.
- Library B (e.g. Dog): Another library the user may choose to use.
I then in Library A and Library B, I have a file called Exports.swift
which has @_exported import Core
.
How to access pure Swift Static Library from pure Swift project?
I've passed your steps and all works, at least at Xcode 11.3.1 / iOS 13.3. The only thing... it seems... see comment
import StaticLib // << looks like you've forgot this one !!!
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
StaticLib.test("How do you do?") // error
return true
}
}
Update: Here are my steps -
- Click project in Xcode Project Navigator
- Click + to add new target
- Select Frameworks & Libraries > Static Library > Next
- Enter name
StaticLib
(in Swift) > Finish - Open StaticLib.swift and enter
public class StaticLib {
public class func printme() {
print("I'm swfit static lib!")
}
}
- Select
StaticLib
schema, Build > OK - Select project in Project Navigator > click main application target > select General
- In section Framework, Libraries, and Embedded Content click + and select
libStaticLib.a
> Add - Select main app schema > Build > OK
- Select AppDelegate.swift > add
import StaticLib
- Add anywhere in code
StaticLib.printme()
- Build > OK > Run ... see output
Update2: for external Lib project
+1 It is needed to copy StaticLib.swiftmodule
(it is created at the same place as libStaticLib.a) into target project folder (I placed it at the level of .xcodeproj
file)
+2 In main application target Build Settings
set SWIFT_INCLUDE_PATHS = ${SRCROOT}
+3 Clean > Build
Note: the indicator that module is loaded is autocompletion for import - it should show StaticLib
Related Topics
Symbolicate Crash in iOS8 with Xcode 6 .1
Where Is the Official Documentation for Cvopenglestexture Method Types
Removing Object from Array in Swift 3
Info.Plist Utility Error: "Info.Plist Couldn't Be Opened Because There Is No Such File"
How to Debug an Issue with a Release Mode Build in iOS
How to Get the MAC Os X Firewall to Permanently Allow My iOS App
How Do Set a Width and Height of an Image in Swift
How to Implement Localization in Swift Ui
Less Blur with 'Visual Effect View with Blur'
What Is Container View in iOS 5 Sdk
iOS Enterprise Provisioning Profile Expiration
Add Uiview Banner Above Status Bar iOS 7
Pulling Data from a Cmsamplebuffer in Order to Create a Deep Copy
Delete Cell from Uicollectionview Without Reloading from Top
How to Resize Uitableviewcell to Fit Its Content
Uinavigationcontroller Interactivepopgesturerecognizer Working Abnormal in iOS7
Uicontroleventeditingchanged Doesn't Get Fired When Using Settext of Uitextfield