1. 程式人生 > >iOS巔峰之兩個App之間相互呼叫並攜帶引數傳值

iOS巔峰之兩個App之間相互呼叫並攜帶引數傳值

1、新建兩個專案:AppOne,AppTwo;

2、分別在其屬性列表中新增如下:

AppOne:  


AppTwo:


AppOne的 viewController.m 中新增程式碼:

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

NSURL *url  = [NSURLURLWithString:@"AppTwo:"];

if ([[UIApplicationsharedApplicationcanOpenURL:url])  // 判斷裝置是否安裝了AppTwo

    {

NSLog(@"canOpenURL");

        [[UIApplicationsharedApplicationopenURL: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 ([[UIApplicationsharedApplicationcanOpenURL:url])

    {

NSLog(@"canOpenURL");

        [[UIApplicationsharedApplication

openURL:url];

    } else

    {

NSLog(@"can not OpenURL");

    }

}