I need to open facebook page in facebook app.
This is my code
let url : URL!
if (UIApplication.shared.canOpenURL(URL(string:"fb://")!)) {
//FIXME: Url is not working ✋
let pageName = "google"
// tried with fb://page?name=%# not worked
let urlString = String.init(format: "fb://page?id=%#",pageName!)
url = URL.init(string: urlString)
} else {
url = URL.init(string: "https://www.facebook.com/google")
}
if #available(iOS 10.0, *) {
UIApplication.shared.open(url!, completionHandler: nil)
} else {
UIApplication.shared.openURL(url!)
}
it perfectly works in safari browser but not in facebook app.
I have added the url schem in inof.plist
<key>LSApplicationQueriesSchemes</key>
<array>
<string>fb</string>
<string>fbapi</string>
<string>fb-messenger-api</string>
<string>fbauth2</string>
<string>fbshareextension</string>
</array>
Can anyone help me to get out this problem?
Thanks In Advance!!!!
try this code , make sure use facebook id from page not page name
check this How to find page Id
let url : URL!
if (UIApplication.shared.canOpenURL(URL(string:"fb://")!)) {
//FIXME: Url is not working ✋
let pageName = "104958162837"
// tried with fb://page?name=%# not worked
let urlString = String.init(format: "fb://profile/%#",pageName)
url = URL.init(string: urlString)
} else {
url = URL.init(string: "https://www.facebook.com/104958162837")
}
if #available(iOS 10.0, *) {
UIApplication.shared.open(url!, completionHandler: nil)
} else {
UIApplication.shared.openURL(url!)
}
And is it opening in a browser when you call UIApplication.shared.openURL(:)?
I didn't exactly understand your problem, but if I'm not mistaken, you've to put the Facebook App ID (it's the first thing that appears in your app in developers.facebook) in your URL Types.
This image should clarify what I'm trying to say:
Do not forget the "fb" String before your App ID.
EDIT: Now that I've understood your problem, I can't be sure that you will be able to solve this - as you can see all this answers-, they apparently don't seem to work anymore, as the URL scheme changed a couple of times, and the one that you're using is actually the last "answered/used" on the referenced answer.
I can almost confirm that there is no supported way to do what you're trying because of this answer in fb-sdk github issues.
Related
I am using universal link in iOS application. For the universal link I am using of Firebase dynamic link.
Universal link is working fine when app is installed. But when app is not installed then this link is opening in safari.
I am creating dynamic link (Universal link) programatically in Swift.
func createDynamicLinkForConsumer(){
guard let link = URL(string: "https://<Domain-Name>?consumerID=\(Auth.auth().currentUser?.uid ?? "")&type=consumerReferral") else { return }
let dynamicLinksDomainURIPrefix = "<appName>.page.link"
let linkBuilder = DynamicLinkComponents(link: link, domain: dynamicLinksDomainURIPrefix)
linkBuilder.options?.pathLength = .short
linkBuilder.iOSParameters = DynamicLinkIOSParameters(bundleID: "<Bundle-ID>")
linkBuilder.iOSParameters?.appStoreID = "<app-store-id>"
linkBuilder.androidParameters = DynamicLinkAndroidParameters(packageName: "<android package name>")
linkBuilder.navigationInfoParameters?.isForcedRedirectEnabled = true
guard let longDynamicLink = linkBuilder.url else { return }
print("The long URL is: \(longDynamicLink)")
self.referralLinkLabel.text = String(describing: longDynamicLink)
DynamicLinkComponents.shortenURL(longDynamicLink, options: nil) { (url, warnings, error) in
if url != nil {
print("Short URL is: \(url)")
self.referralLinkLabel.text = String(describing: url!)
}
}
}
This is the screen which opens after when safari opens the link:
After tapping on "Open" button of alert box browser opens app store app.
Actually what I would like to do is to redirect and go to the app store, so users can download the app directly. I don't want to this two steps to open app store.
You should first init DynamicLinkNavigationInfoParameters, then set property to true.
linkBuilder.navigationInfoParameters = DynamicLinkNavigationInfoParameters()
linkBuilder.navigationInfoParameters?.isForcedRedirectEnabled = true
Hi you can reduce these steps with this check on the dynamic link
But you should have in mind that you can have a problem with your link in Messanger app and some other apps.
In order to open instagram app with certain post I'm using following code:
func instaOpen(_ postId: String, _ postUrl: String){
let appURL = URL(string: "instagram://media?id=\(postId)")!
if UIApplication.shared.canOpenURL(appURL) {
UIApplication.shared.open(appURL, options: [:], completionHandler: nil)
} else {
// if Instagram app is not installed, open URL inside Safari
let webURL = URL(string: postUrl)!
let svc = SFSafariViewController(url: webURL)
present(svc, animated: true, completion: nil)
}
}
When instaOpen function called – instagram app opens, but login prompt forcefully pops over. Not matter what you do - close it or proceed with login, the queried post simply won't open(see gif).
This started happening recently, after I've updated my app and pushed deployment target to iOS12.
I do have instagram listed in my LSApplicationQueriesSchemes as well as I'm 100% positive that correct mediaID is being passed to instaOpen func (the code worked previously).
Let me know if there's any suggestions on how to fix this and actually open instagram post in instagram app.
Updated - Facebook developer fixed the issue.
Its instagram bug you can follow its progress from https://developers.facebook.com/support/bugs/290173615155052/?disable_redirect=0
Probably a bug, as that feature works on Android.
i manage to "fix" the problem on a pwa app using the Instagram web app.
let appURL = URL(string: "https://www.instagram.com/p/\(postId)")!
//https://www.instagram.com/p/insert here media id
if you click for example on someone’s Instagram button from my app
you gonna open the link instagram://app/ username in the profile
let’s say a user put “mark20” as Instagram username
You open the link instagram://app/mark20/
i want to open instagram with showing 'mark20' . currently it is open Instagram news feeds but not open 'mark20' profile . i want to button click open Instagram user profile page
For example
How can i do that
func shareToInstagram() {
//let instagramURL = URL(string: "instagram://app/")
//not working anymore
let instagramURL = URL(string: "instagram://app/mark20/")
if (UIApplication.shared.canOpenURL(instagramURL! as URL)) {
UIApplication.shared.open(instagramURL!, options: ["":""], completionHandler: nil)
} else {
print(" Instagram isn't installed ")
}
}
Yea I figure out the problem path parameter not working anymore but query parameter working
func shareToInstagram() {
let instagramURL = URL(string: "instagram://user?username=mark20")
if (UIApplication.shared.canOpenURL(instagramURL! as URL)) {
UIApplication.shared.open(instagramURL!, options: ["":""], completionHandler: nil)
print("miss you so much ")
} else {
print(" Instagram isn't installed ")
}
}
more check this link instagram iPhone Hooks
Swift
var instagramAppURL = URL(string: "instagram://user?username=USERNAME")
if UIApplication.shared.canOpenURL(instagramAppURL!) {
UIApplication.shared.openURL(instagramAppURL!)
}
Obj C
NSURL *instagramAppURL = [NSURL URLWithString:#"instagram://user?username=USERNAME"];
if ([[UIApplication sharedApplication] canOpenURL:instagramAppURL]) {
[[UIApplication sharedApplication] openURL:instagramAppURL];
}
First, you have to modify Info.plist to list instagram and facebook with LSApplicationQueriesSchemes. Simply open Info.plist as a Source Code, and paste this:
<key>LSApplicationQueriesSchemes</key>
<array>
<string>instagram</string>
<string>fb</string>
</array>
For more details and more ways please refer below link
https://www.instagram.com/developer/mobile-sharing/iphone-hooks/
I don't know how to check if app is installed or not on phone! Or when App is installed, open the app, otherwise open the Appstore link to download the app. I'm using swift 3. I want to do it using app name or bundle identifier.
Thank You!
func openApp(appName:String) {
let appName = "instagram"
let appScheme = "\(appName)://app"
let appUrl = URL(string: appScheme)
if UIApplication.shared.canOpenURL(appUrl! as URL) {
UIApplication.shared.open(appUrl!)
} else {
print("App not installed")
}
}
After looking into so many answers, i am writing a common code which will help for new users. If you have two mobile apps as App1 and App2, if you want to check in App2 that App1 is already installed in his device or not, here is code below.
In App1 add this property in info.plist file
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLName</key>
<string>com.companyName.App1</string>
<key>CFBundleURLSchemes</key>
<array>
<string>App1</string>
</array>
</dict>
</array>
In App2 add this property in info.plist file
<key>LSApplicationQueriesSchemes</key>
<array>
<string>App1</string>
</array>
In App2 write the method to check if app is installed or not on phone! Or when App is installed, open the app, otherwise open the Appstore link to download the app as below.
func checkAndOpenApp(){
let app = UIApplication.shared
let appScheme = "App1://app"
if app.canOpenURL(URL(string: appScheme)!) {
print("App is install and can be opened")
let url = URL(string:appScheme)!
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(url)
}
} else {
print("App in not installed. Go to AppStore")
if let url = URL(string: "https://apps.apple.com/us/app/App1/id1445847940?ls=1"),
UIApplication.shared.canOpenURL(url)
{
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(url)
}
}
}
}
I hope it will help some one.
Between the other answers and their comments, I'm getting the impression that the asker wants to be able to see if any given app is installed.
Beginning with iOS 9.0, that is not possible.
Apps for iOS 9 and later must have a list of requested URL schemes in the Info.plist before being allowed to use canOpenURL:. This is to protect user privacy, as advertisers were abusing this functionality in an arguably invasive fashion. (See this excellent post for more details on those changes.)
Of course, that list is static and cannot be changed after build time or submission to the App Store. If Apple doesn't like the ones you chose, they have every right to reject it.
I'm afraid that what you're asking isn't possible within reason for iOS 9.0 and later.
Edit: I also want to make clear that an app's URL scheme may not necessarily match nicely with its name. (This is more of an issue of a badly named constant than a functional issue.) There used to be a giant list of known URI schemes with documentation for each, but poignantly and fittingly enough, it seems that the wiki hosting it has shut down.
Swift 4.1. One developer can have more than one app on AppStore. So, I need to check if user has installed other apps or not by the same developer. I had Bundle ID's of other apps. Although you can use Appname instead of Bundle Id. So I followed the following steps.
In your current app add LSApplicationQueriesSchemes key of type Array in your info.plist file. Make entry of bundle id or Appname of app there which you want to open from your app.
Other app should have their bundle id or Appname entry in that app URL Scheme.
In your current app check if that app in installed in iPhone or not and can open accordingly.
let app = UIApplication.shared
let bundleID = "some.Bundle.Id://"
if app.canOpenURL(URL(string: bundleID)!) {
print("App is install and can be opened")
let url = URL(string:bundleID)!
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(url)
}
} else {
print("App in not installed. Go to AppStore")
}
You can also test it from Safari browser. Just type the following in search bar
URL_scheme:// or Bundle_Id://
If app is installed the it will show alert with Appname to open it in app.
This worked for me (Swift 3.0)
Below two inputs should be provided:
<APP URL SCEHME>: The URL Scheme of the app which you want to open
<YOUR APP URL>: The App Itunes URL
func openApp() {
let appURL = NSURL(string: "<APP URL SCHEME>")
if UIApplication.shared.canOpenURL(appURL as! URL) {
print("Opening App...")
}else {
UIApplication.shared.openURL(NSURL(string: "<YOUR APP URL>")! as URL)
}
}
first go to info.plist, add LSApplicationQueriesSchemes add an item and place instagram on that item. Now this code will run perfectly.
let appName = "instagram"
let appScheme = "\(appName)://"
let appUrl = URL(string: appScheme)
if UIApplication.shared.canOpenURL(appUrl! as URL)
{
UIApplication.shared.open(appUrl!)
} else {
print("App not installed")
}
I know there are lot of topics about this problem but is there a solution? I have not found official documentation for that.
My problem is I need to redirect my code on storage settings like this : (work in iOS 9)
let settingsUrl = NSURL(string: "prefs:root=CASTLE&path=STORAGE_AND_BACKUP")
if let url = settingsUrl {
UIApplication.shared.openURL(url as URL)
}
But since ios10 this method don't work, so Is there an alternative? I saw SnapChat, Google Maps redirect their apps to different part on settings (not the main screen of settings) so I think there is a solution.
I already implement the url scheme in info.plist but it's still not work
I tried this method but same issue too
let settingsUrl = NSURL(string: "prefs:root=CASTLE&path=STORAGE_AND_BACKUP")
if let url = settingsUrl {
if #available(iOS 10.0, *) {
UIApplication.shared.open(URL.init(string:"Prefs:root=General&path=STORAGE_ICLOUD_USAGE/DEVICE_STORAGE")!, options: [UIApplicationOpenURLOptionUniversalLinksOnly:true], completionHandler:{(success: Bool?) -> Void in})
} else {
UIApplication.shared.openURL(url as URL)
}
}
Thank in advance.
Since iOS 10, it's not possible to open the Settings app from a third party app. The only settings that are allowed to be opened are Keyboard setting but only by a custom keyboard extension and your own application settings.
More details: here
Note: Even for iOS 9, using the URL string that is mentioned in the question can lead to app rejection as it violates iOS App Review Guidelines.