Open link in Facebook app if available - ios

I want to open a page in the Facebook app if the user has it installed on his device.
Now, I am using the following code I found while doing research:
let myUrl = NSURL(string: "fb://profile/myPage'sID")!
if UIApplication.sharedApplication().canOpenURL(myUrl) {
UIApplication.sharedApplication().openURL(myUrl)
} else {
UIApplication.sharedApplication().openURL(NSURL(string: "https://www.facebook.com/myPage")!)
}
This does work for a profile but does not for displaying a page.
(Note that both IDs do work, as I tested them.)
Any ideas?
Thanks in advance :)

I think then it may be caused because you need to add this key-value to the Info.plist:
<key>LSApplicationQueriesSchemes</key>
<array>
<string>fb</string>
</array>

Related

Snapchat SDK getting stuck on completion page Swift/Xcode

so I'm working on an app right now where I need to add snapchat login. To do this, I am using their LoginKit SnapSDK. I implemented it as instructed, but when a user tries to log in, they don't go to the snapchat app but rather it opens a safari popup. This wouldn't be an issue if it worked, but it doesn't. The user can put in their credentials, but on the last page they are prompted with a "continue" button that doesn't do anything. Because it does nothing, the user is never logged in and the popup doesn't close. Below I have attached my code (very simple), an image of my Info.plist, and an image of my Frameworks.
Code:
SCSDKLoginClient.login(from: self) { success, error in
if let error = error {
// An error occurred during the login process
print(error.localizedDescription)
} else {
self.dismiss(animated: true)
self.getUserInfo()
}
}
You made a mistake in your info.plist
for item 3 in LSApplicationQueriesSchema which is rocket should be part of URLScheme. Remove that rocket and put it in URLSchemes
You can just copy this code
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLSchemes</key>
<array>
<string>rocket</string>
</array>
</dict>
</array>
As for frameworks make sure it looks like this

UIActivityViewController, iOS, swift

Do I need to show my App in UIActivityViewController for my another application without the usage of Extension? Is it possible?
And also need when i tapped on other app which are shown in UIActivity ViewController that transfer me into that app.(If is it Possible?)
I have already looked at the following SO Thread I need to show my App in UIActivityViewController for All Applications as Action Extension(Default), but this is done by extension.
For e.g, In iOS 11.0 and above version it give myfile application which shows the files.In this files when I tapped on pdf it shows the own pdfviewer but when i tapped on share button it provide me another added pdfviewer to read and other function which are supported by that application and transfer me to that app.
Is it possible with Action Extension?
Thank You.
It depends whether you want to receive documents or data.
If you want to be listed for Open In or Copy To, as for a document type like PDF, then just declare in the Info.plist that you are a viewer for that document type.
Otherwise to receive data directly thru an activity view, use an Action or Share extension to be listed in the activity view.
Finally I got the answer
When I use UIDocumentInteractionController this is showing me my other app which are support "pdf" and add the property in info.plist which are provided by #matt.
Below code used in parent app so when touch button it shows result like below image.
This is my code:-
#IBAction func ShareDocAction(_ sender : UIButton){
let fileUrl = Bundle.main.url(forResource: "Terms_Condition", withExtension: "pdf")
self.documentController = UIDocumentInteractionController(url: fileUrl!)
DispatchQueue.main.async {
self.documentController.presentOptionsMenu(from: self.actionButton.frame, in: self.view, animated: true)
}
}
and in child app we need to add below code in info.plist:-
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeName</key>
<string>PDF Document</string>
<key>CFBundleTypeRole</key>
<string>Viewer</string>
<key>LSHandlerRank</key>
<string>Alternate</string>
<key>LSItemContentTypes</key>
<array>
<string>com.adobe.pdf</string>
</array>
</dict>
</array>
Here I attach my image show whole scenario
Thank you

canOpenURL: failed for URL: "instagram://app" - error: "This app is not allowed to query for scheme instagram"

