Am getting the device local currency symbol by using this code.
NSLocale *theLocale = [NSLocale currentLocale];
NSString *symbol = [theLocale objectForKey:NSLocaleCurrencySymbol];
NSLog(#"Symbol : %#",symbol);
NSString *code = [theLocale objectForKey:NSLocaleCurrencyCode];
NSLog(#"Code : %#",code);
When the app restart the currency symbol changing but when the app from background the currency symbol not updating.
In iPhone setting i have changed the region United States and launched the app the currency symbol show "$".
When i have change the region India and get the app from Background and call the same code it shows again "$". The currency symbol not updating. When i came back and reenter into the screen the currency symbol updating to India symbol.
In appDelegate:
- (void)applicationWillEnterForeground:(UIApplication *)application:
[[NSNotificationCenter defaultCenter] postNotificationName:#"CurrencyUpdated" object:nil];
In sampleViewController: viewDidLoad
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(reloadCurrencySymbol) name:#"CurrencyUpdated" object:nil];
In sampleViewController:
-(void) reloadCurrencySymbol
{
NSLocale *theLocale = [NSLocale currentLocale];
NSString *symbol = [theLocale objectForKey:NSLocaleCurrencySymbol];
NSLog(#"Symbol : %#",symbol);
NSString *code = [theLocale objectForKey:NSLocaleCurrencyCode];
NSLog(#"Code : %#",code);
}
Can anyone please help to solve this issue? Thanks in advance.
EDIT
I have recalled the [self viewDidLoad]; but the region has not changed. When i return to previous screen and again enter into the same screen the region is updating. Can anyone please help to solve this? Thanks.
in your sampleview controller add this in viewdidLoad
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(reloadCurrencySymbol) name: UIApplicationDidBecomeActiveNotification object:nil];
this is the right way to reload your symbol when the app becomes active
Likely its similar to MacOSX where when you change the language you need to restart the app to get it to start with the new locale.
I'm guessing that you would need to quit first, and there's probably no other way around it.
Related
I have used Push notification in my application, when the notification arrives i will get some values in the payload and i need to update the values in my application. I have amount label in all the views, so i need to update the amount after receiving the notification. But the problem is, the value doesn't update instantly, when i navigate to any view after that it will update. I want to update the value instantly, so how can i do that?
-(void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
NSString *getAmt =[[userInfo valueForKey:#"aps"] valueForKey:#"Amount"];
float totalAmt = [getAmt floatValue] + [amt floatValue];
NSString *finalAmount = [NSString stringWithFormat:#"%.02f",totalAmt];
[[NSUserDefaults standardUserDefaults] setObject:finalAmount forKey:#"amount"];
[self.viewController.view setNeedsDisplay];
}
I am using the Nsuserdefault value for displaying the amount in the amount label. Now the values are changed, when i am navigates to any view after that it will update. But i want to update the value instantly in my application, after getting the notification. So please help me out.
Thanks!
use NSNotication and add a notification against a method of class where method is implemented, when you receive push notification, post that notification and in method update values
Well, you can use NSNotificationCenter for this purpose..
Register you desired UIViewController in NSNotificationCenter for #"NotificaitonRecivied"
like this:
[[NSNotificationCenter defaultCenter] addObserver:self
selector: #selector(updateAmount:)
name: #"NotificaitonRecivied"
object: nil];
now whenever you receive any updated amount, simply broadcast a notification like this:
[[NSNotificationCenter defaultCenter] postNotificationName:#"NotificaitonRecivied" object:nil];
updateAmount: amount will be a method to read your amount from NSUserDefaults to set it on any View
quick easy question
while using a WebView with some text in it - the user can select a snippet of text from it
and press a UIButton which I created - running the following action:
-(IBAction)copyToClip
{
NSString *copyClip = [UIPasteboard generalPasteboard].string;
NSLog(#"Clip = %#",copyClip);
// (works fine)
}
I would like to call the same function without a UIButton, thus when the user did a "copy" action it will activate the above code. (I assume a listener)
what would be the appropriate listener for this?
Use NSNotificationCenter and register for UIPasteboardChangedNotification:
http://developer.apple.com/library/IOs/documentation/UIKit/Reference/UIPasteboard_Class/Reference.html#//apple_ref/c/data/UIPasteboardChangedNotification
[[NSNotificationCenter defaultCenter] addObserver:object selector:#selector(copyToClip) name:UIPasteboardChangedNotification object:nil];
If someone is interested in the Xamarin/C# version:
NSNotificationCenter.DefaultCenter.AddObserver(UIPasteboard.ChangedNotification,
notification => {
// custom code here
});
I'm having an error occur when I read the NSUserDefaults (via InAppSettingsKit). I'm not sure if it's my code that's the issue though. I have set up an observer to check if there are any changes to the NSUserDefaults:
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self
selector:#selector(setOptions)
name:NSUserDefaultsDidChangeNotification
object:nil];
The method this calls is used to update the 'map type' of an MKMapView:
- (void)setOptions
{
// Get the map style
NSString *mapStyle = [[NSUserDefaults standardUserDefaults] stringForKey:kMapType];
// Update map style
if ([mapStyle isEqualToString:#"Hybrid"])
{
map.mapType = MKMapTypeHybrid;
}
else if ([mapStyle isEqualToString:#"Map"])
{
map.mapType = MKMapTypeStandard;
}
else if ([mapStyle isEqualToString:#"Satellite"])
{
map.mapType = MKMapTypeSatellite;
}
[mapStyle release];
}
The app is set up such that you press a button and the InAppSettingsKit is initialised, within this I change the setting for the map type to be displayed and go back to the main screen in my app. At this point the map seems to update correctly and there are no issues. The issue occurs when I try to re-launch the InAppSettingsKit to change the map type again.
Does anyone know if it's my code that's the issue, if so how can I go about fixing it?
just remove the code-line: [mapStyle release]
the stringForKey: will return an autoreleased NSString. So your code is not responsible for releasing. It works fine in the first iteration because the first release call will dealloc that string but the NSUserDefaults still has a pointer to that String but is not used. In the second iteration you get that pointer and try to call isEqualToString on that dealloc-ed object wich will cause the BAD_ACCESS
Reposting with more concise and focused question after original question went unanswered. Also adding more insight into the problem after another day of research:
In my app delegate (didFinishLaunching), I set up a callEventHandler on CTCallCenter.
The idea is that when a callState changes, I post a notification with a userInfo dict
containing the call.callState. In my view, I observe this notification, and when the
userInfo dict contains a value of CTCallDisconnected, I want to unhide a view.
The problem I'm having is that the unhiding aspect is taking, almost consistenly, ~ 7 seconds.
Everything else is working fine, and I know this because I NSLog before and after the unhiding,
and those logs appear immediately, but the darned view still lags for 7 seconds.
Here's my code:
appDidFinishLaunching:
self.callCenter = [[CTCallCenter alloc] init];
self.callCenter.callEventHandler = ^(CTCall* call) {
// anounce that we've had a state change in our call center
NSDictionary *dict = [NSDictionary dictionaryWithObject:call.callState forKey:#"callState"];
[[NSNotificationCenter defaultCenter] postNotificationName:#"CTCallStateDidChange" object:self userInfo:dict];
};
I then listen for this notification when a user taps a button that dials a phone number:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(ctCallStateDidChange:) name:#"CTCallStateDidChange" object:nil];
Then, in ctCallStateDidChange:
- (void)ctCallStateDidChange:(NSNotification *)notification
{
NSLog(#"121");
NSString *callInfo = [[notification userInfo] objectForKey:#"callState"];
if ([callInfo isEqualToString:CTCallStateDisconnected]) {
NSLog(#"before show");
[self.view viewWithTag:kNONEMERGENCYCALLSAVEDTOLOG_TAG].hidden = NO;
NSLog(#"after show");
}
}
I've tracked the problem down to the if condition in the above code sample:
if ([[userInfo valueForKey:#"userInfo"] valueForKey:#"callState"] == CTCallStateDisconnected) {
If I simply replace that with:
if (1 == 1) {
Then the view appears immediately!
The thing is, those NSLog statements are logging immediately, but the view is
lagging in it's unhiding. How could that condition cause only part of it's block
to execute immediately, and the rest to wait ~ 7 seconds?
Thanks!
Try changing your code to this:
- (void)ctCallStateDidChange:(NSNotification *)notification
{
NSLog(#"121");
NSString *callInfo = [[notification userInfo] objectForKey:#"callState"];
if ([callInfo isEqualToString:CTCallStateDisconnected]) {
NSLog(#"before show");
[self.view viewWithTag:kNONEMERGENCYCALLSAVEDTOLOG_TAG].hidden = NO;
NSLog(#"after show");
}
}
Note:
The parameter is an NSNotification, not an NSDictionary
I would not compare strings with ==
No need to cast the view to change the hidden property
Use NO instead of false
Update: Got an idea: Could you try the following, please, in between the NSLogs?
dispatch_async(dispatch_get_main_queue(), ^{
[self.view viewWithTag:kNONEMERGENCYCALLSAVEDTOLOG_TAG].hidden = NO;
});
Reading the CTCallCenter doc, it seems the callEventHandler is dispatched on "the default priority global dispatch queue", which is not the main queue where all the UI stuff happens.
Looks like there is no problem with your hidden code. If I were you, I would comment out all the code after the call ends, and uncomment them one by one to see what is the problem.
Hm... try to call [yourViewController.view setNeedsDisplay] after you change hidden property. Or avoid hidden, use alpha or addSubview: and removeFromSuperview methods instead.
djibouti33,
Where you put this sentence to listen when a user taps a button that dials a phone number?on WillResignActive function?
this sentence --> [[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(ctCallStateDidChange:) name:#"CTCallStateDidChange" object:nil];
Thanks for your time,
Willy.
Reposting with more concise and focused question after original question went unanswered. Also adding more insight into the problem after another day of research:
In my app delegate (didFinishLaunching), I set up a callEventHandler on CTCallCenter.
The idea is that when a callState changes, I post a notification with a userInfo dict
containing the call.callState. In my view, I observe this notification, and when the
userInfo dict contains a value of CTCallDisconnected, I want to unhide a view.
The problem I'm having is that the unhiding aspect is taking, almost consistenly, ~ 7 seconds.
Everything else is working fine, and I know this because I NSLog before and after the unhiding,
and those logs appear immediately, but the darned view still lags for 7 seconds.
Here's my code:
appDidFinishLaunching:
self.callCenter = [[CTCallCenter alloc] init];
self.callCenter.callEventHandler = ^(CTCall* call) {
// anounce that we've had a state change in our call center
NSDictionary *dict = [NSDictionary dictionaryWithObject:call.callState forKey:#"callState"];
[[NSNotificationCenter defaultCenter] postNotificationName:#"CTCallStateDidChange" object:self userInfo:dict];
};
I then listen for this notification when a user taps a button that dials a phone number:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(ctCallStateDidChange:) name:#"CTCallStateDidChange" object:nil];
Then, in ctCallStateDidChange:
- (void)ctCallStateDidChange:(NSNotification *)notification
{
NSLog(#"121");
NSString *callInfo = [[notification userInfo] objectForKey:#"callState"];
if ([callInfo isEqualToString:CTCallStateDisconnected]) {
NSLog(#"before show");
[self.view viewWithTag:kNONEMERGENCYCALLSAVEDTOLOG_TAG].hidden = NO;
NSLog(#"after show");
}
}
I've tracked the problem down to the if condition in the above code sample:
if ([[userInfo valueForKey:#"userInfo"] valueForKey:#"callState"] == CTCallStateDisconnected) {
If I simply replace that with:
if (1 == 1) {
Then the view appears immediately!
The thing is, those NSLog statements are logging immediately, but the view is
lagging in it's unhiding. How could that condition cause only part of it's block
to execute immediately, and the rest to wait ~ 7 seconds?
Thanks!
Try changing your code to this:
- (void)ctCallStateDidChange:(NSNotification *)notification
{
NSLog(#"121");
NSString *callInfo = [[notification userInfo] objectForKey:#"callState"];
if ([callInfo isEqualToString:CTCallStateDisconnected]) {
NSLog(#"before show");
[self.view viewWithTag:kNONEMERGENCYCALLSAVEDTOLOG_TAG].hidden = NO;
NSLog(#"after show");
}
}
Note:
The parameter is an NSNotification, not an NSDictionary
I would not compare strings with ==
No need to cast the view to change the hidden property
Use NO instead of false
Update: Got an idea: Could you try the following, please, in between the NSLogs?
dispatch_async(dispatch_get_main_queue(), ^{
[self.view viewWithTag:kNONEMERGENCYCALLSAVEDTOLOG_TAG].hidden = NO;
});
Reading the CTCallCenter doc, it seems the callEventHandler is dispatched on "the default priority global dispatch queue", which is not the main queue where all the UI stuff happens.
Looks like there is no problem with your hidden code. If I were you, I would comment out all the code after the call ends, and uncomment them one by one to see what is the problem.
Hm... try to call [yourViewController.view setNeedsDisplay] after you change hidden property. Or avoid hidden, use alpha or addSubview: and removeFromSuperview methods instead.
djibouti33,
Where you put this sentence to listen when a user taps a button that dials a phone number?on WillResignActive function?
this sentence --> [[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(ctCallStateDidChange:) name:#"CTCallStateDidChange" object:nil];
Thanks for your time,
Willy.