How to Distribute Swift Library Without Exposing the Source Code

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 -

  1. Click project in Xcode Project Navigator
  2. Click + to add new target
  3. Select Frameworks & Libraries > Static Library > Next
  4. Enter name StaticLib (in Swift) > Finish
  5. Open StaticLib.swift and enter
    public class StaticLib {
public class func printme() {
print("I'm swfit static lib!")
}
}

  1. Select StaticLib schema, Build > OK
  2. Select project in Project Navigator > click main application target > select General
  3. In section Framework, Libraries, and Embedded Content click + and select libStaticLib.a > Add
  4. Select main app schema > Build > OK
  5. Select AppDelegate.swift > add import StaticLib
  6. Add anywhere in code
    StaticLib.printme()

  1. 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



Leave a reply



Submit