Scheme 配置

在其他應用里就可以用以下語句啟動你的app
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"BProject://"]];```
當然你也可以
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"AProject://"]];```
在自定義了 URL scheme 的應用中,app delegate 必須實現(xiàn)以下方法:
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation```
例如,假設我們使用以下的 URL scheme,我們可以像這樣創(chuàng)建一個 URL:
NSString *customURL = @"BProject://?action=play&data=12345";
在 web 開發(fā)中,字符串 ?action=play&data=12345 被稱作查詢詢串(query string)。
在被調用(設置了自定義 URL)的應用的 app delegate 中,獲取參數(shù)的代碼如下:
-
(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
額外功能
如果你想啟動一個頁面那么,此時要考慮你的應用是否已經(jīng)啟動,可以如下判斷使用:
- (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系統(tǒng),由于iOS9為了安全提出了白名單概念,你必須在TestAAPp的info.plist里面配置以下信息

白名單上限只有50個。