This is the code I use:
let instagramURL = NSURL(string: "instagram://app")
if UIApplication.shared.canOpenURL(instagramURL! as URL) {
//Code
} else {
//Showing message "Please install the Instagram application"
}
I am getting unsuccessful to enter in if loop.
I get this error:
canOpenURL: failed for URL: "instagram://app" - error: "This app is not allowed to query for scheme instagram"
I have also Login with Instagram in my device.
Right click on your plist file and open it as source code. Then copy and paste below code:
<key>LSApplicationQueriesSchemes</key>
<array>
<string>instagram</string>
</array>
Note: One thing you have to keep in mind that it will not work on simulator. You need a real device for this.
Open your plist as source code and paste following code:
<key>LSApplicationQueriesSchemes</key>
<array>
<string>instagram</string>
</array>
The issue is that you are not registering the URL scheme in the info.plist file.
Please add this LSApplicationQueriesSchemes and add instagram in your info.plist and it will work.
For those who try to open the app using custom URL Scheme (assume that app FirstApp opens SecondApp):
In FirstApp add LSApplicationQueriesSchemes with URL Scheme to Info.plist like this:
In SecondApp register new URL Scheme in URL Types like this:

NSWidgetExtensionContext openURL Swift

I have been attempting to implement a button to open my iOS app from its widget. I realize this issue has been beaten to death on the forums but I cannot find explanation with the specific error I am receiving. Perhaps some of you more experienced iOS developers can shed some light on this.
I am developing an update to one of my iOS apps for iOS 10 using XCode 8.1 and Swift 2.
Code for my widget's button:
URL scheme added to the widget's info.plist:
The runtime error I receive when pressing the OpenApp button:
AppWidget[11872:3577323] __55-[_NCWidgetExtensionContext openURL:completionHandler:]_block_invoke failed: Error Domain=NSOSStatusErrorDomain Code=-10814 "(null)"
// Note: app name has been substituted with appropriate generics.
I often find the OS Status lookup site pretty useful to infer details from errors. An OS error with code -10814 is a kLSApplicationNotFoundErr, which describes the scenario when:
No application in the Launch Services database matches the input criteria.
It sounds like your application has not been properly registered with the system as a consumer of the URL scheme you are using. Have you double-double (double!) checked that the bundle identifier and URL scheme match? Have you verified that your app can be launched with the URL from Safari?
URL scheme should added to the main app's info.plist, not the widget's.
To open the Containing App from Todays Extension:
let myAppUrl = URL(string: "main-screen:")!
extensionContext?.open(myAppUrl, completionHandler: { (success) in
if (!success) {
print("error: failed to open app from Today Extension")
}
})
You also need to add the following lines to the application's info.plist (open as a source code):
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLName</key>
<string>com.mikitamanko.bubblewrap</string>
<key>CFBundleURLSchemes</key>
<array>
<string>main-screen</string>
</array>
</dict>
</array>
right after the
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
Here's the complete guide how to open the app or share Users Defaults with Extension and the containing app.
Also you should check if you are using any not allowed character for your url scheme.
Maybe it is not your case but I was using this and it was wrong:
my_AppName
instead this finally worked :)
myAppName
as said here, the scheme must begin with alphanumeric character and it can contain only alphanumeric characters, +, - and .

Swift Open Facebook Profile by FacebookUID

