統計 iOS 鎖定裝置、解鎖次數-b

來源:互聯網
上載者:User

標籤:

今天下了個軟體,可以記錄手機解鎖的次數和使用時間,當然啦,App 必須在後台運行著。當時比較納悶的是有什麼 API 可以接收裝置解鎖事件或通知的,Google 了下,還真有哎——我是連結:http://stackoverflow.com/questions/14229955/is-there-a-way-to-check-if-the-ios-device-is-locked-unlocked?noredirect=1&lq=1。

鎖定裝置的狀態

由上面的回答可以知道,裝置在鎖定、解鎖的時候,SpringBoard 都會發出通知,iPhoneDevWiki 這裡:http://iphonedevwiki.net/index.php/SpringBoard.app/Notifications 能找到更多有趣的通知(注意:黃色標註的通知是有狀態變數與之關聯的,後面會用到)。貼訂閱通知的代碼:

CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), // 擷取通知中樞                                 NULL,  // 設定觀察者                                 deviceLockStatusChanged,  // 接收到通知時的回呼函數                                 CFSTR("com.apple.springboard.lockstate"),  // 通知名                                 NULL,  // 要觀察的對象                                 CFNotificationSuspensionBehaviorDeliverImmediately  // 決定應用在後台如何處理通知的標記                                 );

這裡所用的通知中樞並不是我們常用的 [NSNotificationCenter defalutCenter],而是 CFNotificationCenterRef 對象。提一下,即便前者的底層確實是由 CFNotificationCenter 實現的,但它們兩者不是 Toll-Free Bridged。回到 CFNotificationCenterRef,有下面三個函數獲得不同的通知中樞:

  1. CFNotificationCenterGetLocalCenter(void);

  2. CFNotificationCenterGetDistributedCenter(void);

  3. CFNotificationCenterGetDarwinNotifyCenter(void);

第一個是我們熟悉的 Local Center,可以理解為通知的行為完全由本進程維護,範圍也僅在本進程;
第二個是 Distributed Center,如果有看到這個函式宣告上的編譯條件,你就會發現僅在案頭系統上才有 Distributed Center 可用。它可以實現兩個進程之間的通訊,感興趣可以看看 Communicating With the Target Application:https://developer.apple.com/library/mac/documentation/UserExperience/Conceptual/PreferencePanes/Tasks/Communication.html ,似乎是通過 BundleID 實現對特定應用發送通知;
第三個是本文的重點,Darwin Center 的服務由系統的一個守護進程維護,相比 Local Center,通知的範圍擴大到了所有進程。這就為什麼我們的應用能夠接收到 SpringBoard.app 發送的通知。本文用的是用 CoreFoundation 的函數實現接收通知,除此之外,文檔裡還提到了利用 Mach Port, File Descriptors, Signal 等方法。查看 Darwin Notification Concepts:https://developer.apple.com/library/mac/documentation/Darwin/Conceptual/MacOSXNotifcationOv/DarwinNotificationConcepts/DarwinNotificationConcepts.html#//apple_ref/doc/uid/TP40005947-CH5-SW1 瞭解更多。

接下來要講講回呼函數了:

static void deviceLockStatusChanged(CFNotificationCenterRef center,                                void *observer,                                CFStringRef name,                                const void *object,                                CFDictionaryRef userInfo){NSString *nameString = (__bridge NSString*)name;int token; uint64_t state; notify_register_check(nameString.UTF8String, &token); notify_get_state(token, &state);NSLog(@"%@: token: %d, state: %llu", nameString, token, state);if (state == 0) {     counter++; } notify_cancel(token);}

函數的原型是直接抄文檔的,notify_register_check() 可以產生一個 token 值用來關聯某一個通知,接著用 notify_get_state() 就可以獲得響應狀態值。最後是 notify_cancel(),它用來取消跟 token 相關聯的通知和釋放相應的資源,按 manual pages 的描述好像只針對利用 Mach Port 和 File Descriptors 接收訊息時建立的資源。具體到這個回呼函數,不太清楚底層做了什麼,但我們能看到的是 token 被清零了。

後台運行

獲得鎖定裝置、解鎖的方法有了,接著是要讓應用儲存生命力,不能讓它被掛起,否則就統計不了次數了。比較常見的方法就是迴圈播放一段空白的聲音,然後在 Info.plist 裡面添加相應的欄位(KEY: UIBackgroundModes, VALUE: audio),或者直接在 Capabilities 裡面的 Background Modes 中 Audio 的複選框中打個勾。

 

統計 iOS 鎖定裝置、解鎖次數-b

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.