Just My Life & My Work

Posts tagged ‘ios’

[iOS] iBeacon背景偵測流程

半年前在幫朋友阿強研究iBeacon,發現iOS在這方面比Android限制還多!首先來看iBeacon背景偵測流程,是跟前景偵測有何不一樣?

App在前景時,會陸續觸發三支APIdidEnterRegion、didRangeBeacons、didExitRegion,如上圖從時間點A到時間點B。

(繼續閱讀…)

[iOS] 獲取WebView快取中的圖片

由於原生SDK沒有辦法直接獲取UIWebView中已下載的影像,因為想避免重複請求消耗資源避免減損使用者體驗,於是我找到可立馬使用的第三方套件:RNCachingURLProtocol。​雖然發現還要修改才能使用XD~

套件簡介

Simple offline caching for UIWebView and other NSURLConnection clients

WebView的圖片會使用NSURLProtocol來進行快取Cache,此第三方快取圖片會儲存到指定的文件夾,之後可以讀取此指定文件夾來獲取WebView的快取。

(繼續閱讀…)

[iOS] 應用程式的生命週期 (App Life Cycle)

先前已經了解過視圖的生命週期 (View Life Cycle),現在來研究高一層級的應用程式的生命週期 (App Life Cycle)。開發超過六年(從2012年開始)iOS App的我,儘管已爐火純青可以隨意開發一款iOS App,然而再習以為常的開發過程,一定還存在些我不太熟悉的細節!套句郭台銘的霸氣台詞「魔鬼藏在細節中!」所以若能透過圖解的方式來更理解兩個生命週期,想必能研發出品質更好的iOS App。

(繼續閱讀…)

[iOS] 背景模式震動

原本想使用區域推播(Local Notification)來提醒使用者,遇到需求只要震動而不要有介面顯示,可以怎麼做呢?原來只要開背景模式(Required background modes)的「App plays audio or streams audio/video using AirPlay」,即可在偵測到Beacon同時震動!

可是送審會不會有問題?畢竟文檔上所描述的使用方式我都沒有實現⋯⋯

(繼續閱讀…)

[iOS] 指定Pod版本

四年前(2014年),我已寫過Cocoapods教學:使用 CocoaPods 管理第三方套件,此時要講指定Pod版本。好的第三方套件會時常更新來與時俱進,除了新增feature之外,還有修正既有的bug。

當我們自己開發的App已經過了好幾年,想要再透過Cocospods加入新的套件,此時要考慮到舊有的套件盡可能不要更新,若逼不得已還是得更新啦⋯⋯

使用Cocoapods的好處之一就是可以指定Pod版本。像是我有遇到下一個版本的語法被改掉,太難改的話我就會先返回原先的版本。

(繼續閱讀…)

[iOS] 透過HealthKit獲取步數與距離

原本想透過智慧運動裝置來獲取步數與距離,可惜遲遲等不到韌體開發完成,只好暫且透過HealthKit獲取步數與距離!因為iPhone本身就有運動感測器,會自動算出步數與距離,然後寫入HealthKit,想做運動健康相關App,於是有數據來源可使用。

iOS 10開始之後要在info.plist設定Privacy

(繼續閱讀…)

[iOS] BLE某些概念的解釋

已經持續與藍芽奮戰近一年,來回顧一下BLE某些概念的解釋

首先總結CBServiceCBCharacteristic關係如圖,這樣才好理解其角色負責的任務!

bluetooth Services Characteristics

(繼續閱讀…)

[iOS] 移除看不見的字元 (Remove Invisible Character)

看不到的不代表不存在~」,寫程式也會遇到這個現象!我在比較日期字串時,發現明明兩者都是2018-07-17,卻被判斷出不一樣!?

[iOS] 移除看不見的字元 (Remove Invisible Character).png

好在透過取得字串長度,才發現真有看不見的字元「\0」!

(lldb) po firmwareVersionLabel.text.length
32

