黑魔法(method-swizzling)解決第三方庫引發的問題

需求

最近做一個項目中,有個需求,所有網絡請求,都不顯示 NetworkActvityIndicator(也就是狀態欄里旋轉的小圈圈).

解決過程1:

全局搜索 NetworkIndicator 關鍵字, 把所有涉及 NetworkIndicator 的代碼去除,比如 [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];

測試并發現新問題

所有界面都不再顯示NetworkActvityIndicator了,唯獨一個播放視頻的界面依然顯示。

猜想: 第三方庫引發的問題

無論是哪些第三方庫,正常情況都會通過 setNetworkActivityIndicatorVisible 來 顯示狀態欄小圈圈。

驗證過程1

通過繼承 UIApplication 來重寫了 setNetworkActivityIndicatorVisible 方法。(如何繼承UIApplication,請看這里)并把斷點打在這個方法體內。

測試了正常調用 [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES]; 是會觸發斷點的。但是唯獨那個視頻界面,沒有觸發該斷點的情況下,正常顯示小圈圈。

驗證過程2

通過 KVO 監聽 UIApplication 的 networkActivityIndicatorVisible 屬性,結果還是和 驗證過程1 的情況一樣。
正常調用 [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES]; 是會觸發監聽, 唯獨那個視頻界面,沒有觸發監聽的情況下,正常顯示小圈圈。

所以, 視頻界面里顯示的小圈圈,肯定不是通過常規調用 setNetworkActivityIndicatorVisible 方法顯示出來的。

更新猜想: 第三方庫引發的問題,并且不是通過常規方法調用

驗證過程3

顯示小圈圈的情況下,分析了該界面的視圖層級,發現在 statusBar 上,有 類型為UIActivityIndicatorView的視圖存在(并且怪異的存在了兩個)。

那正常情況下,通過 [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES]; 顯示小圈圈時,視圖層級是如何的呢? 通過分析驗證, 也是一樣的。

層級都是 UIStatusBarView -> UIStatusBarForegroundView -> UIStatusBarActivityItemView -> UIActivityIndicatorView

想到解決方案:

既然小圈圈都是 UIActivityIndicatorView 類型的視圖,而 UIActivityIndicatorView 開始動畫常規都是調用 startAnimation 方法。
那何不使用黑魔法(method swizzling)來重寫它的 startAnimation 方法,
判斷它的superView是否為 “UIStatusBarActivityItemView”類型,如果是,則直接跳出。否則,執行原有的 startAnimation方法。

Talk is cheap. Show me the code.

以下是 .m 文件的代碼

