Closed lsamaria closed 4 years ago
For some reason the flag was't showing in certain classes. I couldn't find the exact issue as to why the UIImage(named: code, in: Bundle.FlagIcons, compatibleWith: nil)
was nil but I realized there aren't any alternatives for the FPNCountry
flag
property other than the UIImage(named: "unknown"...
. Considering Apple has its own country flag emojis I decided to use them as an alternative. I followed these answers from Stack Overflow
Swift turn a country code into a emoji flag via unicode
Convert Apple Emoji (String) to UIImage
If the FPNCountry
flag
property is nil instead of creating an image namedUIImage(named: "unknown"...
I took the 2 digit country value from the code
property and passed it into a function that converts it into an emoji flag
of type String
.
I extended
the String
class to add an image()
function that converts a String
to an Image
. If the image isn't nil I pass the image to the flag
property. If for some reason it is nil then I use the UIImage(named: "unknown"...
as a final alternative.
It should be noted that both functions 100% work but because this is a pod I wasn't able to get the FPNCountry
class to recognize the changes. Assuming the author accepts and tests them this is a decent backup plan.
I'm agree with the concept but like I said in an other issue (https://github.com/chronotruck/FlagPhoneNumber/issues/70) I'm afraid Apple will be rejected apps with emojis that don't come from the user.
Oh that sucks, I didn’t know that. Ok I’m filing an issue later. The “unknown” flag doesn’t appear. I looked into your assets and there are warning ⚠️ labels on it but also on all the other flag images. Thanks for responding.
what about using this pod as an alternative?
https://github.com/madebybowtie/FlagKit
public struct FPNCountry {
public var code: FPNCountryCode
public var name: String
public var phoneCode: String
var flag: UIImage?
init(code: String, name: String, phoneCode: String) {
self.name = name
self.phoneCode = phoneCode
self.code = FPNCountryCode(rawValue: code)!
if let flag = UIImage(named: code, in: Bundle.FlagIcons, compatibleWith: nil) {
self.flag = flag
} else {
// use it here
if let countryFlag = Flag(countryCode: countryCode) {
self.flag = countryFlag
} else {
self.flag = UIImage(named: "unknown", in: Bundle.FlagIcons, compatibleWith: nil)
}
}
}
}
At start I used FlagKit but I removed it because the minimum deployment target is 10.0
. I'm working on a new release. I think it will fix a lot of issue. Thanks to propose things.