(lldb) po newVersionLabel.text.length
10

實際上2018-07-17只有10個字元,而韌體回傳卻有32個字元,於是知道後面看不到的字元共有22個。

(繼續閱讀…)

[iOS] 以最後修改時間列出檔案名稱 (List Contents of Directory By Last Modification Date)

先前已寫過[iOS] 列出資料夾裡的檔案名稱 (List Contents of Directory),然而現在想要以最後修改時間列出檔案名稱 (List Contents of Directory By Last Modification Date),直接來看程式碼:

/**
 Theme: List Contents of Directory By Last Modification Date
 IDE: Xcode 9
 Language: Objective C
 Date: 107/08/02
 Author: HappyMan
 Blog: https://cg2010studio.com/
 */

+(NSArray *)listFileAtPathSortedByModificationDate:(NSString *)folderPath
{
    NSError* error = nil;
    NSArray* filesArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:folderPath error:&error];
    if(error != nil) {
        NSLog(@"Error in reading files: %@", [error localizedDescription]);
        return @[];
    }

    // sort by creation date
    NSMutableArray* filesAndProperties = [NSMutableArray arrayWithCapacity:[filesArray count]];
    for(NSString* file in filesArray) {
        NSString* filePath = [folderPath stringByAppendingPathComponent:file];
        NSDictionary* properties = [[NSFileManager defaultManager]
                                    attributesOfItemAtPath:filePath
                                    error:&error];
        NSDate* modDate = [properties objectForKey:NSFileModificationDate];

        if(error == nil)
        {
            [filesAndProperties addObject:[NSDictionary dictionaryWithObjectsAndKeys:
                                           file, @"fileName",
                                           modDate, @"lastModificationDate",
                                           nil]];
        }
    }

    // sort using a block
    // order inverted as we want latest date first
    NSArray* sortedFiles = [filesAndProperties sortedArrayUsingComparator:
                            ^(id path1, id path2)
                            {
                                // compare
                                NSComparisonResult comp = [[path1 objectForKey:@"lastModificationDate"] compare:
                                                           [path2 objectForKey:@"lastModificationDate"]];
                                // invert ordering
                                if (comp == NSOrderedDescending) {
                                    comp = NSOrderedAscending;
                                }
                                else if(comp == NSOrderedAscending){
                                    comp = NSOrderedDescending;
                                }
                                return comp;
                            }];

    return sortedFiles;
}

以我的範例取得:

<__NSArrayI 0x137459190>(
{
fileName = “2018-08-02 10:17:20~2018-08-02 10:19:20.ecg";
lastModificationDate = “2018-08-02 02:19:24 +0000″;
},
{
fileName = “2018-08-02 10:11:32~2018-08-02 10:13:32.ecg";
lastModificationDate = “2018-08-02 02:14:48 +0000″;
},
{
fileName = “2018-08-02 10:09:28~2018-08-02 10:11:28.ecg";
lastModificationDate = “2018-08-02 02:11:31 +0000″;
},
{
fileName = “2018-08-02 10:06:47~2018-08-02 10:08:47.ecg";
lastModificationDate = “2018-08-02 02:08:50 +0000″;
},
{
fileName = “2018-08-02 09:02:28~2018-08-02 09:04:28.ecg";
lastModificationDate = “2018-08-02 01:04:30 +0000″;
},
{
fileName = “2018-07-31 23:40:03~2018-07-31 23:42:03.ecg";
lastModificationDate = “2018-07-31 15:42:07 +0000″;
},
{
fileName = “2018-07-31 14:43:38~2018-07-31 14:45:38.ecg";
lastModificationDate = “2018-07-31 06:45:40 +0000″;
},
{
fileName = “2018-07-31 14:40:58~2018-07-31 14:42:58.ecg";
lastModificationDate = “2018-07-31 06:43:01 +0000″;
}
)

參考:Get directory contents in date modified order

標籤雲