Files
yinmeng-ios/xplan-ios/Main/IM/Tool/CustomAttachmentDecoder.m
2023-08-04 10:20:48 +08:00

71 lines
1.5 KiB
Objective-C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// CustomAttachmentDecoder.m
// xplan-ios
//
// Created by 冯硕 on 2021/11/16.
//
#import "CustomAttachmentDecoder.h"
#import "NSObject+MJExtension.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