iOS實現一個應用去handle另外一個應用(一個App打開另一個App)

1、首先咱們建立兩個用於測試的App項目 (我這裏以App0-A 和 App-B 爲例)app

 

2、打開工程,設置工程的InfoPlist:添加URL Typeside

  給你的App設置一個URL Schemes(明明以你的App或者工程名來命名) 這樣就能讓其它應用識別獲得App測試

  ps:咱們這裏用App_B 去 handle 咱們的App_A,故咱們App_A就要設置URL Schemesurl

  

 

3、在App_B中,設置一個按鈕,實現點擊後handle出咱們的App_Aspa

- (void)viewDidLoad {
    [super viewDidLoad];
    UIButton *App_B_Button = [UIButton buttonWithType:UIButtonTypeCustom];
    App_B_Button.frame = CGRectMake(100, 100, 100, 50);
    App_B_Button.backgroundColor = [UIColor purpleColor];
    [App_B_Button setTitle:@"App_B" forState:UIControlStateNormal];
    [App_B_Button addTarget:self action:@selector(app_B:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:App_B_Button];
}


-(void)app_B:(UIButton *)buttonB
{
    NSURL *url = [NSURL URLWithString:@"appA://"];
    [[UIApplication sharedApplication] openURL:url];
}

點擊按鈕後:代理

這樣就能實現App之間的跳轉的功能了。code

注意:打開應用App-A的過程當中,App-A有兩種狀態。orm

第一種狀態:App_A並無啓動,那麼會啓動App_A。並調用下面的方法。blog

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    return YES;
}

第二種狀態:此時B已經啓動了,可是在後臺運行,這個時候不會調用該方法ci

  

4、若想實現App跳轉的同時進行傳值,只需實現application的代理方法

//當應用程序被其餘程序打開的時候會調用這個方法,在該方法中能夠實現兩個應用程序間的數據局傳遞
//經過這個代理方法能夠攔截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; }
相關文章
相關標籤/搜索