2010-08-18 122 views
33

我正在開發一個iPhone應用程序,它將在企業中安裝少量的第三方應用程序。我有關於捆綁ID的信息。有沒有辦法通過使用一些系統API來檢查應用程序是否已經安裝?目前應用程序再次安裝,覆蓋當前安裝。我需要防止這一些如何。 (如果應用程序已安裝,Apple的AppStore應用程序將禁用安裝選項。)如何以編程方式檢查是否安裝了應用程序?

+2

可能重複的[如何檢查在iPhone設備中安裝的應用程序](http://stackoverflow.com/questions/3243567/how-to-check-installed-application-in-iphone-device) – 2010-08-18 14:25:58

+0

也許這個wiki會幫助你也是:http://wiki.akosma.com/IPhone_URL_Schemes 大部分url方案的問題是,如果你沒有你要調用的應用程序的用戶標識,你不能傳遞任何數據。 – 2013-10-09 19:34:42

+0

http://stackoverflow.com/questions/32643522/fbsdksharedialog-of-facebook-sdk-is-not-working-on-ios9/39159507#39159507 – TharakaNirmana 2016-08-26 06:17:30

回答

57

我認爲這是不可能的,但如果應用程序註冊uri方案,您可以測試。

對於facebook應用程序,URI方案是例如fb://。您可以在您的應用程序的info.plist中註冊。 [UIApplication canOpenURL:url]會告訴你某個網址是否會打開。因此,測試fb://是否會打開,將顯示已安裝應用程序,其中已註冊fb:// - 這是Facebook應用程序的一個好提示。

// check whether facebook is (likely to be) installed or not 
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"fb://"]]) { 
    // Safe to launch the facebook app 
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"fb://profile/200538917420"]]; 
} 
+0

「但如果應用程序註冊uri計劃,你可以測試」:可以你請稍微解釋一下? – attisof 2010-08-18 12:58:59

31

下面就來測試,如果Facebook的應用程序安裝

if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"fb://"]]) { 
    // Facebook app is installed 
} 
22

對於任何試圖與iOS做到這9 /斯威夫特2個例子:

首先,你需要'白名單」中加入以下到您Info.plist文件的URL(安全功能 - 見Leo Natan's answer):

<key>LSApplicationQueriesSchemes</key> 
<array> 
    <string>fb</string> 
</array> 

之後,你可以嚮應用程序是否可用,並有註冊計劃:

guard UIApplication.sharedApplication().canOpenURL(NSURL(string: "fb://")!) else { 
    NSLog("No Facebook? You're a better man than I am, Charlie Brown.") 
    return 
} 
+1

它爲我工作,謝謝 – 2015-10-09 07:49:48

1

當談到社交網絡時,最好檢查多個方案。 (因爲方案 'FB' 是過時的用於IOS9 SDK例如):

NSArray* fbSchemes = @[ 
    @"fbapi://", @"fb-messenger-api://", @"fbauth2://", @"fbshareextension://"]; 
BOOL isInstalled = false; 

for (NSString* fbScheme in fbSchemes) { 
    isInstalled = [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:fbScheme]]; 
    if(isInstalled) break; 
} 

if (!isInstalled) { 
    // code 
    return; 
} 

當然Info.plist中也應包含所有必要的方案:

<key>LSApplicationQueriesSchemes</key> 
<array> 
    <string>fbapi</string> 
    <string>fb-messenger-api</string> 
    <string>fbauth2</string> 
    <string>fbshareextension</string> 
</array> 
3

夫特3.1,3.2斯威夫特,夫特4

if let urlFromStr = URL(string: "fb://") { 
    if UIApplication.shared.canOpenURL(urlFromStr) { 
     if #available(iOS 10.0, *) { 
      UIApplication.shared.open(urlFromStr, options: [:], completionHandler: nil) 
     } else { 
      UIApplication.shared.openURL(urlFromStr) 
     } 
    } 
} 

在Info.plist中添加這些:

<key>LSApplicationQueriesSchemes</key> 
<array> 
    <string>fb</string> 
</array> 
相關問題