2014-09-19 2 views
15

Ich erstelle eine App, und ich habe ein Banner, das meine andere App fördert. Dies ist mein Code:Starten App Store von App in Swift

var barsButton : UIButton = UIButton(frame: CGRectMake((self.view.bounds.width/2) - 51, self.view.bounds.height - 100, 102, 30)) 
barsButton.setImage(UIImage(named: "Bars Icon 2.png"), forState: .Normal) 
barsButton.addTarget(self, action: "openBarsLink", forControlEvents: UIControlEvents.TouchUpInside) 

func openBarsLink() { 
    var barsLink : String = "itms-apps:https://itunes.apple.com/app/bars/id706081574?mt=8" 
    UIApplication.sharedApplication().openURL(NSURL.URLWithString(barsLink)) 
} 

Wenn jedoch der Benutzer den Knopf drückt, es braucht sie nur in den App Store, und nicht die spezifische Seite für meine Anwendung. Was mache ich falsch?

Antwort

22

Sie haben zu viele Protokolle in Ihrer URL. Befreien Sie sich von https: so die URL liest

itms-apps://itunes.apple.com/app/bars/id706081574

1

Link, den Sie versuchen, zu öffnen, ist nicht gültig - entfernen https: Schema von ihm (oder itms: - aber ich schlage vor, erste Option, um zu verhindern Umleitungen)

15

gerade ältere Antworten folgende konnte ich es nicht, funktioniert so hier poste ich meine komplette Lösung:

var url = NSURL(string: "itms-apps://itunes.apple.com/app/bars/id706081574") 
if UIApplication.sharedApplication().canOpenURL(url!) { 
    UIApplication.sharedApplication().openURL(url!) 
} 
6

Verwenden sie einfach die kurze „itms: //“.

Für Swift 3 das ist das Snippet:

UIApplication.shared.openURL(URL(string: "itms://itunes.apple.com/app/id" + appStoreAppID)!) 

Ich hoffe, das jemand hilft.

Prost.

P.S. @Eric Aya war vor der Zeit :)

+0

@Eric D, danke. Allerdings Ich benutze den XCode 8, Beta 3 und es geht nur mit "shared()" – Sasho

+0

Ah, tut mir leid, mein schlechtes. Ich war mir ziemlich sicher, dass sich das auch geändert hat. Ich denke, sie sind nicht fertig mit der Änderung der APIs. :) – Moritz

3

Swift 3 - XCode 8.2.1

UIApplication.shared.openURL(URL(string: "itms-apps://itunes.apple.com/app/id" + appStoreAppID)!) 
3

ich dieses Problem hatte, aber dieser Code funktioniert nur auf das Telefon nicht Simulator. So überprüfen Sie diesen Code:

if let url = URL(string: "itms-apps://itunes.apple.com/app/id" + APP_ID), 
    UIApplication.shared.canOpenURL(url){ 
    UIApplication.shared.openURL(url) 
}else{ 
    //Just check it on phone not simulator! 
    print("Can not open") 
} 
+0

nice call für "öffnen Sie es auf einem echten Gerät", dachte ich, ich würde verrückt werden. – thexande

1

Wie openURL ist veraltet von iOS 10 bei Temperaturen unter Code:

UIApplication.shared.open((URL(string: "itms://itunes.apple.com/app/" + appStoreAppID)!), options:[:], completionHandler: nil)