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>
Related
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:
I'm currently using a custom URL scheme to allow users to access my app (say, FoobarApp) from custom links (foobar://resource/42).
I set up the scheme like so :
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLName</key>
<string>com.acme.foobarapp</string>
<key>CFBundleURLSchemes</key>
<array>
<string>foobar</string>
</array>
</dict>
</array>
This works well when the users click the link in Safari.
Now I'd like them to be able to click said links in third-party apps (in my case Trello), to open my app.
This does not work, as the system (since iOS 9 if I'm not mistaken) now requires that apps whitelist URL scheme they want to query (with the LSApplicationQueriesSchemes in Info.plist).
(This is the message I get in the logs when I click the link in the third-party app:
iPad Trello(UIKit)[2368] : -canOpenURL: failed for URL: "foobar://resource/42" - error: "This app is not allowed to query for scheme foobar"
)
It's not reasonable to expect the third-party app to add my (enterprise) app's scheme to their LSApplicationQueriesSchemes list. Is their any option to "bypass" this protection? A kind of way to tell the system "It's fine, Trello can open my app"?
Summary
Custom app links foobar:// work in Safari
They don't work in third-party app (nothing happens, see log a few lines up)
How can I whitelist third-party apps so they can open my app?
an app has to tell ios it wants to query a url scheme. It has to declare this in its info plist at compile time!
There is no way around this on a apple allowed iPhone.
Sure when you jailbreak a phone, all can be done but... thats not a valid assumption either ;)
a workaround may be to link to a Website via HTTP and have the user open the app from there.
I am currently running the example codes provided by Uber SDK (Objective C version).
I encounter a problem when I clicked on the Native Login and Ride Request Widget Button provided by the Objective C example.
After setting up the app in Uber and also copied the Client ID to the info.plist, I counter the error below:
When I click at the Ride Request Widget Button, the app will load a new view with the "Ride there with Uber" button. After clicking the "Ride there with Uber" button, the example app will flag a UIAlert with a message of "There seems to be a problem connecting to Uber. Please go back to AppName and try again". What could be the problem?
Btw, I fill in the redirect URL as "myapp://oauth/callback" on both the dashboard and the app info.plist.
Anything I miss out during the setup of this example?
Thanks.
Make sure the URL Scheme in Targets > Info is set to something like sampleApp, and not sampleApp://uberSSO. This solved my problem.
For reference, here is the snippet from the Info.plist:
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLName</key>
<string>MySampleAppName</string>
<key>CFBundleURLSchemes</key>
<array>
<string>sampleApp</string>
</array>
</dict>
</array>
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>
I was able to open apps from safari this way:
window.location = 'myapp://do/xx';
or open facebook app:
window.location = 'fb://';
But this stopped working in iOS9.
How can I open apps using URL schemes in safari?
IOS 9 URL Shchemes Update :
iOS 9 introduces LSApplicationQueriesSchemes to allow apps to query if other apps are installed.
1- If a url scheme is declared and calling canOpenURL(scheme)
YES if a installed app supports that URL scheme
NO if no app supporting that url
syslog will show canOpenURL: failed for URL: "urlScheme://" - error: null
2- If a url scheme is not declared and calling canOpenURL(scheme)
always return NO
syslog will show canOpenURL: failed for URL: "urlScheme://" - error: null
In iOS 9, the developer must add these info.plist
LSApplicationQueriesSchemes
<array>
<string>urlscheme</string>
<string>urlscheme2</string>
<string>urlscheme3</string>
<string>urlscheme4</string>
</array>
50 max. unqiue URL scheme can be declared!
With iOS9, Apple is changing a few things concerning URL schemes. Here is an article about those changes.
Basically, you now have to register all URL schemes that are supported by your app in your .plist file.
With the general release of ios9 setting window.location to a custom url does launch the app, but only after the user hits open in a popup. I filed a bug with apple about this, and they responded saying that it was intended behavior for launching an app from safari. They said they would look into the issue where if you hit cancel it fails out on future attempts.
Just assign a desired address to the href property, instead of trying to replace the whole window.location object:
window.location.href = 'myapp://do/xx';
It works for me on iOS 9.0.2, but now safari shows a confirmation dialog to ensure а user really wants open your link in the app.
this is how I have revised my Facebook/Google Login integration to get it work on latest update. which now user Safari web view.
<key>LSApplicationQueriesSchemes</key>
<array>
<string>fbapi</string>
<string>fb-messenger-api</string>
<string>fbauth2</string>
<string>fbshareextension</string>
<string>com.googleusercontent.apps.39373238582-opjuqokmar2i5t3sdk2vs5sifer4moen</string>
<string>com-google-gidconsent-google</string>
<string>com-google-gidconsent-youtube</string>
<string>com-google-gidconsent</string>
<string>com.google.gppconsent.2.4.1</string>
<string>com.google.gppconsent.2.4.0</string>
<string>googlechrome</string>
<string>googlechrome-x-callback</string>
</array>