I made a function that extracts a user name from the device name.
The idea is to skip setup steps to allow the user to go straight to play>
I'd like to offer an improvement on Ricky Helegesson's answer. It has the following features;
Here is the code:
NSArray * nameFromDeviceName(NSString * deviceName)
{
NSError * error;
static NSString * expression = (@"^(?:iPhone|phone|iPad|iPod)\\s+(?:de\\s+)?|"
"(\\S+?)(?:['’]?s)?(?:\\s+(?:iPhone|phone|iPad|iPod))?$|"
"(\\S+?)(?:['’]?的)?(?:\\s*(?:iPhone|phone|iPad|iPod))?$|"
"(\\S+)\\s+");
static NSRange RangeNotFound = (NSRange){.location=NSNotFound, .length=0};
NSRegularExpression * regex = [NSRegularExpression regularExpressionWithPattern:expression
options:(NSRegularExpressionCaseInsensitive)
error:&error];
NSMutableArray * name = [NSMutableArray new];
for (NSTextCheckingResult * result in [regex matchesInString:deviceName
options:0
range:NSMakeRange(0, deviceName.length)]) {
for (int i = 1; i < result.numberOfRanges; i++) {
if (! NSEqualRanges([result rangeAtIndex:i], RangeNotFound)) {
[name addObject:[deviceName substringWithRange:[result rangeAtIndex:i]].capitalizedString];
}
}
}
return name;
}
To use this for return a name;
NSString* name = [nameFromDeviceName(UIDevice.currentDevice.name) componentsJoinedByString:@" "];
This is somewhat complex, so I'll explain;
If a name ends in "s" without an apostrophe before "iPad" etc., I don't try to change it because there is not foolproof way of figuring out if the "s" is a part of the name or a pluralisation of the name.
Enjoy!