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.

推荐阅读更多精彩内容