説明なし
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

PersonNameComponentsFormatting.m 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. //
  2. // MIT License
  3. //
  4. // Copyright (c) 2019 Daniel Lupiañez Casares
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in all
  14. // copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. // SOFTWARE.
  23. //
  24. #import <Foundation/Foundation.h>
  25. const char* AppleAuth_CopyCString(const char* string)
  26. {
  27. if (string == NULL)
  28. return NULL;
  29. char* res = (char*)malloc(strlen(string) + 1);
  30. strcpy(res, string);
  31. return res;
  32. }
  33. const char* AppleAuth_GetPersonNameUsingFormatter(const char *payload, int style, bool usePhoneticRepresentation)
  34. {
  35. if (payload == NULL)
  36. return NULL;
  37. NSError *error = nil;
  38. NSData *payloadData = [NSData dataWithBytes:payload length:strlen(payload)];
  39. NSDictionary * nameComponentsDictionary = [NSJSONSerialization JSONObjectWithData:payloadData options:0 error:&error];
  40. if (error)
  41. return NULL;
  42. if (@available(iOS 9.0, tvOS 9.0, macOS 10.11, *)) {
  43. NSPersonNameComponents *nameData = [[NSPersonNameComponents alloc] init];
  44. [nameData setNamePrefix:[nameComponentsDictionary objectForKey:@"_namePrefix"]];
  45. [nameData setGivenName:[nameComponentsDictionary objectForKey:@"_givenName"]];
  46. [nameData setMiddleName:[nameComponentsDictionary objectForKey:@"_middleName"]];
  47. [nameData setFamilyName:[nameComponentsDictionary objectForKey:@"_familyName"]];
  48. [nameData setNameSuffix:[nameComponentsDictionary objectForKey:@"_nameSuffix"]];
  49. [nameData setNickname:[nameComponentsDictionary objectForKey:@"_nickname"]];
  50. NSDictionary *phoneticRepresentationDictionary = [nameComponentsDictionary objectForKey:@"_phoneticRepresentation"];
  51. if (phoneticRepresentationDictionary)
  52. {
  53. NSPersonNameComponents *phoneticRepresentation = [[NSPersonNameComponents alloc] init];
  54. [phoneticRepresentation setNamePrefix:[phoneticRepresentationDictionary objectForKey:@"_namePrefix"]];
  55. [phoneticRepresentation setGivenName:[phoneticRepresentationDictionary objectForKey:@"_givenName"]];
  56. [phoneticRepresentation setMiddleName:[phoneticRepresentationDictionary objectForKey:@"_middleName"]];
  57. [phoneticRepresentation setFamilyName:[phoneticRepresentationDictionary objectForKey:@"_familyName"]];
  58. [phoneticRepresentation setNameSuffix:[phoneticRepresentationDictionary objectForKey:@"_nameSuffix"]];
  59. [phoneticRepresentation setNickname:[phoneticRepresentationDictionary objectForKey:@"_nickname"]];
  60. [nameData setPhoneticRepresentation:phoneticRepresentation];
  61. }
  62. NSPersonNameComponentsFormatterOptions options = usePhoneticRepresentation ? NSPersonNameComponentsFormatterPhonetic : 0;
  63. NSString *localizedName = [NSPersonNameComponentsFormatter localizedStringFromPersonNameComponents:nameData style:style options:options];
  64. return AppleAuth_CopyCString([localizedName UTF8String]);
  65. } else {
  66. return NULL;
  67. }
  68. }