NSURLProtocol+WKWebView.m 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. //
  2. // NSURLProtocol+WKWebView.m
  3. // Starbuds
  4. //
  5. // Created by 翟玉磊 on 2020/4/27.
  6. // Copyright © 2020 翟玉磊. All rights reserved.
  7. //
  8. #import "NSURLProtocol+WKWebView.h"
  9. //FOUNDATION_STATIC_INLINE 属于属于runtime范畴,你的.m文件需要频繁调用一个函数,可以用static inline来声明。从SDWebImage从get到的。
  10. FOUNDATION_STATIC_INLINE Class ContextControllerClass() {
  11. static Class cls;
  12. if (!cls) {
  13. cls = [[[WKWebView new] valueForKey:@"browsingContextController"] class];
  14. }
  15. return cls;
  16. }
  17. FOUNDATION_STATIC_INLINE SEL RegisterSchemeSelector() {
  18. return NSSelectorFromString(@"registerSchemeForCustomProtocol:");
  19. }
  20. FOUNDATION_STATIC_INLINE SEL UnregisterSchemeSelector() {
  21. return NSSelectorFromString(@"unregisterSchemeForCustomProtocol:");
  22. }
  23. @implementation NSURLProtocol (WKWebView)
  24. + (void)wk_registerScheme:(NSString *)scheme {
  25. Class cls = ContextControllerClass();
  26. SEL sel = RegisterSchemeSelector();
  27. if ([(id)cls respondsToSelector:sel]) {
  28. // 放弃编辑器警告
  29. #pragma clang diagnostic push
  30. #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
  31. [(id)cls performSelector:sel withObject:scheme];
  32. #pragma clang diagnostic pop
  33. }
  34. }
  35. + (void)wk_unregisterScheme:(NSString *)scheme {
  36. Class cls = ContextControllerClass();
  37. SEL sel = UnregisterSchemeSelector();
  38. if ([(id)cls respondsToSelector:sel]) {
  39. // 放弃编辑器警告
  40. #pragma clang diagnostic push
  41. #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
  42. [(id)cls performSelector:sel withObject:scheme];
  43. #pragma clang diagnostic pop
  44. }
  45. }
  46. @end