iOS巔峰之兩個App之間相互呼叫並攜帶引數傳值
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");
[[UIApplicationsharedApplication] 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");
[[UIApplicationsharedApplication]
} else
{
NSLog(@"can not OpenURL");
}
}