How can i get currency symbols from currency code in iphone? - cocoa

I have get currency code (Eg: USD, EUR, INR) from webservice response. I need to show the currency symbols for the corresponding currency code. If the currency code is USD, i need to show $, if the currency code is EUR i need to show €. How can i do this? Please suggest any idea or sample code to do this. Please help me. Thanks in advance.

This code works charm in my project. I will share this to you all.
NSString *currencyCode = #"EUR";
NSLocale *locale = [[[NSLocale alloc] initWithLocaleIdentifier:currencyCode] autorelease];
NSString *currencySymbol = [NSString stringWithFormat:#"%#",[locale displayNameForKey:NSLocaleCurrencySymbol value:currencyCode]];
NSLog(#"Currency Symbol : %#", currencySymbol);
Thanks.

Swift 4 version
Finding locale by currency code:
let localeGBP = Locale
.availableIdentifiers
.lazy
.map { Locale(identifier: $0) }
.first { $0.currencyCode == "GBP" }
print(localeGBP?.currencySymbol) // £
Formatting currency
if let locale = localeGBP {
let formatter = NumberFormatter()
formatter.numberStyle = .currency
formatter.locale = locale
let result = formatter.string(from: 100000) // £100,000.00
}
Edit:
Why .lazy? Without it the loop would run over all the locale identifiers and return the first one which matches. That's about 700ish identifiers, and if the first one is the one you want then you have wasted creating 699 Locales :) With .lazy in there it automatically stops at the first matching one. In my case it reduces the number of times through the loop from 710 down to 22 when converting "GBP". This isn't important if you are only doing this once, but if you're doing this a number of times (i.e. over an array of symbols) then it's an easy way to get a bit more efficiency.

This code is what you are looking for though not very efficient because of loop for locales. Still it works correctly for all currency codes, not just for eur or usd. Hope this will help you.
- (NSLocale *) findLocaleByCurrencyCode:(NSString *)_currencyCode
{
NSArray *locales = [NSLocale availableLocaleIdentifiers];
NSLocale *locale = nil;
for (NSString *localeId in locales) {
locale = [[[NSLocale alloc] initWithLocaleIdentifier:localeId] autorelease];
NSString *code = [locale objectForKey:NSLocaleCurrencyCode];
if ([code isEqualToString:_currencyCode])
break;
else
locale = nil;
}
return locale;
}
- (NSString *)findCurrencySymbolByCode:(NSString *)_currencyCode
{
NSNumberFormatter *fmtr = [[NSNumberFormatter alloc] init];
NSLocale *locale = [self findLocaleByCurrencyCode:_currencyCode];
NSString *currencySymbol;
if (locale)
[fmtr setLocale:locale];
[fmtr setNumberStyle:NSNumberFormatterCurrencyStyle];
currencySymbol = [fmtr currencySymbol];
[fmtr release];
if (currencySymbol.length > 1)
currencySymbol = [currencySymbol substringToIndex:1];
return currencySymbol;
}
Use it this way:
NSString *currencySymbol = [self findCurrencySymbolByCode:currencyCode];

NSLocale will happily tell you the currency symbol used by a particular locale:
[locale objectForKey:NSLocaleCurrencySymbol];
It'll also tell you the currency code:
[locale objectForKey:NSLocaleCurrencyCode];
So all you have to do now is look up the locale that corresponds to a given code. There's no built-in method (that I'm aware of) to do this directly, so loop through all the known locales and pick the one that matches. #Umka's answer has a good example of this in the -findLocaleByCurrencyCode: method.
You could optimise the process by building your own lookup table, rather than iterating through all locales each time. You may need to handle the possibility of duplicate currency codes too, which would require some heuristic for deciding which is the most likely locale.

Building on code from #Mike Abdullah and #Umka, here are some functions in Swift.
func findCodeAndSymbolForAllLocales() {
let locales = NSLocale.availableLocaleIdentifiers()
for localeId in locales {
let locale = NSLocale(localeIdentifier: localeId)
if let code = locale.objectForKey(NSLocaleCurrencyCode) as? String,
let symbol = locale.objectForKey(NSLocaleCurrencySymbol) {
print("\(code) \(symbol)")
}
}
}
func findCurrencySymbolByCode(currencyCode:String) -> String? {
guard let locale = findLocaleByCurrencyCode(currencyCode) else {
print("locale for \(currencyCode) is nil")
return nil
}
return locale.objectForKey(NSLocaleCurrencySymbol) as? String
}
func findLocaleByCurrencyCode(currencyCode:String) -> NSLocale? {
let locales = NSLocale.availableLocaleIdentifiers()
var locale:NSLocale?
for localeId in locales {
locale = NSLocale(localeIdentifier: localeId)
if let code = locale!.objectForKey(NSLocaleCurrencyCode) as? String {
if code == currencyCode {
return locale
}
}
}
return locale
}

NSNumberFormatter * formatter = [NSNumberFormatter new];
formatter.numberStyle = NSNumberFormatterCurrencyStyle;
NSString * localeIde = [NSLocale localeIdentifierFromComponents:#{NSLocaleCurrencyCode: currencyCode}];
formatter.locale = [NSLocale localeWithLocaleIdentifier:localeIde];
NSString * symbol = formatter.currencySymbol;

One way is to start with a valid locale (e.g. the user current locale) and then override the currency code. Simple and efficient and allows you to use the newly constructed locale to configure a currency formatter in order to display a string according to the user's locale preferences:
NSLocale* locale = [NSLocale currentLocale];
if (_currencyCode) {
NSMutableDictionary* components = [[NSLocale componentsFromLocaleIdentifier:locale.localeIdentifier] mutableCopy];
components[NSLocaleCurrencyCode] = _currencyCode;
locale = [[NSLocale alloc] initWithLocaleIdentifier:[NSLocale localeIdentifierFromComponents:components]];
}
return locale.currencySymbol;

NSNumberFormatter *currencyFormatter = [[NSNumberFormatter alloc] init];
[currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
this is for US currency style
NSNumberFormatter *numberFormatter = [[[NSNumberFormatter alloc] init] autorelease];
//[numberFormatter setCurrencySymbol:#"Rs"];
[numberFormatter setNumberStyle:#"en_IN"];
This is for indian

Swift
let locale = NSLocale(localeIdentifier: currencyCode)
let currencySymbol = locale.displayName(forKey: .currencySymbol, value: currencyCode) ?? currencyCode
print("Currency symbol: \(currencySymbol)")

Related

Validating URL from drag and drop in a sandbox

With file access in a sandboxed osx app with swift in mind, does it work the same with URLs provided via Finder or other apps drops?
As there's no NSOpenPanel call to afford folder access as in this example, just urls - I think the folder access is implicit since the user dragged the file from the source / desktop "folder" much the same as implicit selection via the open dialog.
I have not begun the sandbox migration yet but wanted to verify my thinking was accurate, but here's a candidate routine that does not work in sandbox mode:
func performDragOperation(_ sender: NSDraggingInfo!) -> Bool {
let pboard = sender.draggingPasteboard()
let items = pboard.pasteboardItems
if (pboard.types?.contains(NSURLPboardType))! {
for item in items! {
if let urlString = item.string(forType: kUTTypeURL as String) {
self.webViewController.loadURL(text: urlString)
}
else
if let urlString = item.string(forType: kUTTypeFileURL as String/*"public.file-url"*/) {
let fileURL = NSURL.init(string: urlString)?.filePathURL
self.webViewController.loadURL(url: fileURL!)
}
else
{
Swift.print("items has \(item.types)")
}
}
}
else
if (pboard.types?.contains(NSPasteboardURLReadingFileURLsOnlyKey))! {
Swift.print("we have NSPasteboardURLReadingFileURLsOnlyKey")
}
return true
}
as no URL is acted upon or error thrown.
Yes, the file access is implicit. As the sandbox implementation is poorly documented and had/has many bugs, you want to work around URL and Filenames. The view should register itself for both types at initialisation. Code is in Objective-C, but API should be the same.
[self registerForDraggedTypes:[NSArray arrayWithObjects:NSFilenamesPboardType, NSURLPboardType, nil]];
Then on performDragOperation:
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
{
BOOL dragPerformed = NO;
NSPasteboard *paste = [sender draggingPasteboard];
NSArray *typesWeRead = [NSArray arrayWithObjects:NSFilenamesPboardType, NSURLPboardType, nil];
//a list of types that we can accept
NSString *typeInPasteboard = [paste availableTypeFromArray:typesWeRead];
if ([typeInPasteboard isEqualToString:NSFilenamesPboardType]) {
NSArray *fileArray = [paste propertyListForType:#"NSFilenamesPboardType"];
//be careful since this method returns id.
//We just happen to know that it will be an array. and it contains strings.
NSMutableArray *urlArray = [NSMutableArray arrayWithCapacity:[fileArray count]];
for (NSString *path in fileArray) {
[urlArray addObject:[NSURL fileURLWithPath:path]];
}
dragPerformed = //.... do your stuff with the files;
} else if ([typeInPasteboard isEqualToString:NSURLPboardType]) {
NSURL *droppedURL = [NSURL URLFromPasteboard:paste];
if ([droppedURL isFileURL]) {
dragPerformed = //.... do your stuff with the files;
}
}
return dragPerformed;
}

How to append new values to an already existing value in a parse database?

I have a table in parse.com database called as test. I have a column called phone number in it. So i successfully inserted a value in the column (ex. 123456789) now i want to append another value in the same field. (Ex. 123456789,987654321) How do I do that ? I am using Parse and Xcode together. The language which I am using is swift.
I know it's Swift, but you can do it like this in Objective-c, (I'm sure it's close to the swift version):
PFQuery *query = [PFQuery queryWithClassName:#"className"];
[query getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) {
if (object) {
// let's update your phone number
NSString *numbers = #"123";
NSString *newStr = [NSString stringWithFormat:#"%# %# ",object[#"telNumber"], numbers]
object[#"telNumber"] = newStr;
[object saveInBackground];
}
}];
Do you want to add another column or do you want to change the current value in the column phone number?
To add another column you can create a new colmn in parse or you create it programatically:
var stream = PFObject(className: "test")
stream["phoneNumber"] = "123"
stream["anotherValue"] = "abc"
func saveInBackgroundWithBlock(stream: PFObject) {
stream.saveInBackgroundWithBlock {
(succeeded: Bool, error: NSError?) -> Void in
var withStatus = "succ"
if !succeeded {
withStatus = "not succ"
}
print("Upload: \(withStatus)")
}
}
If you want to edit the current value you have to edit it in the following way:
let phoneNumber = "345"
stream.setValue(phoneNumber, forKey: "phoneNumber")
user?.saveInBackgroundWithBlock {
(succeeded: Bool, error: NSError?) -> Void in
var withStatus = "succ Change"
if !succeeded {
withStatus = "not SuccChange"
}
print("Upload: \(withStatus)")
}
If I answered or understood your question wrong, please feel free to correct me. Regards, Alex

How to get an ALAsset URL from a PHAsset?

You can do it sneakily† using the undocumented PHAsset.ALAssetURL property, but I'm looking for something documented.
† In Objective-C, this will help
#interface PHAsset (Sneaky)
#property (nonatomic, readonly) NSURL *ALAssetURL;
#end
Create the assetURL by leveraging the localidentifier of the PHAsset.
Example:
PHAsset.localidentifier returns 91B1C271-C617-49CE-A074-E391BA7F843F/L0/001
Now take the 32 first characters to build the assetURL, like:
assets-library://asset/asset.JPG?id=91B1C271-C617-49CE-A074-E391BA7F843F&ext=JPG
You might change the extension JPG depending on the UTI of the asset (requestImageDataForAsset returns the UTI), but in my testing the extensions of the assetURL seems to be ignored anyhow.
I wanted to be able to get a URL for an asset too. However, I have realised that the localIdentifier can be persisted instead and used to recover the PHAsset.
PHAsset* asset = [PHAsset fetchAssetsWithLocalIdentifiers:#[localIdentifier] options:nil].firstObject;
Legacy asset URLs can be converted using:
PHAsset* legacyAsset = [PHAsset fetchAssetsWithALAssetUrls:#[assetUrl] options:nil].firstObject;
NSString* convertedIdentifier = legacyAsset.localIdentifier;
(before that method gets obsoleted...)
(Thanks holtmann - localIdentifier is hidden away in PHObject.)
Here is working code tested on iOS 11 both simulator and device
PHFetchResult *result = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:nil];
[result enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
PHAsset *asset = (PHAsset *)obj;
[asset requestContentEditingInputWithOptions:nil completionHandler:^(PHContentEditingInput * _Nullable contentEditingInput, NSDictionary * _Nonnull info) {
NSLog(#"URL:%#", contentEditingInput.fullSizeImageURL.absoluteString);
NSString* path = [contentEditingInput.fullSizeImageURL.absoluteString substringFromIndex:7];//screw all the crap of file://
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL isExist = [fileManager fileExistsAtPath:path];
if (isExist)
NSLog(#"oh yeah");
else {
NSLog(#"damn");
}
}];
}];
Read the bottom!
The resultHandler for PHImageManager.requestImage returns 2 objects: result and info.
You can get the original filename for the PHAsset (like IMG_1043.JPG) as well as its full path on the filesystem with:
let url = info?["PHImageFileURLKey"] as! URL
This should work right, but for some reason it doesn't. So basically, you have to copy your image to a file then access that then delete it.
The PHImageFileURLKey is usable to get the original file name, but you cannot actually access that file. It probably has to do with the fact that code in the background can access the file while other apps can delete it.
Here is a PHAsset extension written in Swift that will retrieve the URL.
extension PHAsset {
func getURL(completionHandler : #escaping ((_ responseURL : URL?) -> Void)){
if self.mediaType == .image {
let options: PHContentEditingInputRequestOptions = PHContentEditingInputRequestOptions()
options.canHandleAdjustmentData = {(adjustmeta: PHAdjustmentData) -> Bool in
return true
}
self.requestContentEditingInput(with: options, completionHandler: {(contentEditingInput: PHContentEditingInput?, info: [AnyHashable : Any]) -> Void in
completionHandler(contentEditingInput!.fullSizeImageURL as URL?)
})
} else if self.mediaType == .video {
let options: PHVideoRequestOptions = PHVideoRequestOptions()
options.version = .original
PHImageManager.default().requestAVAsset(forVideo: self, options: options, resultHandler: {(asset: AVAsset?, audioMix: AVAudioMix?, info: [AnyHashable : Any]?) -> Void in
if let urlAsset = asset as? AVURLAsset {
let localVideoUrl: URL = urlAsset.url as URL
completionHandler(localVideoUrl)
} else {
completionHandler(nil)
}
})
}
}
}

UIActivityViewController customize text based on selected activity

I want to customize text for the same information but when I am sharing it on Facebook I don't want to use the twitter hash tags or #username scheme...
How can I diversify text for sharing based on which sharing service would be used?
Ofcourse I'm using UIActivityViewController:
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:#[shareText, shareURL] applicationActivities:nil];
I took this answer and made a simple class for it. The default message will be seen by sharing outlets other than Twitter, and for Twitter words within the hashWords array will appear with hashes if they are present in the default message. I thought I would share it for anyone else who needs it. Thanks Christopher!
Usage:
TwitterHashActivityItemProvider *twit = [[TwitterHashActivityItemProvider alloc] initWithDefaultText:#"I really like stackoverflow and code"
hashWords:#[#"stackoverflow", #"code"]];
NSArray *items = #[twit];
UIActivityViewController *act = [[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:nil];
Header:
#interface TwitterHashActivityItemProvider : UIActivityItemProvider
- (id)initWithDefaultText:(NSString*)text hashWords:(NSArray*)hashItems;
#property (nonatomic,strong) NSArray *hashItems;
#end
Implementation:
#import "TwitterHashActivityItemProvider.h"
#implementation TwitterHashActivityItemProvider
- (id)initWithDefaultText:(NSString*)text hashWords:(NSArray*)hashItems;
{
self = [super initWithPlaceholderItem:text];
if ( self )
{
self.hashItems = hashItems;
}
return self;
}
- (id)item
{
if ( [self.placeholderItem isKindOfClass:[NSString class]] )
{
NSString *outputString = [self.placeholderItem copy];
// twitter gets some hash tags!
if ( self.activityType == UIActivityTypePostToTwitter )
{
// go through each potential hash item and augment the main string
for ( NSString *hashItem in self.hashItems)
{
NSString *hashed = [#"#" stringByAppendingString:hashItem];
outputString = [outputString stringByReplacingOccurrencesOfString:hashItem withString:hashed];
}
}
return outputString;
}
// else we didn't actually provide a string...oops...just return the placeholder
return self.placeholderItem;
}
#end
Instead of passing the text strings into the initWithActivityItems call, pass in your own sub-class of the UIActivityItemProvider class and when you implement the itemForActivityType method it will provide the sharing service as the 'activityType' parameter.
You can then return the customized content from this method.
Swift implementation example of an UIActivityItemProvider subclass. Copy option will use only the password, other activity types will use the full share text. Should be easy to customize for different use cases. Credit to Cristopher & NickNack for their answers.
class PasswordShareItemsProvider: UIActivityItemProvider {
private let password: String
private var shareText: String {
return "This is my password: " + password
}
init(password: String) {
self.password = password
// the type of the placeholder item is used to
// display correct activity types by UIActivityControler
super.init(placeholderItem: password)
}
override var item: Any {
get {
guard let activityType = activityType else {
return shareText
}
// return desired item depending on activityType
switch activityType {
case .copyToPasteboard: return password
default: return shareText
}
}
}
}
Usage:
let itemProvider = PasswordShareItemsProvider(password: password)
let activityViewController = UIActivityViewController(activityItems: [itemProvider], applicationActivities: nil)

How to block some keyboard layouts in NSTextField?

Is there a way to block some keyboard layouts (input sources) in NSTextField.
I need to block all non-romans languages such as Russian, Belorussian, Ukraine and etc or disable all languages and enable only English/Deutsch language.
If it will be not so hard - make some example please.
UPD:
I think i need to use this
but how? =)
Checking just the keyboard attached is maybe a bit flakey. With the Option key you can input a lot non-Roman characters from any keyboard, for instance. Not to mention copy and paste.
A better approach would be to make a subclass of NSFormatter and implement isPartialStringValid:proposedSelectedRange:originalString:originalSelectedRange:errorDescription:
A simple implementation could be something like this:
- (BOOL)isPartialStringValid:(NSString **)partialStringPtr proposedSelectedRange:(NSRangePointer)proposedSelRangePtr originalString:(NSString *)origString originalSelectedRange:(NSRange)origSelRange errorDescription:(NSString **)error
{
NSString *partialString = *partialStringPtr;
NSCharacterSet *acceptedCharacters = [NSCharacterSet characterSetWithCharactersInString: #"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"];
NSCharacterSet *notAcceptedCharacters = [acceptedCharacters invertedSet];
BOOL needsCheck = YES;
BOOL didChange = NO;
do {
NSRange rng = [partialString rangeOfCharacterFromSet:notAcceptedCharacters];
if ( !NSEqualRanges(rng, NSMakeRange(NSNotFound, 0)) ) {
partialString = [partialString stringByReplacingCharactersInRange:rng withString:#""];
didChange = YES;
}
else {
needsCheck = NO;
}
} while (needsCheck);
if ( didChange ) {
*partialStringPtr = partialString;
NSRange newRange = origSelRange;
newRange.length = 0;
*proposedSelRangePtr =newRange;
return NO;
}
return YES;
}
When subclassing NSFormatter you are also required to implement stringForObjectValue: and getObjectValue:forString:errorDescription:, but since you are inputting a string, they can just pass the input string straight through.

Resources