Skip to content

7 ユーザー属性管理

ユーザーの属性を API レベルで設定したり取得したりする場合の実装例をご説明します。

7.1 メソッドからユーザーの属性を設定する

ユーザーの属性を設定する場合、PopinfoReceiver クラスの setSegmentsWithParameters:forKey:completion: メソッドを利用します。

setSegments:forKey:completion: は、安定性の問題から今後のバージョンで廃止を予定しております。

カテゴリID 200 に属性値 4 をセットする例です。

NSDictionary<NSString *, NSArray<NSString *> *> *dict = @{
        @"200": @[@"4"]
};
[self setSegmentsWithParameters:dict completion:^(BOOL isOk, NSString *errorCode) {
    // 設定の結果をダンプ
    NSLog(@"setSegments isOk = %d, errorCode = %@", isOk, errorCode);
}];
let dict = ["200": ["4"]]
PopinfoReceiver.shared.setSegments(parameters: dict)  { isOk, errorCode in
    // 設定の結果をダンプ
    print("setSegments isOK = \(String(describing: isOk)), errorCode = \(String(describing: errorCode))")
}

カテゴリID 800 に属性値 218 と 219 をセットする例です。

NSDictionary<NSString *, NSArray<NSString *> *> *dict = @{
        @"800": @[@"218", @"219"]
};
[self setSegmentsWithParameters:dict completion:^(BOOL isOk, NSString *errorCode) {
    // 設定の結果をダンプ
    NSLog(@"setSegments isOk = %d, errorCode = %@", isOk, errorCode);
}];
let dict = ["800": ["218", "219"]]
PopinfoReceiver.shared.setSegments(parameters: dict)  { isOk, errorCode in
    // 設定の結果をダンプ
    print("setSegments isOK = \(String(describing: isOk)), errorCode = \(String(describing: errorCode))")
}

カテゴリID 200 に属性値 4 を、カテゴリID 800 に属性値 218 と 219 を、カテゴリID 900 に属性値 101, 102, 103 をセットする例です。

NSDictionary<NSString *, NSArray<NSString *> *> *dict = @{
        @"200": @[@"4"],
        @"800": @[@"218", @"219"],
        @"900": @[@"101", @"102", @"103"]
};
[self setSegmentsWithParameters:dict completion:^(BOOL isOk, NSString *errorCode) {
    // 設定の結果をダンプ
    NSLog(@"setSegments isOk = %d, errorCode = %@", isOk, errorCode);
}];
let dict = [
    "200": ["4"],
    "800": ["218", "219"],
    "900": ["101", "102", "103"]
]
PopinfoReceiver.shared.setSegments(parameters: dict)  { isOk, errorCode in
    // 設定の結果をダンプ
    print("setSegments isOK = \(String(describing: isOk)), errorCode = \(String(describing: errorCode))")
}

カテゴリID 200 の属性値をクリアする例です。

NSDictionary<NSString *, NSArray<NSString *> *> *dict = @{
        @"200": @[]
};
[self setSegmentsWithParameters:dict completion:^(BOOL isOk, NSString *errorCode) {
    // 設定の結果をダンプ
    NSLog(@"setSegments isOk = %d, errorCode = %@", isOk, errorCode);
}];
let dict = ["200": []]
PopinfoReceiver.shared.setSegments(parameters: dict)  { isOk, errorCode in
    // 設定の結果をダンプ
    print("setSegments isOK = \(String(describing: isOk)), errorCode = \(String(describing: errorCode))")
}

7.2 メソッドからユーザーの属性を取得する

現在設定されているユーザーの属性を取得する例です。

[[PopinfoReceiver sharedReceiver] getSegmentsWithCompletion:^(BOOL isOk, NSString *errorCode, NSArray *segmentsAry) {
    // segmentsAry 内に取得した属性が入っている。取得した属性をダンプ
    for (NSDictionary *segmentDict in segmentsAry) {
        NSLog(@"is_default=%@", [segmentDict objectForKey:@"is_default"]);
        NSLog(@"value_name=%@", [segmentDict objectForKey:@"value_name"]);
        NSLog(@"is_multiple=%@", [segmentDict objectForKey:@"is_multiple"]);
        NSLog(@"value_id=%@", [segmentDict objectForKey:@"value_id"]);
        NSLog(@"key_id=%@", [segmentDict objectForKey:@"key_id"]);
        NSLog(@"key_name=%@", [segmentDict objectForKey:@"key_name"]);
        NSLog(@"is_set=%@", [segmentDict objectForKey:@"is_set"]);
    }
}];
PopinfoReceiver.shared.getSegmentsWithCompletion { isOK, errorCode, segmentsAry in
    guard let segmentsDictArray: [[AnyHashable: Any]] = segmentsAry else {
        return
    }
    // segmentsAry 内に取得した属性が入っている。取得した属性をダンプ
    for segment in segmentsDictArray {
        print("is_default=\(String(describing: segment["is_default"]))")
        print("value_name=\(String(describing: segment["value_name"]))")
        print("is_multiple=\(String(describing: segment["is_multiple"]))")
        print("value_id=\(String(describing: segment["value_id"]))")
        print("key_id=\(String(describing: segment["key_id"]))")
        print("key_name=\(String(describing: segment["key_name"]))")
        print("is_set=\(String(describing: segment["is_set"]))")
    }
}

ユーザーが設定していない属性も含め、全ての属性を取得する例です。

[[PopinfoReceiver sharedReceiver] getSegmentsAllWithCompletion:^(BOOL isOk, NSString *errorCode, NSArray *segmentsAry) {
    // segmentsAry 内に取得した属性が入っている。取得した属性をダンプ
    for (NSDictionary *segmentDict in segmentsAry) {
        NSLog(@"is_default=%@", [segmentDict objectForKey:@"is_default"]);
        NSLog(@"value_name=%@", [segmentDict objectForKey:@"value_name"]);
        NSLog(@"is_multiple=%@", [segmentDict objectForKey:@"is_multiple"]);
        NSLog(@"value_id=%@", [segmentDict objectForKey:@"value_id"]);
        NSLog(@"key_id=%@", [segmentDict objectForKey:@"key_id"]);
        NSLog(@"key_name=%@", [segmentDict objectForKey:@"key_name"]);
        NSLog(@"is_set=%@", [segmentDict objectForKey:@"is_set"]);
    }
}];
PopinfoReceiver.shared.getSegmentsAll(completion: { isOK, errorCode, segmentsAry in
    guard let segmentsDictArray: [[AnyHashable: Any]] = segmentsAry else {
        return
    }
    // segmentsAry 内に取得した属性が入っている。取得した属性をダンプ
    for segment in segmentsDictArray {
        print("is_default=\(String(describing: segment["is_default"]))")
        print("value_name=\(String(describing: segment["value_name"]))")
        print("is_multiple=\(String(describing: segment["is_multiple"]))")
        print("value_id=\(String(describing: segment["value_id"]))")
        print("key_id=\(String(describing: segment["key_id"]))")
        print("key_name=\(String(describing: segment["key_name"]))")
        print("is_set=\(String(describing: segment["is_set"]))")
    }
})