嘿,兄弟!移动端调试工具Flipper了解一下(下)

avatar
Android @奇舞团Android团队
原文链接: mp.weixin.qq.com

内容简介:上期文章讲过关于Flipper在移动端的使用,主要介绍了Android上的使用,本期来讲一下关于iOS上的集成。

传送门:嘿,兄弟!移动端调试工具Flipper了解一下

Objective-c集成指南

初始化Podfile

相比于 Android ,iOS 集成相对简单一些。可以使用 CocoaPods 。新建项目,然后 pod init 初始化 Podfile 文件,并添加 Flipper 的依赖,例如:

    project 'democome.xcodeproj'

    source 'https://github.com/facebook/flipper.git'

    source 'https://github.com/CocoaPods/Specs'

    swift_version = "4.1"

    flipperkit_version = '0.14.1'

    target 'democome' do

     platform :ios, '9.0'

     pod 'FlipperKit', '~>' + flipperkit_version

     pod 'FlipperKit/FlipperKitLayoutComponentKitSupport', '~>' + flipperkit_version

     pod 'FlipperKit/SKIOSNetworkPlugin', '~>' + flipperkit_version

     pod 'FlipperKit/FlipperKitUserDefaultsPlugin', '~>' + flipperkit_version

     # This post_install script adds swift version to yogakit's pod target.

     # It also adds -DFB_SONARKIT_ENABLED=1 flag to OTHER_CFLAGS, necessary to build expose Flipper classes in the header files

     post_install do |installer|

           installer.pods_project.targets.each do |target|

               if ['YogaKit'].include? target.name

                   target.build_configurations.each do |config|

                       config.build_settings['SWIFT_VERSION'] = swift_version

                   end

               end

           end

           file_name = Dir.glob("*.xcodeproj")[0]

           app_project = Xcodeproj::Project.open(file_name)

           app_project.native_targets.each do |target|

               target.build_configurations.each do |config|

                 if (config.build_settings['OTHER_CFLAGS'])

                   if !(config.build_settings['OTHER_CFLAGS'].include? '-DFB_SONARKIT_ENABLED=1')

                     puts 'Adding -DFB_SONARKIT_ENABLED=1 in OTHER_CFLAGS...'

                     config.build_settings['OTHER_CFLAGS'] << '-DFB_SONARKIT_ENABLED=1'

                   end

                 else

                   puts 'OTHER_CFLAGS does not exist, assigining it to `$(inherited), -DFB_SONARKIT_ENABLED=1` '

                   config.build_settings['OTHER_CFLAGS'] = '$(inherited) -DFB_SONARKIT_ENABLED=1 '

                 end

                 app_project.save

               end

           end

      end

    end

AppDelegate添加如下代码

    FlipperClient *client = [FlipperClient sharedClient];

    SKDescriptorMapper *layoutDescriptorMapper = [[SKDescriptorMapper alloc] initWithDefaults];

    [FlipperKitLayoutComponentKitSupport setUpWithDescriptorMapper: layoutDescriptorMapper];

    [client addPlugin: [[FlipperKitLayoutPlugin alloc] initWithRootNode: application

                                                  withDescriptorMapper: layoutDescriptorMapper]];

    [client addPlugin:[[FKUserDefaultsPlugin alloc] initWithSuiteName:nil]];  [client start];

    [client addPlugin: [[FlipperKitNetworkPlugin alloc] initWithNetworkAdapter:[SKIOSNetworkAdapter new]]];

    [client start];

运行效果如下

Swift集成指南

初始化Podfile

    project 'democome-swift.xcodeproj'

    source 'https://github.com/facebook/flipper.git'

    source 'https://github.com/CocoaPods/Specs'

    swift_version = "4.2.1"

    flipperkit_version = '0.14.1'

    target 'democome-swift' do

     platform :ios, '9.0'

     pod 'FlipperKit', '~>' + flipperkit_version

     # Layout and network plugins are not yet supported for swift projects

     pod 'FlipperKit/FlipperKitLayoutComponentKitSupport', '~>' + flipperkit_version

     pod 'FlipperKit/SKIOSNetworkPlugin', '~>' + flipperkit_version

     pod 'FlipperKit/FlipperKitUserDefaultsPlugin', '~>' + flipperkit_version

     # If you use `use_frameworks!` in your Podfile,

     # uncomment the below $static_framework array and also

     # the pre_install section.  This will cause Flipper and

     # it's dependencies to be static and all other pods to

     # be dynamic.

     # $static_framework = ['FlipperKit', 'Flipper', 'Folly',

     #   'CocoaAsyncSocket', 'ComponentKit', 'DoubleConversion',

     #   'glog', 'PeerTalk', 'RSocket', 'Yoga', 'YogaKit',

     #   'CocoaLibEvent', 'OpenSSL-Static', 'boost-for-react-native']

     # pre_install do |installer|

     #     installer.pod_targets.each do |pod|

     #       if $static_framework.include?(pod.name)  

     #         pod.host_requires_frameworks = false

     #       end

     #   end

     # end

    # This post_install script adds -DFB_SONARKIT_ENABLED flag to OTHER_SWIFT_FLAGS, necessary to build swift target

       post_install do |installer|

         file_name = Dir.glob("*.xcodeproj")[0]

         app_project = Xcodeproj::Project.open(file_name)

         app_project.native_targets.each do |target|

             target.build_configurations.each do |config|

               if (config.build_settings['OTHER_SWIFT_FLAGS'])

                 if !(config.build_settings['OTHER_SWIFT_FLAGS'].include? '-DFB_SONARKIT_ENABLED')

                   puts 'Adding -DFB_SONARKIT_ENABLED ...'

                   swift_flags = config.build_settings['OTHER_SWIFT_FLAGS']

                   if swift_flags.split.last != '-Xcc'

                     config.build_settings['OTHER_SWIFT_FLAGS'] << ' -Xcc'

                   end

                   config.build_settings['OTHER_SWIFT_FLAGS'] << ' -DFB_SONARKIT_ENABLED'

                 end

               else

                 puts 'OTHER_SWIFT_FLAGS does not exist thus assigning it to `$(inherited) -Xcc -DFB_SONARKIT_ENABLED`'

                 config.build_settings['OTHER_SWIFT_FLAGS'] = '$(inherited) -Xcc -DFB_SONARKIT_ENABLED'

               end

               app_project.save

             end

           end

       end

     end

这里需要注意Swift版本,需要和你安装的版本保持一致,否则可能会报错,查看Swift版本命令如下:

    xcrun swift -version

AppDelegate添加如下代码

    let client = FlipperClient.shared()

    let layoutDescriptorMapper = SKDescriptorMapper(defaults: ())

    FlipperKitLayoutComponentKitSupport.setUpWith(layoutDescriptorMapper)

    client?.add(FlipperKitLayoutPlugin(rootNode: application, with: layoutDescriptorMapper!))

    client?.start()

运行结果类似,不在贴出。

--END--

识别二维码,关注我们