在自定义 Cocoapods 中使用故事板和图像资源

11

我正在尝试使用CocoaPods将一个大型iOS项目(用Swift开发)模块化。想法是创建“子应用程序”,包括storyboards和资源,这些可以集成到主项目中。

但是,在这种情况下使用storyboards时遇到了问题。问题类似于(我认为): https://github.com/CocoaPods/CocoaPods/issues/2597

这是我正在尝试转换为pod的模块的.podspec:

Pod::Spec.new do |spec|
  spec.name = 'GlycoAppMenuModule'
  spec.version = '0.0.8'
  spec.license = { :type => 'MIT', :file => 'LICENSE' }
  spec.homepage = 'https://google.com'
  spec.platform = :ios, "8.0"
  spec.authors = { 'Abdulla Contractor' => 'abdulla.contractor@gmail.com' }
  spec.summary = 'Views for menu page'
  spec.source = { :git => 'https://github.com/Holmusk/GlycoAppMenuModule.git', :tag => '0.0.8' }
  spec.source_files = 'GlycoAppMenuModule/*'
  spec.resource_bundles = {
    'resources' => ['GlycoAppMenuModule/**/*.{lproj,storyboard}']}
  # spec.framework = 'Foundation'
end

这里是我尝试使用故事板的代码。

let bundlePath = NSBundle(forClass: GANavigationMenuViewController.self).pathForResource("resources", ofType: "bundle")
        let bundle = NSBundle(path: bundlePath!)
        let storyboard: UIStoryboard = UIStoryboard(name: "Menu", bundle: bundle)
        let vc = storyboard.instantiateInitialViewController() as! UIViewController
        self.presentViewController(vc, animated: false, completion: nil)

我遇到的错误如下:

2015-06-05 11:39:40.365 temp[3593:50802] Unknown class _TtC9resources30GANavigationMenuViewController in Interface Builder file.
2015-06-05 11:39:40.370 temp[3593:50802] Could not load the "icAccount.png" image referenced from a nib in the bundle with identifier "(null)"
2015-06-05 11:39:40.371 temp[3593:50802] Could not load the "icConnect.png" image referenced from a nib in the bundle with identifier "(null)"
2015-06-05 11:39:40.371 temp[3593:50802] Could not load the "icDiabetesProfile.png" image referenced from a nib in the bundle with identifier "(null)"
2015-06-05 11:39:40.373 temp[3593:50802] Could not load the "icLogout.png" image referenced from a nib in the bundle with identifier "(null)"
2015-06-05 11:39:40.377 temp[3593:50802] Could not load the "icCircle.png" image referenced from a nib in the bundle with identifier "(null)"
2015-06-05 11:39:40.386 temp[3593:50802] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UIViewController 0x7fb3f3d2cdd0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key accountButton.'

你有什么想法,我可能错过了什么吗?


看起来类似的问题:https://dev59.com/onA75IYBdhLWcg3w0sl9 - aman.sood
1个回答

9

我曾经遇到同样的问题,但是我解决了它。

在你的.podspec文件中,唯一需要的资源标签是:

s.resources = "MMA_Piece/**/*.{png,jpeg,jpg,storyboard,xib,xcassets}"

现在你可能会问:"我当然已经尝试过这个了,但它没起作用。为什么现在要起作用呢?"

那么让我告诉你 :D

确保你想要包含在自定义CocoaPod中的每个资源都在你的最底层项目目录中。在我的例子(MMA_Piece 项目)中,这是以下文件夹(所选的文件夹):

enter image description here

完成!如果您通过使用“pod install”在另一个项目中使用该pod,则您的资源将随之而来,如下所示:

enter image description here

重要提示:如果您想在其他项目中使用此资源,例如“MainProject”,则您始终需要指定bundle!否则,“MainProject”将无法找到该资源!

希望这能帮助您!


2
非常感谢!我搜寻了几个小时,最终发现奇怪的是需要使用 s.resources 而不是 spec.resource_bundles(它创建了另一个目标,而我将无法从中获取 bundle)。愤怒 - Nike Kov
嗨@ErikBrandsma,你是对的,你的方法很有效。然而,它会破坏故事板国际化,因为编译后的.storyboardc.strings文件被复制到主捆绑存储库中,而不是在*.lproj目录中。 - vmeyer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接