Launch arguments (NSDoubleLocalizedStrings, NSShowNonLocalizedStrings) don't work - ios

I'm trying to use the launch arguments such as NSDoubleLocalizedStrings and NSShowNonLocalizedStrings to test localization in my Objective-C project. For some reason I couldn't get neither of them to work. I tried to set both arguments on launch and options in scheme settings:
Also I've checked NSUserDefaults keys and they both are YES. But the arguments still don't work.
To make sure it's not an XCode bug I've created absolutely new single-view project on Objective-C and Swift with one label on the view. Both projects had empty Localizable.strings files and the code in these projects was the following:
#interface ViewController ()
#property (nonatomic, strong) IBOutlet UILabel *label;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.label.text = NSLocalizedString(#"Some text", nil);
}
#end
and
class ViewController: UIViewController {
#IBOutlet var label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
label.text = NSLocalizedString("Some text", comment: "")
}
}
The result looked like this:
So here are the questions:
1) What could be the reasons launch arguments don't work in my Objective-C project?
2) Why NSDoubleLocalizedStrings argument doesn't work in Swift?

Spending couple days on investigation and deleting gradually all the files and frameworks from my Objective-C project I figured out that the reason was in AFNetworking library. The issue is described here. If you want to use AFNetworking in your project and debug the localization issues you can add such lines to application: didFinishLaunchingWithOptions:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:YES forKey:#"NSDoubleLocalizedStrings"];
[defaults setBool:YES forKey:#"NSShowNonLocalizedStrings"];
[defaults synchronize];
On second launch of the app you’ll see those launch arguments working.
The answer for the second question about why NSDoubleLocalizedStrings argument doesn't work in Swift is that it's probably a bug in XCode. It might be connected with macros which are not supported in Swift, and NSLocalizedString is actually a macro. Therefore I've created a bug on radar.

Related

activating function in swift file from an objective-C file

I'm working on an app (in Xcode with storyboard and written in swift) but for a Reward ad (from AdMob) I had to use an Objective-C file to handle that certain view controller in objective-C. As a reward for clicking the ad I wanted to give the person coins in the app but the label which displays the coins is in another view controller in swift (both linked with a navigation controller).
After searching the internet I couldn't find a solution, but here is what I have already:
In the swift file for rewarding the user (named "RewardingUserAds.swift"):
import Foundation
#objc class RewardingUserAds: NSObject {
#objc func call_increase_score() {
print("Rewarding user ads is called.")
Winkel_ViewController().increase_score()
}
}
I found on internet you should use NSObject and add '#objc' that's why I have it there now, this calls a function in 'Winkel_Viewcontroller' which increases the variable of the coin.
A snippet from my objective-C file ('Ad_ViewController.m') ( I have not changed the 'Ad_ViewController.h'file) which gives the error:
#import "Ad_ViewController.h"
#import "Troeven-Bridging-Header.h"
#import "Troeven-Swift.h"
#import GoogleMobileAds;
#interface Ad_ViewController ()
#property(nonatomic, strong) GADRewardedAd *rewardedAd;
#end
#implementation Ad_ViewController
...
-(void) rewardedAd:(GADRewardedAd *)rewardedAd userDidEarnReward:(GADAdReward *)reward {
RewardingUserAds *obj = [RewardingUserAds alloc];
[obj call_increase_score];
NSLog(#"User earned reward!");
}
The problem occurs in the second to last line ([obj call_increase_score] -> with ‘call_increase_score’ the function in the swift file it should activate/call). The error (red) is labeled as: 'No visible #interface for 'RewardingUserAds' declares the selector 'call_increase_score'' and that whole line stays white collored
I have a bridgehead between them named : 'Troeven-Bridging-Header.h' which is empty. I got little to no experience in objective-C which makes it harder for me to find the mistake I make. Can someone help?
I found the solution:
had to remove '#import "Troeven-Bridging-Header.h"'
and change:
RewardingUserAds *obj = [RewardingUserAds alloc];
[obj call_increase_score];
to:
RewardingUserAds *client = [RewardingUserAds new];
[client call_increase_score];
-> I don't really know why it works, but it works

iOS - How to change app language in storyboard strings without programmatically?

I am developing an app in which an user can change language at any time. I have done this in a demo app. It is working fine. But there is an issue with it, that is for localisation app I have to do following 2 tasks.
Step 1: I have to create IB outlet for every object (i.e in every class ==> buttons, labels, textFields, TextViews).
Step 2 : I have to set language for every objects for example : [myButton setTitle:[Language get:#"Language Test" alter:nil] forState:UIControlStateNormal];
OR myLabel.text=LocalizedString(#"Forgot Passoword");
I have a project which has many screens, around 60 screens (in storyboard). Unfortunately in this project there is no IB outlet for all objects. Strings are set in via storyboard. Now I have to make support this app in another language also (current language english )name Arabic language.
Conclusion : If I do above 2 steps (create IB outlet for every objects
, then write code in .m file for every object). It will take very much
time. Is there any other better options please ? I want do not want
create IB outlet and coding in .m file. Any suggestion will be great!!
What I have done till now :
Follow these tuttorials :
(1). How to force NSLocalizedString to use a specific language
and sample projects :
(1). https://github.com/tonisalae/TSLanguageManager
(2). https://github.com/object2dot0/Advance-Localization-in-ios-apps
My code :
example calls:
[Language setLanguage:#"it"];
OR
[Language setLanguage:#"de"];
+(void)initialize {
NSUserDefaults* defs = [NSUserDefaults standardUserDefaults];
NSArray* languages = [defs objectForKey:#"AppleLanguages"];
NSString *current = [languages objectAtIndex:0];
[self setLanguage:current];
}
+(void)setLanguage:(NSString *)l {
NSLog(#"\n\n\t ***** Hint Lang. selected by user: %# ****\n\n", l);
NSString *path = [[ NSBundle mainBundle ] pathForResource:l ofType:#"lproj" ];
bundle = [NSBundle bundleWithPath:path];
}
+(NSString *)get:(NSString *)key alter:(NSString *)alternate {
return [bundle localizedStringForKey:key value:alternate table:nil];
}
You can achieve this using User Defined Runtime Attribute. Follow this link for detail.

Zendesk iOS SDK multilanguage support

are there anyone familiar with Zendesk iOS SDK? I am trying to integrate this SDK to my iOS app and the integration was done really issue, the only problem I have currently is multi language support. My app have a view when you can change the language on the fly using this code:
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:lang forKey:#"lang"];
[defaults synchronize];
It changes the lang in app successfully, but it is not changing for Zendesk View. How to change language for Zendesk Chat View also?
Thanks!
The answer would be to override ZDCChatStrings bundle with your custom class, and whenever it will call for localizedStringForKey method return translation from your internal translations list. Code:
#import "NSBundle+Language.h"
#import <UIKit/UIKit.h>
#import <objc/runtime.h>
#interface CustomBundle : NSBundle
#end
#implementation CustomBundle
- (NSString *)localizedStringForKey:(NSString *)key value:(NSString *)value
table:(NSString *)tableName
{
return NSLocalizedStringFromTable(key, g_appDelegate.lang, key);
}
#end
#implementation NSBundle (Language)
+ (void)registerBundle
{
static dispatch_once_t tempToken;
dispatch_once(&tempToken, ^{
NSBundle *bundle = [NSBundle bundleWithPath: [[NSBundle mainBundle] pathForResource: #"ZDCChatStrings" ofType: #"bundle"]];
object_setClass(bundle, [CustomBundle class]);
});
}
#end
Initialize somewhere in your AppDelegate:
[NSBundle registerBundle];
You need to change the Localizable string in ZenDesk Api Files not your custom file. But this is Internationalization. Language change according to your phone Language.
Goto Your Projects Supporting Files. There are ZDCChatStrings.bundle and ZDCChat.bundle are there. Open ZDCChatStrings.bundle. In this you see Localizable.strings for All languages.
Right click on strings files which you want and Open as Ascii Property List. Change strings accordingly.

UITextField to hold/save value as I start typing without having me to hit the "done/save" button

I am looking for a code that allows me to have my textfield hold/save value as I start typing. The issue I am facing is that UITextField sets to the previously stored value if I choose to exit the view controller without hitting the done key on the keyboard. I want the UITextField to hold/save the value as I start typing so it holds the value even if I exit the view controller.
you can save the textField Value in NSUserDefault as you type. And retrieve from it.
in ViewDidLoad
-(Void) ViewDidLoad{
NSString *textFieldValue = [[NSUserDefaults standardUserDefaults] valueForKey:#"fieldValue"];
myTextField.text = textFieldValue;
myTextField.delegate = self;
}
//UITextField delegate
- (void)textFieldDidEndEditing:(UITextField *)textField{
[NSUserDefaults standardUserDefaults] setValue:myTextField.text forKey:#"fieldValue"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
I have not tried this specifically on iOS, but I was able to get similar functionality in a Mac app that I was working on by implementing the NSTextFieldDelegate.
#interface AppDelegate : NSObject <NSApplicationDelegate, NSTextFieldDelegate>
Inside of the AppDelegate.m I implemented this part of the protocol:
-(void)controlTextDidChange:(NSNotification *)obj{
This function would run every time text was entered into the field.
Hope this helps.

InAppSettingsKit IASKCustomViewSpecifier

i am having trouble trying to call values from two IASKCustomViewSpecifier in the InAppSettingsKit. I tried changing the Key value from customCell but it seams to then change the dimensions of the customCell text field. They appear fine in the setting bundle in the simulator and the device how ever i just cant work out how to call each one independently
eg
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
oneString = [defaults objectForKey:#"customCellOne"];
twoString = [defaults objectForKey:#"customCellTwo"];
Plist File contents
Item 0
-Title: One
-Type: IASKCustomViewSpecifier
-Key: customCell
-DefaultValue: Test Example String here
Item 1
-Title: Two
-Type: IASKCustomViewSpecifier
-Key: customCell
-DefaultValue: Test Example String here
Any help would be great,
thanks,
I think my answer over at Github solved the issue: https://github.com/futuretap/InAppSettingsKit/issues/closed#issue/43
I have figured out how to set different IASKCustomViewSpecifier cells. I changed the keys on them and they are all looking good now but the problem i am facing now is that when i get out of the settings bundle it doesnt save the changes I made on the IASKCustomViewSpecifier cell's all the others it works fine. I think it has something to do with this code
- (void)textViewDidChange:(UITextView *)textView {
[[NSUserDefaults standardUserDefaults] setObject:textView.text forKey:#"customCell"];
[[NSNotificationCenter defaultCenter] postNotificationName:kIASKAppSettingChanged object:#"customCell"];
}
Now that all the keys are different not sure what to do with the forKey#"customCell".

Resources