ios - From a main app, how can a framework get its own bundle? (Localizable.strings) -
so have main ios app includes framework made.
in framework, created localizable helper date helper. date helper has function timeago()
can return "8 hours ago". , want localize string. have in framework:
public extension string { public func localized(in bundle: bundle) -> string { if let path = bundle.path(forresource: localize.currentlanguage(), oftype: "lproj"), let bundle = bundle(path: path) { return bundle.localizedstring(forkey: self, value: nil, table: nil) } return self } } public extension date { public func timeago(format: dateunitformat = .short) -> string { let thisframeworkbundle = bundle(for: localize.self) // class declared in framework //whatever, display localized word return "second".localized(in: thisframeworkbundle) } }
when run local test (unit testing inside framework itself), localize thing works. bundle found.
but when call timeago
main app, doesn't work anymore.
i tried change line let thisframeworkbundle = bundle(for: localize.self)
let thisframeworkbundle = bundle(identifier: "com.myidentifier")
, same thing (so works within framework, won't work when called main app).
edit:
okay, noticed bundle(identifier: "com.myidentifier")
won't work within framework because i'm using cocoapods , overrides framework identifier cocoapod one. however, still don't understand why using bundle(for: frameworkclass.self)
won't work main app (but works in unit test of framework)
at moment, can give 2 ways. last year i've been using this:
public func returnimage(_ named:string) -> uiimage { let mybundle = bundle.init(identifier: "com.[company].[framework]") let imagepath = (mybundle?.path(forresource: "images", oftype: "bundle"))! + "/" + named let theimage = uiimage(contentsoffile: imagepath) return theimage! }
the key piece tying mybundle
variable bundle id defines in project.
i subscribe blog called useyourloaf. very, resource. today post, contains different way - using classes , bundleforclass
. post comment asking pros/cons between our approaches.
what can tell if named bundleid, can resources using routine.
Comments
Post a Comment