標籤:style class blog code http color
假設有兩個App,項目名分別是SampleA和SampleB,需要在SampleA裡點擊一個Button來啟動SampleB,並傳遞一個字串。具體實現步驟如下:
1. 在SampleB的info.plist檔案裡新增一個URL Schemes,並指定一個字串,這個字串就是調用App的連結名稱:
2. 在SampleA的按鈕點擊操作裡執行下面代碼:
- (IBAction)openClickHandler:(id)sender{ [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"myapp23://time_is_money"]];}
3. 在SampleB的AppDelegate類裡,調用系統事件處理傳遞過來的URL:
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{ NSString *strInfo = [[url host] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:strInfo delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alertView show]; return YES;}
總結:
1. 通過openURL的方式啟動其它App時,地址格式為 @“URL Scheme://URL identifier”,其中URL identifier是可選的。
2. URL identifier不能含有空格和問號。
3. apple並不支援這種做法,實際產品開發中慎用。