1、建立兩個項目:AppOne,AppTwo;
2、分別在其屬性列表中添加如下:
AppOne:
AppTwo:
AppOne的 viewController.m 中添加代碼:
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSURL *url = [NSURLURLWithString:@"AppTwo:"];
if ([[UIApplicationsharedApplication] canOpenURL:url]) // 判斷裝置是否安裝了AppTwo
{
NSLog(@"canOpenURL");
[[UIApplication sharedApplication] openURL:url]; // 開啟AppTwo應用, 如果你想攜帶參數: NSURL *url = [NSURLURLWithString:@"AppTwo://你想攜帶的代數"];
} else
{
NSLog(@"can not OpenURL");
}
}
AppTwo的 viewController.m 中添加代碼:
// AppDelegate裡面接收url
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options
{
NSLog(@"--%@", url);
return YES;
}
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"touchesBegan");
NSURL *url = [NSURLURLWithString:@"AppOne:"];
if ([[UIApplicationsharedApplication] canOpenURL:url])
{
NSLog(@"canOpenURL");
[[UIApplication sharedApplication] openURL:url];
} else
{
NSLog(@"can not OpenURL");
}
}