Note that there are some explanatory texts on larger screens.

plurals
  1. POBetter way to get the user's name from device?
    primarykey
    data
    text
    <p>I made a function that extracts a <strong>user name</strong> from the device name.</p> <p>The idea is to <strong>skip setup steps to allow the user to go straight to play</strong> upon starting the app the first time. </p> <p><strong>This is a sub-optimal approach</strong>, as I can never trust the device name to hold the name of the user. The question is: <strong>What is a better way to do this?</strong></p> <p>My function below gets the right name ...</p> <ul> <li>... if the default name of the device has not changed ("Sanna's iPod")</li> <li>... in English,</li> <li>... in French and similar ("iPod de Sanna")</li> <li>... 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.)</li> </ul> <p>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.</p> <pre><code>- (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 &amp;&amp; [word compare:@"iPod"] != 0 &amp;&amp; [word compare:@"iPad"] != 0 &amp;&amp; [word length] &gt; 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; } </code></pre> <p>I use it for suggesting a username in my app. This way, most users won't have to bother writing their usernames.</p> <p>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?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload