原文
比較詳細的介紹[包含了如何查找 App 的 URL Scheme]數組
iPhone / iOS SDK 特性之一是能夠將 iOS Application 綁定到一個自定義的 URL Scheme 上,在瀏覽器上或其餘應用中能夠經過該 URL Scheme 來啓動應用。瀏覽器
1. Info.plist -> 添加 URL Types 行。 2. URL identifier 用來做爲自定義 URL Scheme 的惟一標識。 3. Item 0 -> 添加 URL Schemes 行 -> 填寫自定義 URL Scheme
注意:
URL Schemes 是一個數組,容許應用定義多個 URL scheme。如分享時添加微信、QQ、微博的 URL scheme。上圖獲得的 URL Scheme : "imcoktestextension://"微信
// 普通使用(直接打開應用) NSString *customURL = @"imocktestextension://"; NSURL *openURL = [NSURL URLWithString:customURL]; if([[UIApplication sharedApplication] canOpenURL:openURL]) { [[UIApplication sharedApplication] openURL:openURL]; } else{ NSLog(@"Failed to open URL"); }
// 普通使用(打開應用的同時傳遞信息) NSString *customURL =@"imocktestextension://info"; NSURL *openURL = [NSURL URLWithString:customURL]; if([[UIApplication sharedApplication] canOpenURL:openURL]) { [[UIApplication sharedApplication] openURL:openURL]; } else{ NSLog(@"Failed to open URL"); }
// 被打開應用監聽 URL Scheme NS_AVAILABLE_IOS(9_0) iOS 9 以前是另外一個接口 - (BOOL) application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options { NSString* prefix = @"imcoktestextension://"; if ([[url absoluteString] rangeOfString:prefix].location != NSNotFound) { NSString* action = [[url absoluteString] substringFromIndex:prefix.length]; if ([action isEqualToString:@"ShowImage"]) { // 展現圖片頁 NSLog(@"我想去 ShowImage 頁面"); [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationShowImage object:nil]; } } return YES; }
注意:
NSURL 做爲從一個應用調用另外一個的基礎,須要遵循 RFC 1808(Relative Uniform Resource Locators)標準。因此經常使用的基於網頁內容的 URL 格式也適用於 URL Scheme。好比 imocktestextension://info 能夠看成一個普通 URL 進行拆分,也能夠直接使用 Safari 打開(前提手機裝有 App)。app