I'm trying to open a user's Facebook profile in Swift using their Facebook UID. In the Simulator, it works and opens up the web version of the profile and works fine, however on my actual device, it opens up the Facebook native app and gives me a "Page not Found" error.
guard let facebookUID = self.user?.facebookUID else {
return
}
print(facebookUID)
let fbURLWeb: NSURL = NSURL(string: "https://www.facebook.com/\(facebookUID)")!
let fbURLID: NSURL = NSURL(string: "fb://profile/\(facebookUID)")!
if(UIApplication.sharedApplication().canOpenURL(fbURLID)){
// FB installed
UIApplication.sharedApplication().openURL(fbURLID)
} else {
// FB is not installed, open in safari
UIApplication.sharedApplication().openURL(fbURLWeb)
}
What am I doing wrong?
Thanks in advance!
EDIT
I've noticed that if a user deletes or doesn't have the Facebook app installed on their phone, it will open up the profile correctly through Safari, which is why it worked on the simulator, but didn't on my device. However, I've also noticed I can get it to work in the Facebook app if I try a public profile like fb://profile/4 (which is Mark Zuckerburg's profile).
Hi based on documentation Facebook ID is the App-scoped User ID so you can use like
fb://profile?app_scoped_user_id=%#
But also check bug report on https://developers.facebook.com/bugs/332195860270199 opening the Facebook profile with
fb://profile?app_scoped_user_id=%#
is not supported.
So you will have to open the Facebook profile within Safari or you can use in app webview to open user profile.
I have check with some facebook id 10000****889,10000***041 etc. all have open profile in facebook app.Yes some have same error.Page bot found.
May be there is Privacy setting->Do you want search engine outside link to your profile->No. restrict search.
To get ids of user from facebook Trick :
1)Right click on the person or page's profile photo, select properties and the ID will be included in the page
2)In mobile browswe if you open photo there is id E.g.
" fbid = *** & pid = 1000....89 " so 1000....89 will be your user id
Here is another solution that works for iOS 10 & Swift 3.
First of all, you should add LSApplicationQueriesSchemes key to Info.plist file. If you do not add this key, canOpenURL will always return false. Than you need to add what scheme(s) you want to check. Use fb for facebook.
It should looks like this:
The rest is Swift:
if UIApplication.shared.canOpenURL(URL(string: "fb://profile/PROFILE_ID")!) {
UIApplication.shared.open(URL(string: "fb://profile/PROFILE_ID")!, options: [:])
} else {
UIApplication.shared.open(URL(string: "https://facebook.com/PROFILE_ID")!, options: [:])
}
Note: Replace PROFILE_ID with your target.
Thanks to vien vu.
From iOS 9 < above, You must whitelist the url's that your app will call out to using the LSApplicationQueriesSchemes key in your Info.plist. So add this code to your plist file:
<key>LSApplicationQueriesSchemes</key>
<array>
<string>fb</string>
<string>fbapi</string>
<string>fbauth2</string>
<string>fbshareextension</string>
<string>fb-messenger-api</string>
<string>twitter</string>
<string>whatsapp</string>
<string>wechat</string>
<string>line</string>
<string>instagram</string>
<string>kakaotalk</string>
<string>mqq</string>
<string>vk</string>
<string>comgooglemaps</string>
</array>
I able to use this URL scheme fb://profile?id={userid/username/pageid} to open a facebook user's profile / page as of 30 Aug 2020.
eg: fb://profile?id=4 OR fb://profile?id=zuck
Warning: Please use at your own risk as this is not documented in Facebook developers documentation. This URL scheme may be removed by Facebook app in the future without any warning and indication.
Swift 2.3
func goFacebook() {
let profileID = "" //Here put profile id Example:'62260589592'
let facebookURL = NSURL(string: "fb://profile/\(profileID)")!
if UIApplication.sharedApplication().canOpenURL(facebookURL) {
UIApplication.sharedApplication().openURL(facebookURL)
} else {
UIApplication.sharedApplication().openURL(NSURL(string: "https://www.facebook.com/PageName")!)
}
}
Note: If you can't find the facebook profile id can you use the follow site to do it (https://findmyfbid.com/)
Set in the info.plist
<key>LSApplicationQueriesSchemes</key>
<array>
<string>fb</string>
<string>fbapi</string>
<string>fbauth2</string>
<string>fbshareextension</string>
<string>fb-messenger-api</string>
</array>

Resources