@implementation UIActivityIndicatorView (HideNetworkActivityIndicator)+ (void)load {static dispatch_once_t onceToken;dispatch_once(&onceToken, ^{Class class = [self class];SEL originalSelector = @selector(startAnimating);SEL swizzledSelector = @selector(xxx_startAnimating);Method originalMethod = class_getInstanceMethod(class, originalSelector);Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);// When swizzling a class method, use the following:// Class class = object_getClass((id)self);// ...// Method originalMethod = class_getClassMethod(class, originalSelector);// Method swizzledMethod = class_getClassMethod(class, swizzledSelector);BOOL didAddMethod =class_addMethod(class,originalSelector,method_getImplementation(swizzledMethod),method_getTypeEncoding(swizzledMethod));if (didAddMethod) {class_replaceMethod(class,swizzledSelector,method_getImplementation(originalMethod),method_getTypeEncoding(originalMethod));} else {method_exchangeImplementations(originalMethod, swizzledMethod);}});
}#pragma mark - Method Swizzling- (void)xxx_startAnimating{if (self.superview != nil && [NSStringFromClass([self.superview class]) isEqualToString: @"UIStatusBarActivityItemView"]) {NSLog(@"黑魔法禁止狀態欄的loading顯示: %@", self);} else {[self xxx_startAnimating];}}@end

成功了!!!

(在xxx_startAnimation方法體內打斷點,程序進入視頻播放界面,觸發斷點,看調用棧,果然是第三方庫引發的問題。)

參考資料:https://nshipster.cn/method-s...

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/388213.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/388213.shtml
英文地址,請注明出處:http://en.pswp.cn/news/388213.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Python 操作 MySQL 的5種方式(轉)

Python 操作 MySQL 的5種方式 不管你是做數據分析,還是網絡爬蟲,Web 開發、亦或是機器學習,你都離不開要和數據庫打交道,而 MySQL 又是最流行的一種數據庫,這篇文章介紹 Python 操作 MySQL 的5種方式,你可以…

unity3d 人員控制代碼

普通瀏覽復制代碼private var walkSpeed : float 1.0;private var gravity 100.0;private var moveDirection : Vector3 Vector3.zero;private var charController : CharacterController;function Start(){charController GetComponent(CharacterController);animation.w…

刪除wallet里面登機牌_登機牌丟失問題

刪除wallet里面登機牌On a sold-out flight, 100 people line up to board the plane. The first passenger in the line has lost his boarding pass but was allowed in regardless. He takes a random seat. Each subsequent passenger takes their assigned seat if availa…

PHP 備份還原 MySql 數據庫

原生 PHP 備份還原 MySql 數據庫 支持 MySql,PDO 兩種方式備份還原 php5.5 以上的版本建議開啟pdo擴展,使用 pdo 備份還原數據 備份文件夾 db_backup、import/log 文件要有讀寫權限環境版本 本人測試環境 php:5.5.38 /5.6.27-nts/7.0.12-nts; mysql: 5.5…

Java? 教程(Queue接口)

Queue接口 Queue是在處理之前保存元素的集合&#xff0c;除了基本的Collection操作外&#xff0c;隊列還提供額外的插入、刪除和檢查操作&#xff0c;Queue接口如下。 public interface Queue<E> extends Collection<E> {E element();boolean offer(E e);E peek();…

字符串操作截取后面的字符串_對字符串的5個必知的熊貓操作

字符串操作截取后面的字符串We have to represent every bit of data in numerical values to be processed and analyzed by machine learning and deep learning models. However, strings do not usually come in a nice and clean format and require preprocessing to con…

最新 Unity3D鼠標滑輪控制物體放大縮小 [

var s 1.0;function Update () {var cube GameObject.Find("Cube");if(Input.GetAxis("Mouse ScrollWheel")){s Input.GetAxis("Mouse ScrollWheel");cube.transform.localScaleVector3(1*s,1*s,1*s);}}

sublime-text3 安裝 emmet 插件

下載sublime&#xff0c;http://www.sublimetext.com/ 安裝package control &#xff1a;https://packagecontrol.io/ins... 這個地址需要翻墻&#xff0c;訪問不了的可以看下圖 import urllib.request,os,hashlib; h 6f4c264a24d933ce70df5dedcf1dcaee ebe013ee18cced0ef93d…

數據科學家訪談錄 百度網盤_您應該在數據科學訪談中向THEM提問。

數據科學家訪談錄 百度網盤A quick search on Medium with the keywords “Data Science Interview” resulted in hundreds of Medium articles to help guide the reader from what concepts are covered to even specific company interviews ranging from Tesla, Walmart, …

unity3d]鼠標點擊地面人物自動走動(也包含按鍵wasdspace控制)

目錄(?)[-] 一效果圖二大概步驟 創建一個plane設置層為Terrain因為后面要判斷是否點擊的是這個層準備好人物模型并且將三個腳本拖放到人物上并且將動畫文件也拖放好記得看前面提醒哦 ThirdPersonCamera相當于smoothflowThirdPersonController修改版mouseMoveContr鼠標點擊人物…

uva 524(Prime Ring Problem UVA - 524 )

dfs練習題,我素數打表的時候ji了&#xff0c;一直沒發現實際上是ji*i&#xff0c;以后可記住了。還有最后一行不能有空格。。。昏迷了半天 我的代碼(紫書上的算法) #include <bits/stdc.h> using namespace std; int bk[110]; int num[110]; int vis[110]; int n; void d…

Web 開發基礎

一、 Web 開發簡介 最早的軟件都是運行在大型機上的&#xff0c;軟件使用者登陸到大型機上去運行軟件。后來隨著 PC 機的興起&#xff0c;軟件開始主要運行在桌面上&#xff0c;而數據庫這樣的軟件運行在服務器端&#xff0c;這種 Client/Server 模式簡稱 CS 架構。隨著互聯網的…

power bi函數_在Power BI中的行上使用聚合函數

power bi函數Aggregate functions are one of the main building blocks in Power BI. Being used explicitly in measures, or implicitly defined by Power BI, there is no single Power BI report which doesn’t use some sort of aggregate functions.聚合功能是Power BI…

關于如何在Python中使用靜態、類或抽象方法的權威指南

Python中方法的工作方式 方法是存儲在類屬性中的函數&#xff0c;你可以用下面這種方式聲明和訪問一個函數 >>> class Pizza(object):... def __init__(self, size):... self.size size... def get_size(self):... return self.size...>&…

廣義估計方程估計方法_廣義估計方程簡介

廣義估計方程估計方法A key assumption underpinning generalized linear models (which linear regression is a type of) is the independence of observations. In longitudinal data this will simply not hold. Observations within an individual (between time points) …

css二

結構性偽類:nth-child(index)系列1.:first-child2.:last-child3.nth-last-child(index)4.only-child :nth-of-type(index)系列1.first-of-type2.last-of-type3.nth-last-type(index)4.only-of-type :not偽類處理導航欄最后一個豎劃線a:not(:last-of-type) :empty偽類 選中所有內…

Unity3d鼠標點擊屏幕來控制人物的走動

今天呢&#xff0c;我們來一起實現一個在RPG中游戲中十分常見的功能&#xff0c;通過鼠標點擊屏幕來控制人物的走動。首先來說一下原理&#xff0c;當我們點擊屏幕時&#xff0c;我們按照一定的方法&#xff0c;將屏幕上的二維坐標轉化為三維坐標&#xff0c;然后我們從攝像機位…

Java中的ReentrantLock和synchronized兩種鎖定機制的對比

2019獨角獸企業重金招聘Python工程師標準>>> 多線程和并發性并不是什么新內容&#xff0c;但是 Java 語言設計中的創新之一就是&#xff0c;它是第一個直接把跨平臺線程模型和正規的內存模型集成到語言中的主流語言。核心類庫包含一個 Thread 類&#xff0c;可以用它…

10.15 lzxkj

幾天前寫的&#xff0c;忘了放了&#xff0c;在此填坑 10月16的題我出的不寫題解了 lzxkj 題目背景 眾所不周知的是&#xff0c; 酒店之王 xkj 一個經常迷失自我的人 有一天&#xff0c; 當起床鈴再一次打響的時候&#xff0c; TA 用 O(1)的時間在 TA 那早就已經生銹的大腦中自…

大數定理 中心極限定理_中心極限定理:直觀的遍歷

大數定理 中心極限定理One of the most beautiful concepts in statistics and probability is Central Limit Theorem,people often face difficulties in getting a clear understanding of this and the related concepts, I myself struggled understanding this during my…