Wie kann ich die Facebook- und Instagram-App öffnen, indem Sie in Swift
auf eine Schaltfläche tippen? Einige Apps leiten zur Facebook-App weiter und öffnen eine bestimmte Seite. Wie kann ich dasselbe tun?
Ich habe es gefunden:
var url = NSURL(string: "itms://iTunes.Apple.com/de/app/x-gift/id839686104?mt=8&uo=4")
if UIApplication.sharedApplication().canOpenURL(url!) {
UIApplication.sharedApplication().openURL(url!)
}
aber ich muss die app URL
kennen. Andere Beispiele waren in ObjectiveC
, die ich nicht kenne = /
Schauen Sie sich diese Links an, es kann Ihnen helfen:
https://instagram.com/developer/mobile-sharing/iphone-hooks/
http://wiki.akosma.com/IPhone_URL_Schemes
Öffne einen Facebook-Link über die native Facebook-App auf iOS
Ansonsten gibt es ein kurzes Beispiel mit Instagram, um ein bestimmtes Profil (Spitzname: johndoe) hier zu öffnen:
var instagramHooks = "instagram://user?username=johndoe"
var instagramUrl = NSURL(string: instagramHooks)
if UIApplication.sharedApplication().canOpenURL(instagramUrl!) {
UIApplication.sharedApplication().openURL(instagramUrl!)
} else {
//redirect to safari because the user doesn't have Instagram
UIApplication.sharedApplication().openURL(NSURL(string: "http://instagram.com/")!)
}
Update für Swift 4 und iOS 10+
OK, es gibt zwei einfache Schritte, um dies in Swift 3 zu erreichen:
Zuerst müssen Sie Info.plist
ändern, um instagram
und facebook
mit LSApplicationQueriesSchemes
aufzulisten. Öffnen Sie einfach Info.plist
als Quellcode und fügen Sie diesen ein:
<key>LSApplicationQueriesSchemes</key>
<array>
<string>instagram</string>
<string>fb</string>
</array>
Danach können Sie instagram
- und facebook
-Apps mit instagram://
und fb://
öffnen. Hier ist ein vollständiger Code für instagram. Sie können dasselbe für Facebook tun. Sie können diesen Code mit jeder Schaltfläche verknüpfen, die Sie als Aktion haben:
@IBAction func InstagramAction() {
let Username = "instagram" // Your Instagram Username here
let appURL = URL(string: "instagram://user?username=\(Username)")!
let application = UIApplication.shared
if application.canOpenURL(appURL) {
application.open(appURL)
} else {
// if Instagram app is not installed, open URL inside Safari
let webURL = URL(string: "https://instagram.com/\(Username)")!
application.open(webURL)
}
}
Für facebook
können Sie diesen Code verwenden:
let appURL = URL(string: "fb://profile/\(Username)")!
Sie müssen eigentlich keine Web- und App-URL mehr verwenden. Die Web-URL wird automatisch in der App geöffnet, wenn der Benutzer über sie verfügt. Instagram oder andere Apps implementieren dies an ihrem Ende als Universal Link
Swift 4
func openInstagram(instagramHandle: String) {
guard let url = URL(string: "https://instagram.com/\(instagramHandle)") else { return }
if UIApplication.shared.canOpenURL(url) {
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(url)
}
}
}
In Swift 4:
Ändern Sie einfach appURL und webURL :
Twitter://user?screen_name=\(screenName)
instagram://user?screen_name=\(screenName)
facebook://user?screen_name=\(screenName)
- 'openURL' wurde in iOS 10.0 nicht mehr unterstützt:
let screenName = "imrankst1221"
let appURL = NSURL(string: "instagram://user?screen_name=\(screenName)")!
let webURL = NSURL(string: "https://Twitter.com/\(screenName)")!
if UIApplication.shared.canOpenURL(appURL as URL) {
if #available(iOS 10.0, *) {
UIApplication.shared.open(appURL as URL, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(appURL as URL)
}
} else {
//redirect to safari because the user doesn't have Instagram
if #available(iOS 10.0, *) {
UIApplication.shared.open(webURL as URL, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(webURL as URL)
}
}
Basierend auf der akzeptierten Antwort ist dies der Weg, mit Swift 4 dies eleganter zu machen
UIApplication.tryURL([
"instagram://user?username=johndoe", // App
"https://www.instagram.com/johndoe/" // Website if app fails
])
Denken Sie daran, das Schema hinzuzufügen, damit die App geöffnet werden kann. Auch wenn Sie vergessen, dass sich Instagram in Safari öffnet.
Die tryUrl ist eine Erweiterung, die der hier dargestellten ähnelt: https://stackoverflow.com/a/29376811/704803