64 lines
1.8 KiB
Objective-C
64 lines
1.8 KiB
Objective-C
//
|
||
// CustomAttachmentDecoder.m
|
||
// mew-ios
|
||
//
|
||
// Created by 触海 on 2023/11/20.
|
||
//
|
||
|
||
#import "CustomAttachmentDecoder.h"
|
||
#import "NSObject+MEWExtension.h"
|
||
#import "AttachmentModel.h"
|
||
|
||
@implementation CustomAttachmentDecoder
|
||
|
||
- (id<NIMCustomAttachment>)decodeAttachment:(NSString *)content {
|
||
id<NIMCustomAttachment> attachment;
|
||
NSData *data = [content dataUsingEncoding:NSUTF8StringEncoding];
|
||
if (data) {
|
||
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
|
||
if ([dict isKindOfClass:[NSDictionary class]]) {
|
||
int first = [dict[@"first"] intValue];
|
||
int second = [dict[@"second"] intValue];
|
||
NSDictionary *data = dict[@"data"];
|
||
if ([data isKindOfClass:[NSString class]]) {
|
||
data = [self dictionaryWithJsonString:(NSString *)data];
|
||
}
|
||
if ([data isKindOfClass:[NSDictionary class]]) {
|
||
AttachmentModel *attachment = [[AttachmentModel alloc]init];
|
||
attachment.first = (short)first;
|
||
attachment.second = (short)second;
|
||
attachment.data = data;
|
||
return attachment;
|
||
}
|
||
|
||
}
|
||
}
|
||
return attachment;
|
||
}
|
||
|
||
|
||
//json格式字符串转字典:
|
||
- (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString {
|
||
if (jsonString == nil) {
|
||
return nil;
|
||
}
|
||
|
||
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
|
||
NSError *err;
|
||
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
|
||
|
||
options:NSJSONReadingMutableContainers
|
||
|
||
error:&err];
|
||
|
||
if(err) {
|
||
NSLog(@"json解析失败:%@",err);
|
||
return nil;
|
||
}
|
||
|
||
return dic;
|
||
|
||
}
|
||
|
||
@end
|