LineSDKJSONWebToken+RawString.m 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. //
  2. // LineSDKJSONWebToken+RawString.m
  3. // XenonSDK
  4. //
  5. // Created by fq on 2021/1/21.
  6. // Copyright © 2021 SAGESSE. All rights reserved.
  7. //
  8. #import "LineSDKJSONWebToken+RawString.h"
  9. #import <objc/runtime.h>
  10. @implementation LineSDKJSONWebToken (RawString)
  11. + (void)load {
  12. static dispatch_once_t onceToken;
  13. dispatch_once(&onceToken, ^{
  14. [self ty_swizzleInstanceMethod:@selector(initWithString:) with:@selector(ty_initWithString:)];
  15. });
  16. }
  17. - (instancetype)ty_initWithString:(NSString *)string {
  18. id instance = [self ty_initWithString:string];
  19. [instance ty_setAssociate:string withKey:@"rawString"];
  20. return instance;
  21. }
  22. - (NSString *)rawString {
  23. return [self ty_getAssociateWithKey:@"rawString"];
  24. }
  25. + (BOOL)ty_swizzleInstanceMethod:(SEL)oriSel with:(SEL)swiSel {
  26. Method oriMethod = class_getInstanceMethod(self, oriSel);
  27. Method swiMethod = class_getInstanceMethod(self, swiSel);
  28. if (!oriMethod || !swiMethod) return NO;
  29. class_addMethod(self,
  30. oriSel,
  31. class_getMethodImplementation(self, oriSel),
  32. method_getTypeEncoding(oriMethod));
  33. class_addMethod(self,
  34. swiSel,
  35. class_getMethodImplementation(self, swiSel),
  36. method_getTypeEncoding(swiMethod));
  37. method_exchangeImplementations(class_getInstanceMethod(self, oriSel),
  38. class_getInstanceMethod(self, swiSel));
  39. return YES;
  40. }
  41. //相当于 setValue:forKey 进行关联value对象
  42. - (void)ty_setAssociate:(id)obj withKey:(const void * _Nonnull)key {
  43. if (!key) {
  44. return;
  45. }
  46. objc_setAssociatedObject(self, key, obj, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  47. }
  48. //用来读取对象
  49. - (id)ty_getAssociateWithKey:(const void * _Nonnull)key {
  50. if (!key) {
  51. return nil;
  52. }
  53. return objc_getAssociatedObject(self, key);
  54. }
  55. @end