IOS之URL Scheme的使用(可以在app之间调用传递数据)
iOS实现一个应用去handle另一个应用(一个App打开另外一个App)
1.我们平时进行授权的时候,就是去打开第三方并且把我们写的那个urlScheme传给第三方(为了授权成功后,第三方能找到我们应用的urlScheme),然后打开第三方应用,第三方应用再根据我们传过去的urlScheme,去打开我们的应用,并且传值给我们
2.打开应用是NSURL *url = [NSURL URLWithString:@"callAPP://callapp://str==123456"];
[[UIApplication sharedApplication] openURL:url];
获取到的url.scheme为"TestB",url.host为callsuccess,
url.scheme:callAPP
url.host:callapp
url.resourceSpecifier://callapp://str==123456
url.absoluteString:callAPP://callapp://str==123456
absoluteURL:callAPP://callapp://str==123456
host:callapp
path://str==123456
relativePath://str==123456
2.//当应用程序被其他程序打开的时候会调用这个方法,在该方法中可以实现两个应用程序间的数据局传递
//通过这个代理方法可以拦截url
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
NSString *urlStr = [url absoluteString];
if ([urlStr hasPrefix:@"AppA://"]) {
urlStr = [urlStr stringByReplacingOccurrencesOfString:@"AppA://" withString:@""];//参数就在url,传值也在里面
}
return NO;
}
3.- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
NSString *handleUrl = [url absoluteString];
if ([handleUrl isEqualToString:@"TestBApp://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;
}