iOS Review-DetectScheme

Targets

  • Using schemes to detect if the list of apps are installed then return the result as two strings - installed app list & uninstalled app list.

Implementation

Using method canOpenScheme, we can easily detect any URL Scheme to see if the application is installed, which I have introduced in my last article.
click here to see.
Therefore, in this task, I am going to write a runable tool module for any project(application) to complete the app-installed detection task. And I record this coding-testing process circle to mark down some usful gained knowledge and important notes for reminder.

1. detectWithArray & detectScheme

  • Outline
    This method is designed to return the result installed or uninstalled apps list seperated by commas as a string acording to an appList which is declared as an array.

1、 Traverse the array
So I will to get the app scheme and detect it each time by traversing the array. And this array must be made up with string objects - each object is the exact scheme of an app.
since let's simply traverse the array:

- (void) init {
    self = [super init];
    if(self){
        NSArray *appList = @[@"scheme1", @"scheme2"];
        NSMutableArray *installedApp = [[NSMutableArray alloc] init];
    }
    return self;
}

- (NSString *)detectWithArray:(NSArray *)appArray {
    
    for ( int i = 0; i < appArray.count, i++){
        [detectScheme:appArray[i];
    }
    NSString *installedList = [installedApp componentsJoinedByString:@","];
    return installedList;
}

2毡证、 Detect scheme
As you can see above, I now need to design a method detectScheme to complete the logic.

- (void)detectScheme: (NSString *)scheme{
    //here the scheme need to be added with "://"
    NSString *schemeURL = [[NSString alloc] initWithFormat:@"%@://", scheme];
    NSURL *url = [NSURL URLWithString: schemeURL];
    if ([[UIApplication sharedApplication] canOpenURL:url]) {
            //add this app scheme into the result array
            [_installedApp addObject:scheme];
    }   
}

3、 Call the method
Now we need to set the "entrance" of this method, which means I need to find a place to call the method. As I run the application, the main method will be called automatically, and also by AppDelegate class our application will do all the launching job. Ok, very cool, I can just call my method when this type of method being called.
However, I wrote it as a object method(-).So I need to creat an object of my class to call the method. It will be soon found not convenient.

#import "DetectScheme.h"


//code


DetectScheme *ds = [[DetectScheme alloc] init];
NSLog(@"Result:%@", [ds detectWithArray:ds.appList]);

However, This is a bad example for following reasons:
1鸦列、 To call the method, you need to creat an object of the class because it is object method. Also it need to creat class's properties. It is not convenient to be reused and might cause some unnecessary errors.
2、 In the method detectScheme, it will alloc and init some variables which are unnecessary (even not allowed) to be created many times like that. So to avoid such as memory leaking problem, it is better to creat the variables outside the for-loop. In other words, to maintain the result variable, we don't need to seperate the logic into two parts(methods).

2. detectWithDictionary only

To solve the problem above, I just comerge two method into one, and turn it into a class method.

Because there are schemes which are meaningless, so I use dictionary to identify the schemes with their app names as keys. Then the result will be more readable.

+ (NSString *)detectWithDictionary {
    NSDictionary *appList = [NSDictionary dictionaryWithObjectsAndKeys:@"scheme1", @"app1", @"scheme2", @"app2", @"scheme3", @"app3", nil];
    NSMutableArray *installedApp = [[NSMutableArray alloc] init];
    
    //檢測這個(gè)scheme是否存在留凭,存在即證明裝有此app
    for (NSString *key in appList) {
        NSString *schemeURL = [[NSString alloc] initWithFormat:@"%@://", [appList objectForKey:key]];
        NSURL *url = [NSURL URLWithString: schemeURL];
        if ([[UIApplication sharedApplication] canOpenURL:url]) {
            [installedApp addObject:key];
        }
    }
    
    //把installedapp的array變成string輸出
    NSString *installedList = [installedApp componentsJoinedByString:@","];
    NSLog(@"Result: %@", installedList);
    return installedList;
}

With this class method, I can simply use one line to call this method:

NSLog(@"Result:%@", [DetectScheme detectWithDictionary]);

3. Demo application

To test this tool class, I creat another application to do a unit test. Also in order to make this application's outcome more reasonable to understand, I also make it to return uninstalled apps list.
Have a glance at my file root:


1.png
1.png

rewrite the method to return both installed apps list and uninstalled apps list:

+ (NSString *)detectWithDictionary: (BOOL) forInstalled{
    NSDictionary *appList = [NSDictionary dictionaryWithObjectsAndKeys:@"scheme0", @"app0", @"scheme1", @"app1", @"scheme2", @"app2",  nil];
    NSMutableArray *installedApp = [[NSMutableArray alloc] init];
    NSMutableArray *uninstalledApp = [[NSMutableArray alloc] init];
    //檢測這個(gè)scheme是否存在菌羽,存在即證明裝有此app
    for (NSString *key in appList) {
        NSString *schemeURL = [[NSString alloc] initWithFormat:@"%@://", [appList objectForKey:key]];
        NSURL *url = [NSURL URLWithString: schemeURL];
        if ([[UIApplication sharedApplication] canOpenURL:url]) {
            [installedApp addObject:key];
        } else {
            [uninstalledApp addObject:key];
        }
    }
    
    //把installedapp的array變成string輸出
    NSString *installedList = [installedApp componentsJoinedByString:@","];
    NSString *uninstalledList = [uninstalledApp componentsJoinedByString:@","];
    NSLog(@"Result: %@", installedList);
    if(forInstalled){
        return installedList;
    } else {
        return uninstalledList;
    }
}

Let's run the demo to do the test.


2.png
2.png

Ending

This task it self is not a complecated one, but the way to think how to implement it reminds me of how important it is to optimize the algorithm and coding structure. And the very delicate codes don't come to our minds at the very first time, so I need to keep practicing more to polish my coding mind and skill.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市席揽,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌谓厘,老刑警劉巖幌羞,帶你破解...
    沈念sama閱讀 218,682評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異竟稳,居然都是意外死亡属桦,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,277評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門他爸,熙熙樓的掌柜王于貴愁眉苦臉地迎上來聂宾,“玉大人,你說我怎么就攤上這事诊笤∠敌常” “怎么了?”我有些...
    開封第一講書人閱讀 165,083評(píng)論 0 355
  • 文/不壞的土叔 我叫張陵讨跟,是天一觀的道長纪他。 經(jīng)常有香客問我,道長晾匠,這世上最難降的妖魔是什么茶袒? 我笑而不...
    開封第一講書人閱讀 58,763評(píng)論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮混聊,結(jié)果婚禮上弹谁,老公的妹妹穿的比我還像新娘。我一直安慰自己句喜,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,785評(píng)論 6 392
  • 文/花漫 我一把揭開白布沟于。 她就那樣靜靜地躺著咳胃,像睡著了一般。 火紅的嫁衣襯著肌膚如雪旷太。 梳的紋絲不亂的頭發(fā)上展懈,一...
    開封第一講書人閱讀 51,624評(píng)論 1 305
  • 那天销睁,我揣著相機(jī)與錄音,去河邊找鬼存崖。 笑死冻记,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的来惧。 我是一名探鬼主播冗栗,決...
    沈念sama閱讀 40,358評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼供搀!你這毒婦竟也來了隅居?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,261評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤葛虐,失蹤者是張志新(化名)和其女友劉穎胎源,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體屿脐,經(jīng)...
    沈念sama閱讀 45,722評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡涕蚤,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,900評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了的诵。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片万栅。...
    茶點(diǎn)故事閱讀 40,030評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖奢驯,靈堂內(nèi)的尸體忽然破棺而出申钩,到底是詐尸還是另有隱情,我是刑警寧澤瘪阁,帶...
    沈念sama閱讀 35,737評(píng)論 5 346
  • 正文 年R本政府宣布撒遣,位于F島的核電站,受9級(jí)特大地震影響管跺,放射性物質(zhì)發(fā)生泄漏义黎。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,360評(píng)論 3 330
  • 文/蒙蒙 一豁跑、第九天 我趴在偏房一處隱蔽的房頂上張望廉涕。 院中可真熱鬧,春花似錦艇拍、人聲如沸狐蜕。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,941評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽层释。三九已至,卻和暖如春快集,著一層夾襖步出監(jiān)牢的瞬間贡羔,已是汗流浹背廉白。 一陣腳步聲響...
    開封第一講書人閱讀 33,057評(píng)論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留乖寒,地道東北人猴蹂。 一個(gè)月前我還...
    沈念sama閱讀 48,237評(píng)論 3 371
  • 正文 我出身青樓,卻偏偏與公主長得像楣嘁,于是被迫代替她去往敵國和親磅轻。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,976評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容