Using standard Apple translations for Alert button? - ios

Is this true? When you instantiate a UIAlertButton, you have to pass it an explicit title for the Cancel button, like so:
UIAlertView *av =
[[UIAlertView alloc]
initWithTitle:#"Error"
message:err.localizedDescription
delegate:nil
cancelButtonTitle:#"Cancel"
otherButtonTitles:nil];
That means that if you want a localized app (which of course you do), you have to localize the Cancel string too, even though Apple has obviously got a canonical translation already. Am I really forced to write something like this to handle it (or is this even OK)?
NSBundle* uikitBundle = [NSBundle bundleForClass:[UIButton class]];
UIAlertView *av =
[[UIAlertView alloc]
initWithTitle:NSLocalizedString(#"Error", #"Title for Alert box when error occurs")
message:err.localizedDescription
delegate:nil
cancelButtonTitle:NSLocalizedStringFromTableInBundle(#"Cancel", #"Localizable", uikitBundle, nil)
otherButtonTitles:nil];
This looks horrible to me, but the idea that I have to maintain my own translations of words mandated by Apple's HIG (like "Cancel" or "OK") seems equally absurd.

As you expect, that's not recommended as your code introduces an undocumented, unsupported dependency which could break your app if a future iOS update comes along that changes how Apple localizes their UIButton (not very likely, but who knows).
Really, "OK" and "Cancel" are not difficult things to translate. If you don't wish a translator to re-localize these for you as part of your app's localization work then you could retrieve these yourself from iOS (using your code) and copy the translation to your .strings file, so that you'll have a reliable copy of the translation from now on!

Related

Associated objects inUIAlertView in iOS

Associated objects are used for creating property in iOS and some cunning changes in iOS. Would anyone care to explain how is this doing any of these?
- (IBAction)doSomething:(id)sender {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Alert" message:nil
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
objc_setAssociatedObject(alert, &kRepresentedObject,
sender,
OBJC_ASSOCIATION_RETAIN_NONATOMIC);
[alert show];
}
- (void)alertView:(UIAlertView *)alertView
clickedButtonAtIndex:(NSInteger)buttonIndex {
UIButton *sender = objc_getAssociatedObject(alertView,
&kRepresentedObject);
self.buttonLabel.text = [[sender titleLabel] text];
}
The apple's definition does not help me understand either. "Sets an associated value for a given object using a given key and association policy."
As per my knowledge you can that we can add new property at runtime in existing class object.It allow objects to associate arbitrary values for keys at runtime.
Associated Objects—or Associative References, as they were originally
known—are a feature of the Objective-C 2.0 runtime, introduced in OS X
Snow Leopard (available in iOS 4). The term refers to the following
three C functions declared in , which allow objects to
associate arbitrary values for keys at runtime:
objc_setAssociatedObject
objc_getAssociatedObject
objc_removeAssociatedObjects
Why we use this ? as its allow us to add custom property to existing class and can utilize where it required and after that we will remove that property at runtime.
As per your Usage here you can say that there is no sender property in UIAlertView class and you haven't rights to change UIAlertView so by the use of associateObject you can add run time property which you needed while alert delegate will call.
objc_setAssociatedObject(alert, &kRepresentedObject,
sender,
OBJC_ASSOCIATION_RETAIN_NONATOMIC);
This will add it runtime and we can also remove it after its usage at runtime.
You can find more detail on this link : AssociatedObject
Hope this will helps to understand AssociatedObject concept at runtime.

localize UIAlertView & offline notifications in project with no storyboard

I'm working on a project that doesn't have any storyboards. So I can't use "base internationalization"
I've created localizable.string then turned on localization for each required languages.
Then tried doing
- UIAlertView * messageBox = [[UIAlertView alloc] initWithTitle:
NSLocalizedString(#"title", nil)
message: NSLocalizedString(#"msg", nil)
delegate: self
cancelButtonTitle: NSLocalizedString(#"ok", nil)
otherButtonTitles: nil];
And for notification i'm using nsdictionary and store notifications as:
- [alertMessages setObject:NSLocalizedString(#"test_notification", nil)
forKey:#"6"];
but it doesn't do any changes when changing language of the device. Is there any step i'm missing or if any better method is there? any working references would be greatly appreciated!
SOLUTION 1:
Do the following steps.
Clean Project
Delete App From Device/Simulator
If Simulator - Reset it.
Delete Derived Files (XCode > Window > Projects > Derived Data Delete)
Install App, and it will work.
I think its simply Cache Issue.
SOLUTION 2:
NSLocalizedString uses Localizable.strings file by default. Change your File.strings name and try again.
NSLocalizedStringFromTable(#"Message", #"File");

productsRequest seems to return empty set for one app while non-empty for another

dear all:
I inherited a program that can be compiled to two iOS apps, one for traditional Chinese and one for simplified Chinese.
The apps have inAppPurchase and worked fine in the last version.
Then this year, I revised the program without changing the inAppPurchase process.
But after the submissions, apple rejected the traditional Chinese app saying that it would be stuck in the inAppPurchase page.
But the simplified Chinese app was approved.
I then traced the program to the following code.
It seems that my apps always get the empty response set for productsRequest.
However, I must say that I did not do sandbox testing since I don't know how.
I am not sure why it worked fine in last year's version but failed in this year's.
Is there change to some default assumption on bundle id, application id, .. etc.
I checked all my program and could not find where I can set up the app id for the productsRequest statement.
My program is just standard as follows.
Any help will be appreciated.
// determine whether payments can be made
if (![SKPaymentQueue canMakePayments]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"warning" message:#"ineffective" delegate:nil cancelButtonTitle:#"OK?" otherButtonTitles:nil];
[alert show];
}
else {
[buttonPrice setTitle:#"waiting for price" forState:UIControlStateNormal];
[buttonPriceAll setTitle:#"waiting for price" forState:UIControlStateNormal];
// Request product data
productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObjects:bookProductIds[0], bookProductIds[purchaseVolume-1], nil]];
productsRequest.delegate = self;
[productsRequest start];
[self operationStarted];
}

iOS 7.1 app is crashing in showing alert view

I have used alertview many times but currently i have an issue. My app is working in all the version except it is crashing in iOS 7.1. Below is the log message.
[_UIBarBackgroundCustomImageContainer image]: message sent to deallocated instance 0x13b88840
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Title" #"Test" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
I don't understand why it is only crashing in iOS 7.1
Are you sure to be on main thread ?
You can test it like this : [NSThread isMainThread];
As requested by the OP I have just moved my comments to be an answer.
There are a few issues wrong with the following line:
[[UIAlertView alloc]initWithTitle:#"TestTitle" #"Test" delegate:self cancelButtonTitle:kActionOk otherButtonTitles:nil, nil];
just replace it with
[[UIAlertView alloc]initWithTitle:#"TestTitle" message:#"Test" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
As for what the issues are with that line, they are:
1) The method initWithTitle:delegate:cancelButtonTitle:otherButtonTitles: isn't a real instance method for UIAlertView so it shouldn't work in any iOS where as you have said it does work in iOS versions prior to 7. The method that you should be using is initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles: notice the extra parameter message: this is the main issue and should create a compiler error and if it got passed that should throw a runtime error of unrecognised selector.
2) The second is is that you have two nils being passed in for the last parameter for otherButtonTitle:, this parameter is nil terminated so as soon as it sees nil it will end what can be passed into that parameter so the second nil is pointless and never seen. This also may create a compiler error but would be in the shadows of the first issue (1)
For more information in regards to UIAlertView please the Apple Documentation on UIAlertView

Observe NSLog messages in Xcode

I'm debugging a 3rd party SDK which puts lot of useful information into the console.
I used to display some messages in tooltips (for our tester), which I'm receiving from SDK delegate.
But delegate methods don't include many details and sometimes it turns helpless, otherwise console includes much more helpful information (especially if the SDK's log level is set to DEBUG_ALL or something like that).
So, my question - is it possible to observe NSLog messages and to be notified in some way when they are printed to console? Of course I would like to have string message as a parameter?
I would like to display it on device/simulator screen, so that the tester doesn't have to run XCode or view the device's console.
I'm using iConsole for the same purpose. It's quite useful.
What SDK? If the SDK supports CocoaLumberjack, then I suggest installing that, and configuring the loggers to do what you want -- even route somewhere else.
CocoaLumberjack gives you a lot of power and configurability when it comes to logging.
If your SDK uses NSLog to print the details in the console, then you can use macros to redefine the NSLog.
#define NSLog(FORMAT, ...) ShowLogInAlert(FORMAT);
void ShowLogInAlert(NSString *format, ...){
//show the log in the alert here.
va_list ap;
va_start (ap, format);
format = [format stringByAppendingString:#"\n"];
NSString *msg = [[NSString alloc] initWithFormat:[NSString stringWithFormat:#"%#",format] arguments:ap];
// NSLog(#"%#", msg);
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:msg message:#"" delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alert show];
va_end (ap);
}

Resources