How to get the user "Name" using Swift? - ios

I'm trying to create an app that when the user open the first view it will provide with a nice greeting.
For example depending on the time of the day it would say "Good Morning/Afternoon/Night - User Name".
How do I get the name that the User has on his iPhone? I need the name that is on Settings > General > About _ NAME?

UIDevice.current.name gives back device name, so you should parse the string trying to distinguish between device type (iPhone, iPad and so on...) and the name.
But is bit tricky: suppose You have "My iPhone" , "Mary's iPhone" or "IPhone of John" or similar.
Soem people try to use regular expressions, but result is not so good, specially for non-english names.
see at:
http://stackoverflow.com/questions/8261961/better-way-to-get-the-users-name-from-device

Swift 2
UIDevice.currentDevice().name
Swift 3
UIDevice.current.name

let benutzerGeraeteName = UIDevice.current.name
print("Hallo, \(benutzerGeraeteName)")

Related

can we release same app with 1 or 2 different functionality for 2 different countries?

I want to release my app in only 2 country and i want to do 2 different functionality for both country.
For example.
ViewController1 functionality is different in Jamaica.
ViewController1 functionality is different in Kenya.
Different functionality means content is different, or input forms are different.
Is it possible? if yes then please refer some document.
Thanks in advance
You should have a screen that allows user to select their country, after that, store selected country in our app (by UserDefault or Keychain, etc...).
Based on the selected country then you can switch logic/layout to adapt the requirement above
some notes about App Store:
1) language should / must be selected by user on Prefs, NOT in Apps.
Chances Apple will refuse apps not following above logic.
2) You could test current language / Zone using code (see below for language)
BUT I think Apple can refuse as you use a different behaviour
3) if really you need it, You can load a different controller using Storyboards (I suggest using different storyboards AND lod them at runtime using segues and "*.soryboard" as in:
func ViewControllerFromStoryboardWith( name: String ) -> UIViewController {
// we use an identifier equal to filename for now.
let storyboard = UIStoryboard(name: name, bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: name) as UIViewController
return vc
}
// test lang:
func currHWLanguage()->String{
let defs : UserDefaults = UserDefaults.standard
let languages : NSArray = defs.object(forKey: "AppleLanguages") as! NSArray
let current = languages[0] as! String
// since 9.0 we get en-US etc.. so cut to 2:
let result = (current as NSString).substring(to: 2)
#if DEBUG
// force to IT as a bug in simulator
// return "IT"
#endif
return result.uppercased()
//NSLog("%#", current)
}
This is a problem many applications are trying to solve. Basically, you have the following options:
Let the user choose. This is the safest option if one application contains two different configuration.
Try to detect location of the user. Language/Locale is unsafe because many people will have English (or different) locale set up. Very unsafe. You shouldn't ask for GPS location for this. The safest option is to create a server request and check the location using the IP address. A bit complicated and won't work if a VPN is used (e.g. antivirus apps create VPNs).
Create two different apps. In the end, this is the best option. Add a second application target to your project and release two separate apps with separate configuration.

Localized Strings are always in English in UITests (Snapshot)

I'm pretty new to Fastlane and love the idea of Snapshot, but I got a little problem.
When I'm trying to create a new set of screenshots I'm facing the problem that the UITests don't use the correct localized Strings to fetch the UI elements.
My current state is based on this Stack entries: XCode 7 UITests with localized UI
The localization method:
func localizedString(_ key: String) -> String {
let uiTestBundle = Bundle(for: MyUITests.self)
return NSLocalizedString(key, bundle: uiTestBundle, comment: "")
}
Trying to perform a tap that way:
app.navigationBars[localizedString("key_1")].buttons[localizedString("key_2")].tap()
The error I get is the following:
No matches found for "Rolling stone" NavigationBar
Rolling stone is the Base/English Localization of the key, but there should be a German one. So for any reason the UITest always picks the English Localization.
Does anybody have an idea how to troubleshoot this? I checked the SnapshotHelper and it passes a "de-DE" as the language, so that's not the point.
But I just can't find the Bug :(
Localization
This link here should be sufficient enough to solve your problem. You simple pick the correct language option during the test tab under edit scheme option.

MSMessage message.senderParticipantIdentifier.uuidString not converting to name

I'm in the final stages of an iMessage extension and have one last lingering issue that I thought was a problem with the simulator but now I have the app working on test devices it's not resolved itself.
When I create a message caption the following code works to change the partipipantIdentifer into their name:
layout.caption = "$\(conversation.localParticipantIdentifier.uuidString)"
However when viewing the message at the recipient's end this code:
self.titleLabel.text = "$\(self.message!.senderParticipantIdentifier.uuidString) sent:"
doesn't display the senders name it just displays their UUID string. What am I missing to convert the sender UUID to their name? Does anyone know if this is this possible or not?
Thanks
The "$(participantIdentifier)" syntax only works within MSMessageLayout. When setting the UILabel.text you can only get the uuid.

Reading Localized String

I have an app that I'd like to be able to get the localized string for a certain key, regardless of what my current localization on the iPhone is.
I have the following in an en.lproj localized strings file
"Black" = "Black";
In the es.lproj localized strings file I have
"Black" = "Negro";
So what I would like to do is get the Spanish string when my phone is in the US and set up accordingly
I'm using the following code:
let bpath:String = NSBundle.mainBundle().pathForResource("es", ofType: "lproj")! as String
let bundle = NSBundle(path: bpath as String)
let thisWord="Black"
let ourWord=NSLocalizedString(thisWord, bundle: bundle!, comment: "")
I'm expecting to get "Negro" in the value for ourWord, but I always get "Black"
Am I missing something obvious?
Localization settings apply based on the settings you set in your phone not the location. If your phone is setup to display an Application Language of Spanish then you will see the appropriate string. To configure Application Language settings so you can test this you need to edit your Scheme.
In Xcode 7.1+ Goto Product > Scheme > Edit Scheme
Then change the Application Language and/or Region to simulate what a user would see who has an iPhone configured for a region or language other than English in the US.
(Switch it to Spanish and then debug the app on the device again to see the updated localized strings).
Actually my code works fine. I had made the stupid mistake of not setting the "Localizable.strings" file as localized.
Feeling pretty dumb right now

How do I find correct translations of "reserved names" used in iOS

I have an app that needs the Location Service to be active and authorized and
if not so a message should appear to the user asking them to enabled and authorize said serivce.
The thing is that my app is translated into 26 different languages and it is important that the localized version of "Location services" in the iOS-settings menu matches the one in my translated popup.
So, is there some sort of Phrasebook for the iOS where one can see which translations being used by apple for different phrases in different languages?
e.g:
en: Location Services
sv: Platstjänster
de: Ortungsdienste
In this post, there is a solution for AppKit.I think it is similar with iOS. Try this
NSBundle *appBundle = [ NSBundle bundleWithIdentifier:#"com.apple.UIKit" ];
NSString *localSetFont = [ appBundle localizedStringForKey: STRING_YOU_WANT_TO_SEEK value: nil table: STRING_IN_WHICH_TABLE];

Resources