use_frameworks!只有一些 pods 或 swift pods

use_frameworks! for only some pods or swift pods

我的项目中有 Objective CSwift Pods。

pod 'WBWebViewConsole', '~> 1.0.1' 
use_frameworks! 
pod 'XWebView', '~>0.9.5’ 
pod 'Starscream', '~> 1.1.3'

As swift PODs(XWebView, Starscream) 只能添加为框架我必须采用 use_frameworks!

但这使得所有 PODs 作为框架,包括 Objective-C PODs(WBWebViewConsole)。

但这会导致 Objective-C POD 出现问题,但我不打算将 Objective-C POD 作为框架。

所以无论如何我可以忽略几个 PODs 被转换为框架?

谢谢。

更新:

如何重现问题?

问题出在 POD WBWebViewConsole

运行 任何 iOS 8+ 设备中的 attached project 从 google 驱动器加载 html 文档时具有互联网连接。

在 html 文档中……单击 General/Info/Warning/Debug/Error 日志

只要您单击上述任何按钮,您就会在 html 页面上显示一些文本……

库是关于捕获在 html 页面中生成的日志…

除了在 html 页面中显示一些文本外,每当您单击按钮时,我都会在后台编写一些登录信息。

现在点击按钮 get logs ... 并查看 Xcode IDE 中的日志...您将看到生成的所有控制台日志html

Get logs-> 是一个超级强加在 webview 上的原生按钮..这个库让我们从 wkwebview 读取控制台日志 ..

现在在 POD 中 你取消注释以下行

use_frameworks!
pod 'PLCrashReporter'
pod 'XWebView', '~> 0.9.5’

并安装 pod

然后将以下代码粘贴到 ViewController

之上
import WBWebViewConsole

这是 project,所有这些都没有注释,重现问题所需的所有更改

现在你 运行 设备上的项目并单击 html 页面中的一些按钮并单击 获取日志 你看不到生成的评论html 页

具体如下,在 WBWKWebView 中实现的委托方法没有被触发。

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
- (void)wb_evaluateJavaScript:(NSString *)javaScriptString completionHandler:(void (^)(NSString *, NSError *))completionHandler

但是在创建 WBWKWebView 的实例并加载 URL 时,此 class 的所有必要委托都被解雇了。

当单击 html 页面中的按钮写入日志时,上面的代表应该会触发

基于 CocoaPods 的成员:

这对于一般情况是不可能的,因为存在传递依赖性。如果 Pod A 是动态构建的并且依赖于静态构建的 Pod B,并且应用程序也依赖于 Pod B,则无法构建,因为任何一个 Pod A 都会在 link 处丢失 Pod B 的符号时间,或者你最终会得到 Pod B 的多个副本。可能还有更多的场景是行不通的。

但是,您应该能够创建一个插件来支持像您这样的特定情况。

                    source 'https://github.com/CocoaPods/Specs.git'
                    platform :ios, '8.0'
                    use_frameworks!
                    pod 'WBWebViewConsole', '~> 1.0.1' 
                    pod 'XWebView', '~>0.9.5’ 
                    pod 'Starscream', '~> 1.1.3'

无需在 use_framework! 上方指定 objective c pod。

JSBridge初始化时,它会在webview中添加一些"UserScripts",其源是从以前版本的[NSBundle mainBundle]加载的。但是如果它在框架中,资源文件在框架包中而不是 mainBundle.

所以修复在 WBWebViewConsoleDefines

替换这个

inline static NSBundle * WBWebBrowserConsoleBundle()
{
return [NSBundle bundleWithPath:[NSString stringWithFormat:@"%@/%@", [[NSBundle mainBundle] bundlePath], @"WBWebBrowserConsole.bundle"]];
}

inline static NSBundle * WBWebBrowserConsoleBundle()
{
return [NSBundle bundleWithPath:[NSString stringWithFormat:@"%@/%@", [NSBundle bundleForClass:[WBWebViewConsole class]], @"WBWebBrowserConsole.bundle"]]; 
}

其实作者已经发布了新版本它也有修复

pod 'WBWebViewConsole', '~> 1.0.2'

  1. pod 安装
  2. 重新启动 Xcode 并清理您的项目
  3. 再次构建并运行

注意:这是不可能的。如果你use_frameworks!一切都变成了动态框架。

我在我的项目中同时使用了Objective-C和Swift、pods。如果必须包含用 Swift 编写的 pods,则必须使用指令 use_frameworks!。将 use_frameworks! 指令与非 Swift pods 一起使用不会导致问题。将指令放在 pods 列表的顶部。

一般来说,Objective-C pods 应该只要在你想使用框架的任何 swift 文件中使用 import ModuleName 就可以工作,但如果没有,那么你应该尝试将 bridging-header.h 添加到您的项目中,并在其中 #import 添加 objective-c 框架的头文件。

请尝试此操作并在此处更新结果。

您可以覆盖 use_frameworks! 并将所有 pods 安装为静态,除了您选择作为框架的那些

Podfile 中使用 use_frameworks! 并在文件末尾添加

dynamic_frameworks = ['aaa','bbb'] # <- swift libraries names

# Make all the other frameworks into static frameworks by overriding the static_framework? function to return true
pre_install do |installer|
  installer.pod_targets.each do |pod|
    if !dynamic_frameworks.include?(pod.name)
      puts "Overriding the static_framework? method for #{pod.name}"
      def pod.static_framework?;
        true
      end
      def pod.build_type;
        Pod::BuildType.static_library
      end
    end
  end
end

根据@MujtabaFR 的回答,我有错误说在我的 pod 全局导入头文件中找不到文件,我无法解决这些问题。所以在已经工作的静态库中,我尝试插入我的动态框架,所以我尝试了他的向后兼容的解决方案,因此逆转他的解决方案对我来说很好,这里供参考

dynamic_frameworks = ['aaa','bbb'] # <- swift libraries names

# Make all the other frameworks into dynamic frameworks by overriding the dynamic_framework? function to return true
pre_install do |installer|
  installer.pod_targets.each do |pod|
    if dynamic_frameworks.include?(pod.name)
      puts "Overriding the dynamic_framework? method for #{pod.name}"
      def pod.dynamic_framework?;
        true
      end
      def pod.build_type;
        Pod::BuildType.dynamic_framework
      end
    end
  end
end

所以这仍然是我现有的代码,并插入了我的新 swift pod 作为动态框架。

谢谢@MujtabaFR