iOS-调用Custom URL Scheme

Scheme 配置


iOS-调用Custom URL Scheme_第1张图片
11.png

在其他应用里就可以用以下语句启动你的app

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"BProject://"]];```
当然你也可以

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"AProject://"]];```
在自定义了 URL scheme 的应用中,app delegate 必须实现以下方法:

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
                                       sourceApplication:(NSString *)sourceApplication
                                              annotation:(id)annotation```
例如,假设我们使用以下的 URL scheme,我们可以像这样创建一个 URL:

NSString *customURL = @"BProject://?action=play&data=12345";

在 web 开发中,字符串 ?action=play&data=12345 被称作查询询串(query string)。
在被调用(设置了自定义 URL)的应用的 app delegate 中,获取参数的代码如下:
  • (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
    sourceApplication:(NSString *)sourceApplication
    annotation:(id)annotation
    {
    NSLog(@"Calling Application Bundle ID: %@", sourceApplication);
    NSLog(@"URL scheme:%@", [url scheme]);
    NSLog(@"URL query: %@", [url query]);

    return YES;
    }```
    以上代码在应用被调用时的输出为:

Calling Application Bundle ID: com.hunantv.app
URL scheme:BProject
URL query: action=play&data=12345

额外功能
如果你想启动一个页面那么,此时要考虑你的应用是否已经启动,可以如下判断使用:

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url  
  sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{  
    NSString *handleUrl = [url absoluteString];  
    if ([handleUrl isEqualToString:@"BProject://callsuccess"]) {  
        return YES;  
    }else{  
        UINavigationController *vc = (UINavigationController *)_window.rootViewController;  
        if (vc == nil) {  
            PathViewController *controller = [[PathViewController alloc] initWithNibName:@"PathViewController" bundle:nil];  

            self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];  
            self.mUINavigationController = [[UINavigationController alloc] init];  

            [self.mUINavigationController pushViewController:controller animated:YES];  
            [self.window addSubview:self.mUINavigationController.view];  

            // Override point for customization after application launch.  
            self.window.backgroundColor = [UIColor whiteColor];  
            [self.window makeKeyAndVisible];  
        }  
        return YES;  
    }
  }```
也就是把appdelegate里面的didFinishLaunchingWithOptions初始化app的代码拷贝进去。此时会启动PathViewController这个页面。然后在这个页面里面可以添加一个返回按钮来返回到调用APP。

再次 在TestAAPp里面使用URl Scheme调起你的APP
  • (void)buttonPressed:(UIButton *)button
    {
    NSString *customURL = @"BProject://play/12345";

    if ([[UIApplication sharedApplication]
    canOpenURL:[NSURL URLWithString:customURL]])
    {
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:customURL]];
    }
    else
    {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"URL error"
    message:[NSString stringWithFormat:
    @"No custom URL defined for %@", customURL]
    delegate:self cancelButtonTitle:@"Ok"
    otherButtonTitles:nil];
    [alert show];
    }
    }```
    注意
    如果你是iOS9系统,由于iOS9为了安全提出了白名单概念,你必须在TestAAPp的info.plist里面配置以下信息

iOS-调用Custom URL Scheme_第2张图片
111.png

白名单上限只有50个。

你可能感兴趣的:(iOS-调用Custom URL Scheme)