ios获取已安装的应用列表

来源:互联网 发布:伟达公关 知乎 编辑:程序博客网 时间:2024/05/17 22:45

URL Scheme

我们知道可以给应用设置URL Scheme,这样别的应用就可以通过这个地址打开咱们的应用。其实还有一个api叫canOpenURL.这样如果咱们知道要检查的IOS应用列表的URL Scheme的话,就可以用canOpenURL检查一下。

假设有个APP注册了URL Scheme:myapp

if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"myapp://com.yourcompany.appName"]];) {    //YES} else {    //NO}

openURL能帮助你运行Maps,SMS,Browser,Phone甚至其他的应用程序。
openURL的使用方法:
[[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:myURLString]];

自己定义URL,方法如下:
打开info.plist,添加一项URL types
展开URL types,再展开Item1,将Item1下的URL identifier修改为URL Scheme
展开URL Scheme,将Item1的内容修改为myapp

或者:(增加一下此段设置)

<key>CFBundleURLTypes</key>    <array>        <dict>            <key>CFBundleURLSchemes</key>            <array>                <string>myapp</string>            </array>            <key>CFBundleURLName</key>            <string>com.yourcompany.appName</string>        </dict>    </array>

其他程序可通过myapp://访问此自定义URL

可通过

[[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString: @"myapp://com.yourcompany.appName"]];

来判断用户机器中是否安装了该程序
最近接触到程序内打开自己,通过第三方控件来调用本身程序:

通过- (BOOL)application:(UIApplication )application handleOpenURL:(NSURL )url
可以通过处理url来获取信息扫行相应操作。

运行中得进程

这种方法是获取运行中的应用列表。如果应用没被运行过或不在后台,就无法获取到

// .h@interface UIDevice (ProcessesAdditions)- (NSArray *)runningProcesses;@end// .m#import <sys/sysctl.h>@implementation UIDevice (ProcessesAdditions)- (NSArray *)runningProcesses {        int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0};        size_t miblen = 4;        size_t size;        int st = sysctl(mib, miblen, NULL, &size, NULL, 0);        struct kinfo_proc * process = NULL;        struct kinfo_proc * newprocess = NULL;    do {        size += size / 10;        newprocess = realloc(process, size);        if (!newprocess){            if (process){                free(process);            }            return nil;        }        process = newprocess;        st = sysctl(mib, miblen, process, &size, NULL, 0);    } while (st == -1 && errno == ENOMEM);        if (st == 0){                if (size % sizeof(struct kinfo_proc) == 0){                        int nprocess = size / sizeof(struct kinfo_proc);                        if (nprocess){                                NSMutableArray * array = [[NSMutableArray alloc] init];                                for (int i = nprocess - 1; i >= 0; i--){                                        NSString * processID = [[NSString alloc] initWithFormat:@"%d", process[i].kp_proc.p_pid];                                        NSString * processName = [[NSString alloc] initWithFormat:@"%s", process[i].kp_proc.p_comm];                                        NSDictionary * dict = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:processID, processName, nil]                                                                                                                                                 forKeys:[NSArray arrayWithObjects:@"ProcessID", @"ProcessName", nil]];                                        [processID release];                                        [processName release];                                        [array addObject:dict];                                        [dict release];                                }                                free(process);                                return [array autorelease];                        }                }        }        return nil;}@end// Example usage.NSArray * processes = [[UIDevice currentDevice] runningProcesses];for (NSDictionary * dict in processes){        NSLog(@"%@ - %@", [dict objectForKey:@"ProcessID"], [dict objectForKey:@"ProcessName"]);}

私有API

最好用的就是私有API了,但是有审核不通过的风险

#include <objc/runtime.h>Class LSApplicationWorkspace_class = objc_getClass("LSApplicationWorkspace");NSObject* workspace = [LSApplicationWorkspace_class performSelector:@selector(defaultWorkspace)];NSLog(@"apps: %@", [workspace performSelector:@selector(allApplications)]);
0 0
原创粉丝点击