Flutter创建插件的命令
创建插件的命令
flutter create --org com.plugin.demo --template=plugin --platforms=android,ios -i swift -a java plugin_demo
OC引用swift 文件
.m文件中添加以下代码
//以下代码实现OC调用Swift
#if __has_include(<plugin_demo/plugin_demo-Swift.h>)
#import <plugin_demo/plugin_demo-Swift.h>
#else
// Support project import fallback if the generated compatibility header
// is not copied when this plugin is created as a library.
// https://forums.swift.org/t/swift-static-libraries-dont-copy-generated-objective-c-header/19816
#import "plugin_demo-Swift.h"
#endif
IOS插件plugin_demo.podspec配置
当对.podspec文件进行修改后,需要对项目执行pod install才有效
#
# To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html.
# Run `pod lib lint plugin_demo.podspec` to validate before publishing.
#
Pod::Spec.new do |s|
s.name = 'plugin_demo'
s.version = '0.0.1'
s.summary = 'A new Flutter plugin project.'
s.description = <<-DESC
A new Flutter plugin project.
DESC
s.homepage = 'http://example.com'
s.license = { :file => '../LICENSE' }
s.author = { 'Your Company' => 'email@example.com' }
s.source = { :path => '.' }
#s.source_files = 'Classes/**/*'
#指定包含的源文件类型,但是插件中用到的.metallib,.tflite,.mbd文件放置在pod所在的文件夹下不能正常
#引用,需要放到主项目中
s.source_files = ['Classes/**/*.{swift,h,m,mm,framework,tflite,bmd,metallib}']
s.dependency 'Flutter'
s.platform = :ios, '12.0'
# 指定桥接头文件路径,实现swift文件能访问oc文件
s.user_target_xcconfig = {
'SWIFT_OBJC_BRIDGING_HEADER' => '${PODS_ROOT}/../.symlinks/plugins/plugin_demo/ios/Classes/plugin_demo-Bridging-Header.h'
}
#指定用到的其他.framework
s.vendored_frameworks = "*.framework"
#指定用到的系统framework
s.frameworks = "AppTrackingTransparency"
#PODS_TARGET_SRCROOT =
# Flutter.framework does not contain a i386 slice.
s.pod_target_xcconfig = { 'DEFINES_MODULE' => 'YES', 'EXCLUDED_ARCHS[sdk=iphonesimulator*]' => 'i386' }
s.swift_version = '5.0'
# If your plugin requires a privacy manifest, for example if it uses any
# required reason APIs, update the PrivacyInfo.xcprivacy file to describe your
# plugin's privacy impact, and then uncomment this line. For more information,
# see https://developer.apple.com/documentation/bundleresources/privacy_manifest_files
# s.resource_bundles = {'plugin_demo_privacy' => ['Resources/PrivacyInfo.xcprivacy']}
end
官方文档
https://guides.cocoapods.org/syntax/podspec.html
以下配置来源于网上
s.ios.source_files = 'PoCSDK/Classes/**/*','PoCSDK/Frameworks/Sinch.frameworks/Headers/*'
#引用的第三方framework
s.ios.vendored_frameworks = 'PoCSDK/Sinch.framework'
#配置头文件
s.ios.public_header_files = 'PoCSDK/Frameworks/Sinch.frameworks/Headers/*'
s.resource_bundles = {
'NoddsSDK' => ['PoCSDK/Assets/*']
}
#指定项目中用到的系统库
s.frameworks = 'UIKit', 'MapKit', 'AudioToolbox', 'AVFoundation'
s.ios.dependency 'Alamofire', '~> 4.5'
s.ios.dependency 'Socket.IO-Client-Swift'
s.ios.dependency 'ReachabilitySwift'
其它问题
当在.podspec文件中通过s.vendored_frameworks 配置了引用的第三方framework,但是build时依然编译不过
出现找不到对应的文件时,如
iOS Undefined symbol: _OBJC_CLASS_$_XXXXXX , _OBJC_CLASS_$_XXX", referenced from: in XXX.o
解决方法:
1.点击Pods 在TARGETS 找到对应的插件。
2.切换到General -> Frameworks and Libraries 中点击加号+选择。
3.选择在插件目录中用到的framework.
3.视情况看是否对Framework标记为 Embed & Sign