Flutter插件开发《iOS原生模块开发》

博客围绕Flutter中iOS原生模块开发展开,介绍了引入第三方库的方法。开发中常需用到原生组件、插件或与原生模块通信,当现有资源不满足需求时需自行引入。文中介绍了直接引用、将第三方库复制到新文件夹、处理非framework库而是文件这三种引入方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

上一篇简单的整理了一下Podspec语法整理,主要是为了这一篇Flutter中iOS原生模块开发。

简介

在开发Flutte中我们难免会遇到原生组件、插件或者与原生模块通信,比如地图、引入第三方sdk如微信、支付宝等SDK,还有摄像头SDK,我们必须要用到原生, 当然你也可以用pub.dev/flutter中的,但是这并不是最终的解决之道,Flutter刚发展不久,假如刚好没有或者并不满足你的需求,因此这就需要自己动手,俗话说自己动手丰衣足食

用法一,直接引用

这种方法比较简单就像iOS项目中直接引用,前提是pod search可以搜索到它。


#
# To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html
#
Pod::Spec.new do |s|
  s.name             = 'flutter_txmap_plugin'
  s.version          = '0.0.1'
  s.summary          = '一款腾讯地图Flutter插件'
  s.description      = <<-DESC
一款腾讯地图Flutter插件
                       DESC
  s.homepage         = 'http://example.com'
  s.license          = { :file => '../LICENSE' }
  s.author           = { 'Your Company' => 'my_snail@126.com' }
  s.source           = { :path => '.' }
  s.source_files = 'Classes/**/*'
  s.public_header_files = 'Classes/**/*.h'
  s.dependency 'Flutter'
  ## 引入,还可以携带版本号
  s.dependency 'AFNetworking', '~> 1.0'

  s.ios.deployment_target = '9.0'
end

复制代码

用法二,将第三方库copy到新建的文件夹中

这种方法我们不可以像第一种直接引入,因为pod search根本搜索不到,比如腾讯地图,遇到这种方式的话只有下载他们的库,然后引入,例如:

#
# To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html
#
Pod::Spec.new do |s|
  s.name             = 'flutter_txmap_plugin'
  s.version          = '0.0.1'
  s.summary          = '一款腾讯地图Flutter插件'
  s.description      = <<-DESC
一款腾讯地图Flutter插件
                       DESC
  s.homepage         = 'http://example.com'
  s.license          = { :file => '../LICENSE' }
  s.author           = { 'Your Company' => 'my_snail@126.com' }
  s.source           = { :path => '.' }
  s.source_files = 'Classes/**/*'
  s.public_header_files = 'Classes/**/*.h'
  s.dependency 'Flutter'
  # 引入资源,比如我们想要显示标注的图片
  s.resources = ['Images/*.png']
  # 腾讯地图的framework库,pod search搜索的应该是很早的版本和最新的api对应不上
  s.vendored_frameworks = 'Frameworks/QMapKit.framework', 'Frameworks/TencentLBS.framework'

  s.ios.deployment_target = '9.0'
end

复制代码

第三种,假如不是framework库而是.a文件

最近在引入一个摄像头sdk和配置设备到Wi-Fi下面,都是.a的库,这种方式又是如何引入了,也是搞了好久,终于在guides.cocoapods.org/syntax/pods…中找到了 如下:

#
# To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html
#
Pod::Spec.new do |s|
  s.name             = 'erazl_plugin'
  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/**/*'
  s.public_header_files = 'Classes/**/*.h', 'Library/*.h'
  # 引入Library文件夹下所有的*.a库
  s.vendored_libraries = 'Library/*.a'
  s.frameworks = 'MobileCoreServices', 'CFNetwork', 'CoreGraphics'
  # 只是引入*.a库还是不行,还是要引入相关联的系统库,否则无法编译通过
  # 用户目标(应用程序)需要链接的系统库列表,
  s.libraries = 'z.1.2.5', 'c++', 'c', 'iconv.2.4.0', 'sqlite3', 'stdc++.6.0.9', 'xml2', 'bz2.1.0', 'resolv', 'xml2', 'z'
  s.dependency 'Flutter'

  s.ios.deployment_target = '9.0'
end


复制代码

上诉就是最近在开发iOS原生模块时遇到的各种问题,在此做个简单的总结,如果还有其他方式欢迎互相学习

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值