美文网首页
Swift framework制作

Swift framework制作

作者: 搞好关系 | 来源:发表于2020-11-13 23:32 被阅读0次

    swift支持OC以及重命名

    @objc(PaymentManager)
    open class PayManager : NSObject {
        private override init(){}
        @objc(manager)
        public static let manager: PayManager = PayManager()
        @objc
        public func canOpen(_ url: URL) -> Bool{
            return true
        }
    }
    

    对于需要支持OC的需要使用@objc标记,同时可以重新定义出在OC中的表现形式,上面的会被编译成如下

    SWIFT_CLASS_NAMED("PayManager")
    @interface PaymentManager : NSObject
    - (nonnull instancetype)init SWIFT_UNAVAILABLE;
    + (nonnull instancetype)new SWIFT_UNAVAILABLE_MSG("-init is unavailable");
    SWIFT_CLASS_PROPERTY(@property (nonatomic, class, readonly, strong) PaymentManager * _Nonnull manager;)
    + (PaymentManager * _Nonnull)manager SWIFT_WARN_UNUSED_RESULT;
    - (BOOL)canOpen:(NSURL * _Nonnull)url SWIFT_WARN_UNUSED_RESULT;
    @end
    

    而在OC中调用即是另一形式,可以充分展现出swift和OC语法的差异化表现形式

     [PaymentManager.manager canOpen:[NSURL URLWithString:@""]];
    

    快速脚本辅助打包

    选择我们的framework对应的target,然后新建target


    aggregate

    新建对应脚本


    image.png
    
    # Merge Script
     
    # 1
    # Set bash script to exit immediately if any commands fail.
    set -e
     
    # 2
    # 这里替换成自己的 Framework 名称
    FRAMEWORK_NAME="AtomeSDK"
     
    # 3
    # If remnants from a previous build exist, delete them.
    if [ -d "${SRCROOT}/build" ]; then
    rm -rf "${SRCROOT}/build"
    fi
     
    # 4
    # Build the framework for device and for simulator (using
    # all needed architectures).
    xcodebuild -target "${FRAMEWORK_NAME}" -configuration Release -arch arm64 only_active_arch=no defines_module=yes BITCODE_GENERATION_MODE=bitcode OTHER_CFLAGS="-fembed-bitcode" -sdk "iphoneos"
    xcodebuild -target "${FRAMEWORK_NAME}" -configuration Release -arch x86_64  only_active_arch=no defines_module=yes BITCODE_GENERATION_MODE=bitcode OTHER_CFLAGS="-fembed-bitcode-marker" -sdk "iphonesimulator"
     
    # 5
    # Remove .framework file if exists on Desktop from previous run.
    if [ -d "${HOME}/Desktop/${FRAMEWORK_NAME}.framework" ]; then
    rm -rf "${HOME}/Desktop/${FRAMEWORK_NAME}.framework"
    fi
     
    # 6
    # Copy the device version of framework to Desktop.
    cp -r "${SRCROOT}/build/Release-iphoneos/${FRAMEWORK_NAME}.framework" "${HOME}/Desktop/${FRAMEWORK_NAME}.framework"
     
    # 7
    # Replace the framework executable within the framework with
    # a new version created by merging the device and simulator
    # frameworks' executables with lipo.
    lipo -create -output "${HOME}/Desktop/${FRAMEWORK_NAME}.framework/${FRAMEWORK_NAME}" "${SRCROOT}/build/Release-iphoneos/${FRAMEWORK_NAME}.framework/${FRAMEWORK_NAME}" "${SRCROOT}/build/Release-iphonesimulator/${FRAMEWORK_NAME}.framework/${FRAMEWORK_NAME}"
     
    # 8
    # Copy the Swift module mappings for the simulator into the
    # framework.  The device mappings already exist from step 6.
    cp -r "${SRCROOT}/build/Release-iphonesimulator/${FRAMEWORK_NAME}.framework/Modules/${FRAMEWORK_NAME}.swiftmodule/" "${HOME}/Desktop/${FRAMEWORK_NAME}.framework/Modules/${FRAMEWORK_NAME}.swiftmodule"
     
    # 9
    # Delete the most recent build.
    if [ -d "${SRCROOT}/build" ]; then
    rm -rf "${SRCROOT}/build"
    
    fi
    

    注意事项

    1 开启向后兼容

    BUILD_LIBRARIES_FOR_DISTRIBUTION=YES, 代表构建向后兼容的 framework,生成的framework会包含swiftmodule


    image.png

    2 关于使用static/dynamic两种framework

    2.1 使用 Dynamic Library

    Dynamic Library
    此时生成的framework进行分发使用时,宿主项目需要进行特殊设置(为了iOS扩展插件和watch才开放的内嵌形式)
    image.png
    需要选择后两项,否则会报错:image not found
    dyld: Library not loaded: @rpath/AtomeSDK.framework/AtomeSDK
      Referenced from: /Users/yue/Library/Developer/CoreSimulator/Devices/1E57E3BD-7D03-463C-8C97-EEDB0BAD3681/data/Containers/Bundle/Application/515B7E52-64DE-4322-AE7C-7FE633DDA837/GCD_Operation.app/GCD_Operation
      Reason: image not found
    dyld: launch, loading dependent libraries
    DYLD_SHARED_CACHE_DIR=/Users/yue/Library/Developer/CoreSimulator/Caches/dyld/19H2/com.apple.CoreSimulator.SimRuntime.iOS-14-0.18A372
    DYLD_ROOT_PATH=/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot
    DYLD_LIBRARY_PATH=/Users/yue/Library/Developer/Xcode/DerivedData/GCD_Operation-entiamsptuetnmbduoegttqlthos/Build/Products/Debug-iphonesimulator:/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/usr/lib/system/introspection
    DYLD_INSERT_LIBRARIES=/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/usr/lib/libBacktraceRecording.dylib:/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulat
    (lldb) 
    

    2.2使用 Static Library形式的framework分发

    ton

    此时生成的framework 拖进项目直接使用即可

    相关文章

      网友评论

          本文标题:Swift framework制作

          本文链接:https://www.haomeiwen.com/subject/uvxibktx.html