Light's Blog

The best or nothing.

iOS知识小集-170828

| Comments

TableView Header / Footer Height

UITableView默认有section header/footer高度,设为0无效,最小可设为1。

CNContactPickerViewController

1
2
3
4
5
6
7
8
9
10
11
12
13
14
-(void)pickContact{
  CNContactPickerViewController *controller = [[CNContactPickerViewController alloc] init];
  controller.delegate = self;
  [self presentViewController:controller animated:YES completion:nil];
}

- (void)contactPicker:(CNContactPickerViewController *)picker
    didSelectContactProperty:(CNContactProperty *)contactProperty {
  CNContact *contact = contactProperty.contact;
  NSString *name = [CNContactFormatter stringFromContact:contact style:CNContactFormatterStyleFullName];
  CNPhoneNumber *phoneValue = contactProperty.value;
  NSString *phoneNumber = phoneValue.stringValue;
  NSLog(@"%@--%@", name, phoneNumber);
}

CNContactStore

info.plist添加Privacy - Contacts Usage Description

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
- (void)checkAuthorization {
  NSLog(@"Contact - Check Authorization");
  //  status
  CNAuthorizationStatus status =
      [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
  //  check authorization
  if (status != CNAuthorizationStatusAuthorized) {
    NSLog(@"Contact - Not Authorized");
    //  request access
    CNContactStore *store = [CNContactStore new];
    NSLog(@"Contact - Request Authorization");
    [store
        requestAccessForEntityType:CNEntityTypeContacts
                 completionHandler:^(BOOL granted, NSError *_Nullable error) {
                   if (granted) {
                     //  YES
                     NSLog(@"Contact - Authorized");
                     [self fetchContact];
                   } else {
                     //  No
                     NSLog(@"Contact - Not Authorized");
                   }
                 }];
  } else {
    NSLog(@"Contact - Authorized");
    [self fetchContact];
  }
}

- (void)fetchContact {
  NSLog(@"Contact - Fetch Contact");
  //  keys
  NSArray *keys = @[
    CNContactPhoneNumbersKey,
    CNContactGivenNameKey,
    CNContactFamilyNameKey
  ];
  //  request
  CNContactFetchRequest *request =
      [[CNContactFetchRequest alloc] initWithKeysToFetch:keys];
  //  store
  NSError *error = nil;
  CNContactStore *store = [CNContactStore new];
  [store
      enumerateContactsWithFetchRequest:request
                                  error:&error
                             usingBlock:^(CNContact *_Nonnull contact,
                                          BOOL *_Nonnull stop) {
                               //  contact
                               NSString *firstName = contact.familyName;
                               NSString *lastName = contact.givenName;
                               NSLog(@"%@ %@", firstName, lastName);
                               //  phone number
                               for (CNLabeledValue *labeledValue in contact
                                        .phoneNumbers) {
                                 CNPhoneNumber *phoneValue = labeledValue.value;
                                 NSString *phoneNumber = phoneValue.stringValue;
                                 NSString *label = [CNLabeledValue
                                     localizedStringForLabel:labeledValue
                                                                 .label];
                                 NSLog(@"%@ : %@", label, phoneNumber);
                               }
                               NSLog(@"");
                             }];
}

iOS本地化

  1. Project -> Localizations -> Add Chinese
  2. New File -> Strings File
  3. Strings File -> Utities -> Localization -> Add Chinese
  4. Add Localized String

灯效说明

继承

智能家居控制

分解

Comments