Swift has five levels of access control. Use the following rules of thumb when creating your own frameworks:
public class CalendarPickerViewController: UIViewController {
Now CalendarPickerViewController is visible to any app file that imports the CalendarControl framework. Next, add the public keyword to:
CalendarPickerViewController.init(baseDate:selectedDateChanged:)
CalendarPickerViewController.init(coder:)
CalendarPickerViewController.viewDidLoad()
CalendarPickerViewController.viewWillTransition(to:with:)
CalendarPickerViewController.collectionView(_:numberOfItemsInSection:)
CalendarPickerViewController.collectionView(_:cellForItemAt:)
CalendarPickerViewController.collectionView(_:didSelectItemAt:)
CalendarPickerViewController.collectionView(_:layout:sizeForItemAt:)
Note: You might wonder why you have to declare init as public. Apple explains this and other finer points of access control in their Access Control Documentation.
Build and run. Now you get your CustomCalendarPicker. Congratulations! You now have a working stand-alone framework and an app that uses it!
import CustomClenderPicker
You might have heard about XCFramework during WWDC 2019. Yes, you’re right: This is the name of the binary framework you can generate with Xcode. Before 2019, you only had one opportunity to make your own binary framework: Universal Static Library, also known as Fat Framework. To support multiple architectures, like a simulator and devices, you had to combine them under one library in the fat framework. However, after this article, your frameworks don’t have to be fat anymore. Archiving Your Framework For this section, you’ll work with your old friend, Terminal. Woohoo! Open your terminal and navigate to the framework folder with the following command. Alternatively, you could drag your project folder to your terminal after the cd command:
cd /Users/xyg/Desktop/CustomClenderPicker
Start with iOS. Enter the following command into the terminal:
xcodebuild archive \
-scheme CustomClenderPicker \
-configuration Release \
-destination 'generic/platform=iOS' \
-archivePath './build/CustomClenderPicker.framework-iphoneos.xcarchive' \
SKIP_INSTALL=NO \
BUILD_LIBRARIES_FOR_DISTRIBUTION=YES
When it's done, it's like this:
Enter the following command into the terminal:
xcodebuild archive \
-scheme CustomClenderPicker \
-configuration Release \
-destination 'generic/platform=iOS Simulator' \
-archivePath './build/CustomClenderPicker.framework-iphonesimulator.xcarchive' \
SKIP_INSTALL=NO \
BUILD_LIBRARIES_FOR_DISTRIBUTION=YES
xcodebuild archive \
-scheme CustomClenderPicker \
-configuration Release \
-destination 'platform=macOS,arch=x86_64,variant=Mac Catalyst' \
-archivePath './build/CustomClenderPicker.framework-catalyst.xcarchive' \
SKIP_INSTALL=NO \
BUILD_LIBRARIES_FOR_DISTRIBUTION=YES
Now, make the binary framework, XCFramework. Add the following command to the terminal:
xcodebuild -create-xcframework \
-framework './build/CustomClenderPicker.framework-iphonesimulator.xcarchive/Products/Library/Frameworks/CustomClenderPicker.framework' \
-framework './build/CustomClenderPicker.framework-iphoneos.xcarchive/Products/Library/Frameworks/CustomClenderPicker.framework' \
-framework './build/CustomClenderPicker.framework-catalyst.xcarchive/Products/Library/Frameworks/CustomClenderPicker.framework' \
-output './build/CustomClenderPicker.xcframework'
If this error occurs:
No 'swiftinterface' files found within '/Users/xyg/Desktop/CustomClenderPicker/build/CustomClenderPicker.framework-iphonesimulator.xcarchive/Products/Library/Frameworks/CustomClenderPicker.framework/Modules/CustomClenderPicker.swiftmodule'.
you need to set the build library for distribution of the framework project 在 Build Settings ~> Build Library for Distribution 设为YES
import CustomClenderPicker
Distributing CustomClenderPicker.XCFramework as a cocoapods CocoaPods is a bit more strict and requires you to keep the built binaries with the Podspec
pod spec create CustomClenderPicker
At the root of the project, create the following CustomClenderPicker.podspec. In this spec we're not publishing actual code, so we don't have to specify which files are applicable. Instead, we must use the vendored_frameworks property to indicate which XCFramework(s) need to be published.
Pod::Spec.new do |spec|
spec.name = "CustomClenderPicker"
spec.version = "1.0.2"
spec.summary = "CustomClenderPicker Library for iOS apps"
spec.description = "CustomClenderPicker Library for iOS apps. time picker to select time"
spec.homepage = "https://gitee.com/xiyg/projects"
spec.documentation_url = "https://gitee.com/xiyg/custom-clender-picker"
spec.license = { :type => "MIT" }
spec.author = { "xiyg" => "xyg15243228311@163.com" }
spec.source = { :git => 'https://gitee.com/xiyg/custom-clender-picker.git', :tag => "#{spec.version}" }
spec.swift_version = "5.3"
# Supported deployment targets
spec.ios.deployment_target = "13.0"
# Published binaries
spec.vendored_frameworks = "Sources/CustomClenderPicker.xcframework"
# spec.dependency 'PromisesSwift', '1.2.8' # Third Party Dependency
end
git add .
git commit -m ""
git push
git tag "0.0.1"
git push --tags
pod spec lint CustomClenderPicker.podspec --allow-warnings
pod trunk register xyg15243228311@163.com 'xiyg' --description='CustomClenderPicker is a tool'
pod trunk me
pod trunk push CustomClenderPicker.podspec
pod search CustomClenderPicker
if occur error like this:
~ pod search CustomClenderPicker
[!] Unable to find a pod with name, author, summary, or description matching `CustomClenderPicker`
rm ~/Library/Caches/CocoaPods/search_index.json
then:
pod setup
pod search CustomClenderPicker
Distributing CustomClenderPicker.XCFramework as a Swift Package At WWDC 2020, Apple announced that you can easily distribute your XCFramework within Swift Packages. Isn’t that awesome? Note: If you’re not familiar with Swift Packages or Swift Package Manager you can find out more by reading Swift Package Manager for iOS. You should have a Swift Package for distributing your XCFramework. You’ll create one in the next section. Then you can share your fancy framework by publishing it on GitHub. Preparing the Swift Package Note: If you’re not familiar with Swift Packages or Swift Package Manager you can find out more by reading Swift Package Manager for iOS.
import PackageDescription
let package = Package(
name: "CustomClenderPicker",
platforms: [
.macOS(.v10_15), .iOS(.v14), .tvOS(.v14)
],
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
name: "CustomClenderPicker",
targets: ["CustomClenderPicker"]),
],
targets: [
.binaryTarget(
name: "CustomClenderPicker",
path: "./Sources/CustomClenderPicker.xcframework")
]
)
.
├── LICENSE
├── Package.swift
├── README.en.md
├── README.md
└── Sources
└── CustomClenderPicker.xcframework
Carthage 是一款 iOS 项目依赖管理工具,与 Cocoapods 有着相似的功能,可以帮助你方便的管理第三方依赖,它会把三方依赖编译成 framework ,以 framework 的形式将三方依赖加入到项目中进行使用和管理 如何使用carthage
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。