如何通过点击Swift中的button来打开fb和instagram应用程序
如何通过点击Swift中的button来打开Facebook和Instagram应用程序? 在某些应用程序中,通过点击redirect到Facebook应用程序并打开某个页面。 我如何在Swift中做同样的事情?
我find了,
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!) }
但在这里我必须知道一个应用程序的URL。 而其他的例子是在ObjC,我根本不知道= /
看看这些链接,它可以帮助你:
https://instagram.com/developer/mobile-sharing/iphone-hooks/
http://wiki.akosma.com/IPhone_URL_Schemes
在iOS上通过本地Facebook应用程序打开Facebook链接
否则,Instagram有一个简单的例子,在这里打开一个特定的configuration文件(昵称:johndoe)
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/")!) }
在迅速3;
首先,你应该在你的Info.plist上添加这个
比你可以使用这个代码;
let instagramUrl = URL(string: "instagram://app") UIApplication.shared.canOpenURL(instagramUrl!) UIApplication.shared.open(instagramUrl!, options: [:], completionHandler: nil)
Swift 3和iOS 10+的更新
好的,在Swift 3中有两个简单的步骤来实现这一点:
首先,您必须修改Info.plist
以使用LSApplicationQueriesSchemes
列出instagram
和facebook
。 简单地打开Info.plist
作为源代码,并粘贴:
<key>LSApplicationQueriesSchemes</key> <array> <string>instagram</string> <string>fb</string> </array>
之后,您可以使用instagram://
和fb://
打开instagram
和facebook
应用程序。 这里是一个完整的代码为instagram,你可以做同样的脸谱,你可以链接这个代码,你有任何button作为一个行动:
@IBAction func InstagramAction() { let Username = "instagram" // Your Instagram Username here let appURL = NSURL(string: "instagram://user?username=\(Username)")! let webURL = NSURL(string: "https://instagram.com/\(Username)")! let application = UIApplication.shared if application.canOpenURL(appURL as URL) { application.open(appURL as URL) } else { // if Instagram app is not installed, open URL inside Safari application.open(webURL as URL) } }
对于facebook
,你可以使用这个代码:
let appURL = NSURL(string: "fb://profile/\(Username)")!