IOS打开其他应用、以及被其他应用打开

1、打开其他应用

     appURLStr = "cwork://app_id?title=xxx&content=xxx"

     [[UIApplication sharedApplication] openURL:[NSURL URLWithString:appURLStr]];

  •      需要对方应用在Info.pist中配置:

     如此便能将对方应用调起。

 

 

2、被其他应用打开

     (1)同上图所示,需要自己在info.plist中配置,别人掉起自己,其中identifer没什么用,主要是URL Schemes,它会用来拼接被调起的url:如:cwork://xx-id?parame1 = "1" & parame2="2"

       a、程序已经运行时,会唤起appdelegate中的代理:

#define CWorkURLScheml      @"cwork://"
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
    if (!url){
        return NO;
    }
    
    NSString *URLString = [url absoluteString];
    监听被谁调起
    if ([URLString rangeOfString:CWorkURLScheml].length > 0) {
    }
    return YES;
}

     b、 程序首次被启动时:    

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  
    NSURL *aLaunchOptionsURL = [launchOptions objectForKey:UIApplicationLaunchOptionsURLKey];
    // 被其他应用唤醒监听,解析
    if ([aLaunchOptionsURL.absoluteString hasPrefix:CWorkURLScheml]) {
    }
	return YES;
}

  

(2) 然后url中带有参数,可以对url进行解析,以"?"为分隔符,对两边参数进行解析

+ (NSDictionary *)urlPropertyValue:(NSString *)aURLStr
{
    NSArray *aList = [aURLStr componentsSeparatedByString:@"?"];
    if (aList.count < 2) {
        return nil;
    }
    NSMutableDictionary *aDict = [NSMutableDictionary dictionary];
    NSString *rootPath = [aList objectAtIndex:0];
    [aDict setObject:rootPath forKey:@"rootPath"];
    NSString *aValueStr = [aList objectAtIndex:1];
    NSArray *aList1 = [aValueStr componentsSeparatedByString:@"&"];
    for (NSString *aStr in aList1) {
        NSArray *l = [aStr componentsSeparatedByString:@"="];
        if (l.count == 2) {
            NSString *k = [l objectAtIndex:0];
            NSString *v = [l objectAtIndex:1];
            [aDict setObject:v forKey:k];
        }
    }
    return aDict;
}

  

原文地址:https://www.cnblogs.com/yqlog/p/4935462.html