从设备获取用户名的更好方法是什么? [英] Better way to get the user's name from device?

查看:107
本文介绍了从设备获取用户名的更好方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个从设备名称中提取用户名的功能。

I made a function that extracts a user name from the device name.

想法是跳过设置步骤允许用户在第一次启动应用程序时直接进行游戏

这是一种次优方法,因为我永远不会相信设备名称可以保存用户名。问题是:有什么更好的方法可以做到这一点?

This is a sub-optimal approach, as I can never trust the device name to hold the name of the user. The question is: What is a better way to do this?

我的下面的函数得到了正确的名字......

My function below gets the right name ...


  • ...如果设备的默认名称未更改(Sanna的iPod)

  • ... in English,

  • ...法语和类似地方(iPod de Sanna)

  • ...瑞典语及类似词语(Sannas iPod )如果名称不以S结尾(Johannes iPod=>返回Johanne,但应该返回Johannes是正确的,因为名称本身以S结尾。)

  • ... if the default name of the device has not changed ("Sanna's iPod")
  • ... in English,
  • ... in French and similar ("iPod de Sanna")
  • ... in Swedish and similar ("Sannas iPod") if the name does not end with S ("Johannes iPod" => returns "Johanne" but should rather return "Johannes" to be correct, as the name itself ends with an S.)

如果用户已将设备名称更改为默认表单以外的其他名称,显然无法使名称正确。

It obviously does not get the name right if the user has changed the name of the device to something else than the default form.

- (NSString *) extractPlayerNameFromDeviceName: (NSString *) deviceName  {

    // get words in device name
    NSArray *words = [deviceName componentsSeparatedByString:@" "];
    NSMutableArray *substrings = [[NSMutableArray alloc] init]; 
    for (NSString *word in words) {
        NSArray *subwords = [word componentsSeparatedByString:@"'"];
        [substrings addObjectsFromArray:subwords];
    }

    // find the name part of the device name
    NSString *playerName = [NSString stringWithString: @""];
    for (NSString *word in substrings) {
        if ([word compare:@"iPhone"] != 0
            && [word compare:@"iPod"] != 0
            && [word compare:@"iPad"] != 0
            && [word length] > 2) {
            playerName = word;
        }
    }

    // remove genitive
    unichar lastChar = [playerName characterAtIndex:[playerName length] - 1];
    if (lastChar == 's') {
        playerName = [playerName substringToIndex:[playerName length] - 1];
    }
    lastChar = [playerName characterAtIndex:[playerName length] - 1];
    if (lastChar == '\'') {
        playerName = [playerName substringToIndex:[playerName length] - 1];
    }
    return playerName;
}

我用它来建议我的应用中的用户名。这样,大多数用户都不必费心去写他们的用户名。

I use it for suggesting a username in my app. This way, most users won't have to bother writing their usernames.

我的应用程序没有连接到任何其他服务,如iTunes或Facebook,但每个用户都需要一个用户名称。那我怎么得到这个名字?

My app is not connected to any other service like iTunes or Facebook, but every user needs a user name. So how do I get the name?

推荐答案

我想对Ricky Helegesson的答案进行改进。它具有以下功能;

I'd like to offer an improvement on Ricky Helegesson's answer. It has the following features;


  • 它虽然效率较低,但因为它使用正则表达式而效率较低,但我想它应该是只打了一次电话。

  • 我已将它用于包括手机以及iPod,iPhone和iPad。

  • 它仅在iPad,iPhone等之后立即删除's,但仅在字符串末尾删除。

  • 删除iPad等等它们是第一个单词,如iPad模拟器。

  • 它将每个单词的第一个字母大写。

  • 它不区分大小写。

  • 这是一个函数,因为它没有依赖关系。

  • It is a little smaller, although less efficient because it uses regular expressions, but then I suppose it should be called only once.
  • I have expended it to include "phone" as well as "iPod, "iPhone" and "iPad".
  • It only removes "'s" when it immediately preceded by "iPad", "iPhone" etc., but only at the end of the string.
  • It removes "iPad" and so on when they are the first word, as in "iPad Simulator".
  • It capitalises the first letter of each word.
  • It is case insensitive.
  • It is a function because it has no dependencies.

这是代码:

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;
}

用于返回名称;

NSString* name = [nameFromDeviceName(UIDevice.currentDevice.name) componentsJoinedByString:@" "];

这有点复杂,所以我会解释一下;

This is somewhat complex, so I'll explain;


  1. 正则表达式包含三个部分;

  1. The regular expression holds three parts;

  1. 在字符串的开头,匹配但不返回iPhone,iPod,iPad或手机和可选字词 de。

  2. 在字符串的末尾,匹配并返回一个单词,后跟可选的's(不返回),然后是iPad,iPhone ,iPod或手机(也未返回)。

  3. 此匹配与上一个匹配,但它应适用于中文设备名称。 (改编自Travis Worm的提交。请告诉我它是否错误。)

  4. 匹配并返回任何与之前规则不符的词。

  1. At the start of the string, match but do not return "iPhone", "iPod", "iPad" or "phone" and an optional word "de".
  2. At the end of the string, match and return a word that is followed by and optional " 's" (which is not returned) and then "iPad", "iPhone", "iPod" or "phone" (which are not returned either).
  3. This match is the same as the previous, but it should work for Chinese device names. (Adapted from Travis Worm's submission. Please tell me if its wrong.)
  4. Match and return any word that doesn't match the previous rules.


  • 迭代所有匹配项,将它们大写并将它们添加到数组中。

  • 返回数组。

  • 如果一个名字以s结尾而没有iPad之类的撇号,我不会尝试改变它,因为没有万无一失的计算方法如果s是名称的一部分或名称的复数。

    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.

    享受!

    这篇关于从设备获取用户名的更好方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