长期以来,iOS 不允許 App 直接啟動另一個 App。後來有人用 class-dump 挖出私有 API,找到 launchApplicationWithIdentifier:suspended: 這個方法可以辦到:
NSString *identifier = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleIdentifier"];
[[UIApplication sharedApplication] launchApplicationWithIdentifier:identifier suspended:NO];
但私有 API 終究不是長久之計——你永遠拿不到 App Store 的審核通過。所幸,對於「SDK 需要開啟宿主 App 或其他 App」這類需求,其實有官方且正當的解法:openURL:。
除了開網頁,URL Scheme 還能操作地圖搜尋、寄信、撥號、傳簡訊、跳 App Store。
// 開啟地圖搜尋
- (IBAction)openMaps {
NSString *addressText = @"1 Infinite Loop, Cupertino, CA 95014";
addressText = [addressText stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSString *urlText = [NSString stringWithFormat:@"https://maps.google.com/maps?q=%@", addressText];
NSLog(@"%@", urlText);
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlText]];
}
// 寄信
- (IBAction)openEmail {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto://devprograms@apple.com"]];
}
// 撥打電話
- (IBAction)openPhone {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://8004664411"]];
}
// 傳送簡訊
- (IBAction)openSms {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms://466453"]];
}
// 開啟瀏覽器
- (IBAction)openBrowser {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://itunesconnect.apple.com"]];
}
步驟其實很簡單:開啟 Info.plist,新增一項 URL types,展開後編輯 Item 0,把 URL identifier 改為你的 Bundle 標識,再展開 URL Schemes,在 Item 0 填入自訂的 scheme 名稱(例如 myapp)。設定完成後,其他 App 就能透過 myapp:// 開啟你的程式。
NSURL *url = [NSURL URLWithString:@"myapp:"];
[[UIApplication sharedApplication] openURL:url];
SDK 有個先天的優勢:每一個接入方都有一個唯一的 AppId。我們可以要求第三方開發者在自己的 Info.plist 中,把 URL Scheme 定義成「前綴 + AppId」的形式,例如某應用配到 AppId 為 111122223333,就請他設定 URL Scheme 為 NDSDK111122223333。這樣 SDK 內部就能精準判斷「這個 AppId 的應用是否已安裝」。
更進一步,canOpenURL: 可以直接查詢系統中是否有人能處理這個 URL:
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}
不論對方是 ipa 還是 pxl 安裝的版本,這個判斷都成立。
如果選擇走這條路,就必須在文件裡寫清楚規則。但現實是:開發者不一定會讀文件。因此實務上更可靠的做法是在 SDK 啟動時主動檢查,把設定錯誤提早暴露在開發階段。
檢查三件事:使用者是否配置了 AppId、Info.plist 中是否正確設定 CFBundleURLSchemes、這個 URL 是否真的打得開。
// 1. 檢查 App ID 是否設定
if (!kAppId) {
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:@"Setup Error"
message:@"Missing app ID. You cannot run the app until you provide this in the code."
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
[alertView release];
}
else {
// 2. 檢查 URL scheme 是否寫進 plist
NSString *url = [NSString stringWithFormat:@"fb%@://authorize", kAppId];
BOOL bSchemeInPlist = NO;
NSArray *aBundleURLTypes = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleURLTypes"];
if ([aBundleURLTypes isKindOfClass:[NSArray class]] && [aBundleURLTypes count] > 0) {
NSDictionary *aBundleURLTypes0 = [aBundleURLTypes objectAtIndex:0];
if ([aBundleURLTypes0 isKindOfClass:[NSDictionary class]]) {
NSArray *aBundleURLSchemes = [aBundleURLTypes0 objectForKey:@"CFBundleURLSchemes"];
if ([aBundleURLSchemes isKindOfClass:[NSArray class]] && [aBundleURLSchemes count] > 0) {
NSString *scheme = [aBundleURLSchemes objectAtIndex:0];
if ([scheme isKindOfClass:[NSString class]] && [url hasPrefix:scheme]) {
bSchemeInPlist = YES;
}
}
}
}
// 3. 檢查是否真的能開啟
BOOL bCanOpenUrl = [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:url]];
if (!bSchemeInPlist || !bCanOpenUrl) {
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:@"Setup Error"
message:@"Invalid or missing URL scheme. You cannot run the app until you set up a valid URL scheme in your .plist."
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
[alertView release];
}
}
上面這套寫法在 iOS 9 之後必須補上兩處設定,否則會失效:
第一,canOpenURL: 需要白名單。 在 Info.plist 加入 LSApplicationQueriesSchemes 陣列,把你要查詢的 scheme 逐一列出,否則 canOpenURL 一律回傳 NO。
第二,openURL: 已改為 completion handler 版本。 舊寫法在 iOS 10 被標記為 deprecated:
[application openURL:url options:@{} completionHandler:^(BOOL success) {
if (success) {
NSLog(@"已成功開啟");
}
}];
另外,若 App 在 iOS 9 之後被加入了 ATS 限制,透過 http 開啟外部網頁也可能被攔,記得確認伺服器支援 https。