XCUIApplication *app = [[XCUIApplication alloc] init];
[app.buttons[#"login"] tap];
See as above, in tests.m file, I want get login button by string "login", But, my app support multi-languages. How to get current language of my app.
The method below is the method how I get current Language. User set language in setting view of my app. I store current Language in NSUserDefaults.
+ (NSString*)currentLanguage
{
NSUserDefaults *lanUser = [NSUserDefaults standardUserDefaults];
NSString *currentLanguage = [lanUser valueForKey:kCNOGASingularLanguage];
if([currentLanguage length]==0)
currentLanguage = kApplicationLanguageEnglish;
return currentLanguage;
}
Storing the user's language in your app's UserDefaults and then accessing that from your UITest won't work. Your app and the UITest are running as separate processes, which means that your UITest cannot access your app's UserDefaults
There is a simple solution: To become independent from the users language you can set the accessibilityIdentifier on your UIButton and then access the button via the accessibilityIdentifier:
In your app:
button.accessibilityIdentifier = #"login";
In your UITest:
[app.buttons[#"login"] tap];
The accessibilityIdentifier is never displayed and VoiceOver also does not read it, so it does not have to be localized. Just make sure you are using accessibilityIdentifier and not accessibilityLabel. Because accessibilityLabel will be read by VoiceOver for handicapped users and should be localized.
ALTERNATIVE
If you cannot use accessibilityIdentifier to query your UI elements you could use your app's LocalizableStrings file in your UITests to get the localized button title (in this case):
First add your Localizable.strings file to your UITest target, then access the file via the UITest bundle to get the localized strings (I use a little helper method for that):
func localized(_ key: String) -> String {
let uiTestBundle = Bundle(for: AClassFromYourUITests.self)
return NSLocalizedString(key, bundle: uiTestBundle, comment: "")
}
I wrote a little blog post about this a while back with some more details.
Related
I'm having trouble accessing localized resources from my Intents extension.
I've got a couple of JSON files that are localized into different languages in a framework that's shared between my app and the extension. When I access them though my app via path(forResource:ofType), it accesses the proper localized version, but if I call it from the extension in the Shortcuts app, then it returns the English, even if both the app itself and the Shortcuts app are both set to another language.
The only way I can get the correct language to appear is if I change the device language - changing it just for the app or for Shortcuts doesn't have an effect.
I'm building with the iOS 14 SDK, but the same behavior occurs on both iOS 13 and 14.
Firstly, please check Target Membership of Localizable file, make sure it was checked your extension.
Secondly, save your app language in UserDefaults which have AppGroups.
static func setLanguageExtension(_ language: String, forKey: String) {
if let userDefault = UserDefaults(suiteName: "APP_LANGUAGE") {
userDefault.setValue(language, forKey: forKey)
}
}
static func getLanguageExtension(key: String) -> String? {
if let userDefault = UserDefaults(suiteName: "APP_LANGUAGE") {
return userDefault.string(forKey: key)
} else {
return "en-US"
}
}
I am developing an iOS application which supports English and Arabic. User can change the application language from inside the app.
When user changes the language I am setting it like ,
//change app language
UserDefaults.standard.set([language], forKey: "AppleLanguages")
currentLanguage = language
UserDefaults.standard.synchronize()
//current language updating
var currentLanguage : String{
get{
if let selectedLanguage = UserDefaults.standard.string(forKey: "selectedLanguage"){
return selectedLanguage
}else{
let language = Locale.preferredLanguages[0]
if language.hasPrefix("ar"){
return SupportedLanguage.ar.rawValue
}else{
return SupportedLanguage.en.rawValue
}
}
}
set{
UserDefaults.standard.setValue(newValue, forKey: "selectedLanguage")
}
}
In this way, App is not exiting. Just reloading the root view controller
The issue I am facing is, when I change the application language like this, the privacy alerts like “..requesting permission for using Location”, “… would ,like to use Photo album” etc are not showing in the selected language. I have created InfoPlist.string files for English and Arabic and added like
NSCameraUsageDescription = ".... would like to access Camera";
NSLocationAlwaysAndWhenInUseUsageDescription = ".... wants to use your current location for better usability";
Still its not showing. Also I tried deleting, cleaning app, deleting derived data.
Any idea why its happening?
Changing AppleLanguages key needs app to be restarted so new localization applied , you can try to use NSLocalizedString with tableName or change current bundle you read from , but system localization won't be changed until app restarted
I am creating an app that for now I would like to offer in English and German. I checked the Base localization mark in the project configuration and added German. I left English as the development language.
Then I created a file Translation.plist which basically consists of dictionaries that I call categories, e.g. I have one dictionary for button texts, label texts etc. Each of the category dictionaries again consists of dictionaries that contain two Strings: value and comment. Translation.plist is localized via XCode. The folders Base.lproj, en.lproj and de.lproj exist and contain a copy of the plist-file as expected.
Then I created a class Translator.swift that is supposed to load the Translation.plist file as an NSDictionary depending on the user's preferred locale. The code looks like this:
func relevantDictionary(category: String) -> NSDictionary {
let preferredLocale = Bundle.main.preferredLocalizations.first ?? "Base"
NSLog("User's preferred locale is \(preferredLocale)")
guard let url = Bundle.main.url(forResource: "Translation", withExtension: "plist") else {
fatalError("Could not find Translation.plist")
}
NSLog("Using \(url.absoluteURL) for translation")
guard let root = NSDictionary(contentsOf: url) else {
fatalError("Could not find dictionary for category (locale=\(preferredLocale)")
}
guard let relevant = root.value(forKey: category) as? NSDictionary else {
fatalError("Could not create dictionary from Translation.plist")
}
return relevant
}
Then I created a String extension that uses the Translator as follows:
func localize(category: String) -> String {
return Translator.instance.translate(category: category, string: self)
}
With this I call the Translator by "yes".localize("button"). In English I would expect "Yes", in German I would expect "Ja". The log says the following:
2017-07-05 08:45:24.728 myApp[13598:35048360] User's preferred locale is de_DE
2017-07-05 08:45:24.728 myApp[13598:35048360] Using file:///Users/me/Library/Developer/CoreSimulator/Devices/A39D3318-943D-4EFE-BB97-5C2218279132/data/Containers/Bundle/Application/4614E696-B52E-4C30-BBE8-3C76F6392413/myApp.app/Base.lproj/Translation.plist for translation
I wonder why this happens and what I have missed. I would have expected that de.lproj/Translation.plist is loaded instead of Base.lproj/Translation.plist.
Any help is highly appreciated.
You can do it with single .plist file. You don't need to create different .plist files for it.
Firstly, Add English and German countries with locale in Project -> info
https://i.stack.imgur.com/M4QIY.png
Once, you added countries with locale in Project -> info then add localizable.strings file in your bundle.
https://i.stack.imgur.com/lnjgL.png
At the end, just add country's locale in your language support class.
NSArray* languages = #[#"en", #"de"];`enter code here`
NSString *current = [languages objectAtIndex:0];
[self setLanguage:current];
Hope it would help you.
I'm trying to add TodayExtension to my existing app and I want that app and extension will share data through NSUserDefaults.
According to this tutorial http://www.glimsoft.com/06/28/ios-8-today-extension-tutorial/
I did like it say:
Create new target with Today extension
Add new App-Group in Extension and App
Add code to app
Add code to extension
App Code
func updateExtensionData() {
let key = "identityCode"
let valueForStore:Float = 2.0
let extensiontDefaults = NSUserDefaults(suiteName: "group.company.mineSharedDefaults")
extensiontDefaults?.setFloat(valueForStore, forKey: key)
let results = extensiontDefaults?.synchronize()
let checkCode = extensiontDefaults?.objectForKey(key)
print("code for save \(valueForStore) synchronize result \(results) readed code \(checkCode!)")
}
Extension code
private func updateData() {
let key = "identityCode"
let extensionDefaults = NSUserDefaults(suiteName: "group.company.mineSharedDefaults")
let checkCode = extensionDefaults?.floatForKey(key)
print("synchronize result \(resut) readed code \(checkCode!)")
}
Issue
My issue is that extension always read 0 instead of 2. Clearly I'm missing something. There is some major difference between iOS8 and 9 in case of AppGroup and NSUserDefaults? I know that there is but between app an watchOS2.
For being more clear this is keys from App and Extension NSUserDefaults
App Keys
AppleKeyboards
AppleKeyboardsExpanded
AddingEmojiKeybordHandled
AppleLanguages
ApplePasscodeKeyboards
PreferredLanguages
AppleLocale
NSInterfaceStyle
MSVLoggingMasterSwitchEnabledKey
NSLanguages
AppleITunesStoreItemKinds
identityCode
AppleLanguagesDidMigrate
PKEnableStockholmSettings
Extension Keys
AppleKeyboards
AppleKeyboardsExpanded
AddingEmojiKeybordHandled
AppleLanguages
ApplePasscodeKeyboards
PreferredLanguages
AppleLocale
NSInterfaceStyle
MSVLoggingMasterSwitchEnabledKey
NSLanguages
AppleITunesStoreItemKinds
AppleLanguagesDidMigrate
PKEnableStockholmSettings
It's clear that key identityCode is not appearing in extension at all.
Ok I'm not sure what helps but I've done two thing:
rename current AppGroup identifier and create new one with name group.my.entire.bundle.id.of.my.app.sharedData instead of group.appname.sharedData
remove and create new AppGroup identifier
I can use "identityCode" as key.
CThere could be several things!
first; did you allow keychain sharing under "Capabilities" for both your container app and the extension, and did you set the Keychain groups to the same group?
I recommend using this: https://github.com/kefbytes/KeychainWrapper
download it, it's very simple, just add the swiftfile to your project
allow keychain sharing under cabailities,
set your serviceName and accesgroup like this
KeychainWrapper.serviceName = "give it a name in here"
(servicename is neccessary for it to work)
KeychainWrapper.accessGroup = "write your accessgroup name here"
(accesgroup is optional, since you are setting it under Capabilities)
save a value to a key like this
KeychainWrapper.setString("2", forKey: "identityCode"
and retrieve with
KeychainWrapper.stringForKey("identityCode")
Recently, I've been making a simple iOS 8 share extension to understand how the system works. As Apple states in its App Extension Programming Guide:
By default, your containing app and its extensions have no direct access to each other’s containers.
Which means the extension and the containing app do not share data. But in the same page Apple brings a solution:
If you want your containing app and its extensions to be able to share data, use Xcode or the Developer portal to enable app groups for the app and its extensions. Next, register the app group in the portal and specify the app group to use in the containing app.
Then it becomes possible to use NSUserDefaults to share data between the containing app and the extension. This is exactly what I would like to do. But for some reason, it does not work.
In the same page, Apple suggests the standard defaults:
var defaults = NSUserDefaults.standardUserDefaults()
In a WWDC presentation (217), they suggest a common package:
var defaults = NSUserDefaults(suiteName: kDefaultsPackage)
Also, I enabled App Groups for both the containing app target and the extension target, with the same App Group name:
But all this setup is for nothing. I cannot retrieve the data I stored in the containing app, from the extension. It is like two targets are using completely different NSUserDefaults storages.
So,
Is there a solution for this method?
How can I share simple data between the containing app and the share extension? The data is just user credentials for an API.
You should use NSUserDefaults like this:
Save data:
objc
NSUserDefaults *shared = [[NSUserDefaults alloc] initWithSuiteName:#"group.yougroup"];
[shared setObject:object forKey:#"yourkey"];
[shared synchronize];
swift
let defaults = UserDefaults(suiteName: "group.yourgroup")
defaults?.set(5.9, forKey: "yourKey")
Read data:
objc
NSUserDefaults *shared = [[NSUserDefaults alloc] initWithSuiteName:#"group.yougroup"];
id value = [shared valueForKey:#"yourkey"];
NSLog(#"%#",value);
swift
let defaults = UserDefaults(suiteName: "group.yourgroup")
let x = defaults?.double(forKey: "yourKey")
print(x)
This will work fine!
Here is how I did it:
Open your main app target > Capabilities > App Groups set to on
Add a new app group and make sure it is ticked (e.g. group.com.seligmanventures.LightAlarmFree)
Open your watch target (the one with Capabilities tab) > App Groups set to on
Add a new app group and make sure it is ticked (e.g. group.com.seligmanventures.LightAlarmFree - but must be the same name as group above)
Save data to the group as follows:
var defaults = NSUserDefaults(suiteName: "group.com.seligmanventures.LightAlarmFree")
defaults?.setObject("It worked!", forKey: "alarmTime")
defaults?.synchronize()
Retrieve data from the group as follows:
var defaults = NSUserDefaults(suiteName: "group.com.seligmanventures.LightAlarmFree")
defaults?.synchronize()
// Check for null value before setting
if let restoredValue = defaults!.stringForKey("alarmTime") {
myLabel.setText(restoredValue)
}
else {
myLabel.setText("Cannot find value")
}
If you have
group.yourappgroup
use
var defaults = NSUserDefaults(suiteName: "yourappgroup")
This works for me
So apparently it works, only when the group name is used as the suite name for NSUserDefaults.
The documentation says NSUserDefaults.standartUserDefaults() should also work but it does not, and this is probably a bug.
In my scenario I'm sharing data between the parent iOS app and WatchKit. I'm using Xcode 6.3.1, iOS Deployment Target 8.3
var defaults = NSUserDefaults(suiteName: "group.yourappgroup.example")
In your viewDidLoad make sure you synchronize:
override func viewDidLoad() {
super.viewDidLoad()
defaults?.synchronize()
}
Example of a button sending text but of course you can pass whatever:
#IBAction func btnSend(sender: UIButton) {
var aNumber : Int = 0;
aNumber = aNumber + 1
//Pass anything with this line
defaults?.setObject("\(aNumber)", forKey: "userKey")
defaults?.synchronize()
}
Then on the other side make sure app group matches:
var defaults = NSUserDefaults(suiteName: "group.yourappgroup.example")
Then synchronize and cal: (In this case "lblNumber" is an IBOutlet label)
defaults?.synchronize()
var tempVar = defaults!.stringForKey("userKey")!;
lblNumber.setText(tempVar);
Then if you wanted to set something on this side and sync it back then just do the same thing and synchronize and just make sure the stringForKey that you call on the other side is the same:
defaults?.setObject("sending sample text", forKey: "sampleKey")
defaults?.synchronize()
Hope this makes sense
I translated in swift foogry's answer and it works!!
Save data:
let shared = NSUserDefaults(suiteName: "nameOfCreatedGroup")
shared("Saved String 1", forKey: "Key1")
shared("Saved String 2", forKey: "Key2")
Read data:
let shared = NSUserDefaults(suiteName: "nameOfCreatedGroup")!
valueToRead1 = shared("Key1") as? String
valueToRead2 = shared("Key2") as? String
println(valueToRead1) // Saved String 1
println(valueToRead2) // Saved String 2
You may share data by Following below steps:
1) Select your project -> Select Capabilities tab -> Enable App Groups -> Click on '+' -> paste your bundle Id after 'group.'
2) Select your Extension -> Select Capabilities tab -> Enable App Groups -> Click on '+' -> paste your bundle Id after 'group.'
3) Place below code in your main app for which data you want to share:
NSUserDefaults * appGroupData = [[NSUserDefaults alloc]initWithSuiteName:#"group.com.appname"];
NSData * data = [NSKeyedArchiver archivedDataWithRootObject:[self allData]]; // Get my array which I need to share
[appGroupData setObject:data forKey:#"Data"];
[appGroupData synchronize];
4) You may get object in extension:
NSUserDefaults * appGroupData = [[NSUserDefaults alloc] initWithSuiteName:#"group.com.appname"];
NSData * data = [appGroupData dataForKey:#"Data"];
NSArray * arrReceivedData = [NSKeyedUnarchiver unarchiveObjectWithData:data];
You should use NSUserDefaults like this following and make sure
you must have enabled app group in your provisional profile and app group must configure as a green symbol and it should add to your provisional profile & BundleID.
NSUserDefaults *sharedUserDefault = [[NSUserDefaults alloc] initWithSuiteName:#"group.yougroup"];
[sharedUserDefault setObject:object forKey:#"yourkey"];
[sharedUserDefault synchronize];
NSUserDefaults *sharedUserDefault = [[NSUserDefaults alloc] initWithSuiteName:#"group.yougroup"];
sharedUserDefault value = [sharedUserDefault valueForKey:#"yourkey"];