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];
    
    //检测这个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];
    //检测这个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.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,569评论 4 363
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,499评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,271评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,087评论 0 209
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,474评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,670评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,911评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,636评论 0 202
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,397评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,607评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,093评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,418评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,074评论 3 237
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,092评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,865评论 0 196
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,726评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,627评论 2 270

推荐阅读更多精彩内容