iOS應用之間的跳轉與資料傳遞

黑暗森林的歌者發表於2018-02-26

在開發的時候遇到需要從其他APP呼叫自己的APP的需求,比如從Safari中開啟APP,並且傳遞一些資訊的需要

1、首先設定自己的URL types

開啟專案中的工程檔案,開啟info選項,在下面的URL types中新增自己APP的Identifier,並且自定義一個自己的scheme,如圖:

Paste_Image.png
舉個例子,設定自己的APP的scheme為 myApp;

2、其他應用的呼叫

其他的用如果想要調起自己的APP,需要執行如下類似程式碼

NSString *paramStr = [NSString stringWithFormat:@"myApp://userInfo....."]; 
NSURL *url = [NSURL URLWithString:[paramStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
 [[UIApplication sharedApplication] openURL:url];
複製程式碼

myAPP://後面就是需要傳進來的值

3、接受被調起並且獲取傳來的值

在appdelegate中新增如下方法

-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
     NSString *tmpUrl = [url absoluteString];
   
    return YES;
}
複製程式碼

url就是 myApp後面的 userInfo.....

sourceApplication主動調起的應用的bundle id

在自己的應用中設定對應的引數來接收資料。需要傳遞的資料資訊可以拼接成url的字串

相關文章