How to open fb and instagram app by tapping on button in Swift
Take a look at these links, it can help you:
https://instagram.com/developer/mobile-sharing/iphone-hooks/
http://wiki.akosma.com/IPhone_URL_Schemes
Open a facebook link by native Facebook app on iOS
Otherwise, there is a quick example with Instagram for opening a specific profile (nickname: johndoe) here:
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/")!)
}
redirect to the facebook app when I click share button in swift webview
SOLUTION:
Have a look at this: How to open fb and instagram app by tapping on button in Swift
1st links on Google. Or are you looking for something different ?
EDIT: You asked me how to make the button trigger an event, here' how:
1) Open the Assistant editor by using ALT-CMD-ENTER.
2) Create an IBAction from your button by CTRL dragging from the button to your code:
3) Put the necessary code in your IBAction, for example:
@IBAction func Fb(sender: UIButton) {
if SLComposeViewController.isAvailableForServiceType(SLServiceTypeFacebook) {
let fbShare:SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
self.presentViewController(fbShare, animated: true, completion: nil)
} else {
let alert = UIAlertController(title: "Accounts", message: "Please login to a Facebook account to share.", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
}
how to open instagram app on button click
You can open instagram app by username like,
NSURL *instagramURL = [NSURL URLWithString:@"instagram://user?username=USERNAME"];
if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) {
[[UIApplication sharedApplication] openURL:instagramURL];
}
You can refer iPhone Hooks of Instagram for more ways and details about api!!!
Update :
Replace below line,
NSString *instagramURL = @"instagram://app";
with
NSURL *instagramURL = [NSURL URLWithString:@"instagram://app"];
You are assigning directly string as url!!
Xcode swift link to facebook page
The problem is with the format of your Facebook url so notice the format. I use this extension to open urls. You provide it with an array of urls in the order you want them to try to be opened and it tries the first one first and if it fails it goes to the second one and so on:
extension UIApplication {
class func tryURL(urls: [String]) {
let application = UIApplication.sharedApplication()
for url in urls {
if application.canOpenURL(NSURL(string: url)!) {
application.openURL(NSURL(string: url)!)
return
}
}
}
}
And for use:
UIApplication.tryURL([
"fb://profile/116374146706", // App
"http://www.facebook.com/116374146706" // Website if app fails
])
[Update] for Swift 4:
extension UIApplication {
class func tryURL(urls: [String]) {
let application = UIApplication.shared
for url in urls {
if application.canOpenURL(URL(string: url)!) {
application.openURL(URL(string: url)!)
return
}
}
}
}
And then:
UIApplication.tryURL(urls: [
"fb://profile/116374146706", // App
"http://www.facebook.com/116374146706" // Website if app fails
])
[Update] for iOS 10 / Swift 5
extension UIApplication {
class func tryURL(urls: [String]) {
let application = UIApplication.shared
for url in urls {
if application.canOpenURL(URL(string: url)!) {
if #available(iOS 10.0, *) {
application.open(URL(string: url)!, options: [:], completionHandler: nil)
}
else {
application.openURL(URL(string: url)!)
}
return
}
}
}
}
Open link to Facebook page from iOS app
You have to use the facebook ID for the given page rather than the vanity URL. To get the pages ID, enter your page's vanity name at the following URL and copy the value for "id":
https://graph.facebook.com/yourappspage
Once you have the id, you can set up the method to check if FB is installed using the canOpenURL method and serve the appropriate content for both states:
NSURL *facebookURL = [NSURL URLWithString:@"fb://profile/113810631976867"];
if ([[UIApplication sharedApplication] canOpenURL:facebookURL]) {
[[UIApplication sharedApplication] openURL:facebookURL];
} else {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://facebook.com"]];
}
Related Topics
Screenshot for Avplayer and Video
How to Compare Two Nsdate Objects in Objective C
Programmatically Switching Between Tabs Within Swift
Calculating Bearing Between Two Cllocation Points in Swift
How Much Delay of iOS Push Notification
Xctest and Asynchronous Testing in Xcode 6
How to Maintain Presenting View Controller's Orientation When Dismissing Modal View Controller
Button in Uitableviewcell Not Responding Under iOS 7
Changed Project Name in Xcode Causing Naming Error
Wkwebview in Interface Builder
Xcode - Error Creating Lldb Target
Module Compiled with Swift 4.2.1 Cannot Be Imported by the Swift 5.0 Compiler