How to link iOS app to facebook page in Swift? - ios

I'm trying to link my app to facebook page , I have used this code :
let facebookURL = NSURL(string: "fb://profile/turathalanbiaa")!
if UIApplication.sharedApplication().canOpenURL(facebookURL)
{
UIApplication.sharedApplication().openURL(facebookURL)
}
else
{
UIApplication.sharedApplication().openURL(NSURL(string: "https://www.facebook.com/turathalanbiaa")!)
}
info.plist
<key>LSApplicationQueriesSchemes</key>
<array>
<string>fb</string>
</array>
but facebook app shows the user profile page not the page that I provided. what did I miss ?

Related

Facebook iOS SDK login error (Invalid Scopes: public_profile, openid.)

I receive the follow error when trying to login using the iOS Facebook SDK:
Invalid Scopes: public_profile, openid.
I have followed the instructions from here which has resulted in this set up:
info.plist
<plist version="1.0">
<dict>
...
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLName</key>
<string></string>
<key>CFBundleURLSchemes</key>
<array>
<string>GOOGLE_URL_SCHEME</string>
<string>fbMY_FB_APP_ID</string>
</array>
</dict>
</array>
<key>FacebookAppID</key>
<string>MY_FB_APP_ID</string>
<key>FacebookClientToken</key>
<string>MY_FB_CLIENT_TOEN</string>
<key>FacebookDisplayName</key>
<string>MY_FB_APP_NAME</string>
<key>LSApplicationQueriesSchemes</key>
<array>
<string>fbauth2</string>
<string>fbapi</string>
<string>fb-messenger-share-api</string>
</array>
</dict>
</plist>
AppDelegate.swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
ApplicationDelegate.shared.application( application, didFinishLaunchingWithOptions: launchOptions )
return true
}
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]
) -> Bool {
return ApplicationDelegate.shared.application(app, open: url, sourceApplication: options[UIApplication.OpenURLOptionsKey.sourceApplication] as? String, annotation: options[UIApplication.OpenURLOptionsKey.annotation])
}
Login snippet
let loginManager = LoginManager()
loginManager.logIn(permissions: ["public_profile"], from: self) { result, error in
if let error = error {
print("FB Error: \(error)")
} else if let result = result, result.isCancelled {
print("FB Cancelled")
} else {
print("FB Logged In")
}
}
I am on iOS 16.2 using the facebook-ios-sdk version 15.1.0. The same error occurs with FB app in live and development mode. I receive the same error on the simulator and device. I can successfully login on the web when using the FB app JS library. I believe I have followed the instructions correctly. What am I missing?
Edit: Settings Screenshots
Edit:
So I just downloaded this sample from the facebook-ios-sdk. Added my FB App Id to the FacebookAppID field and URL Schemes in the plist. Added the FB Client Token to the plist and updated the bundle identifier to the one listed in the Facebook app, but I get the same error when I run the sample. What the hell is going on? The issue must be in the Facebook app set up, but where? I can't find this issue reported anywhere on the net. Surely someone has seen this before.
I figured it out. I was missing the facebook login from my facebook app products. I found it by accident and now I can login successfully.

canOpenURL return true and app is installed but app is not opening

when I using canOpenURL to open universal link, (target app is installed)
canOpenURL return true, but app is not opening (LSApplicationQueriesSchemes are not registered)
but if I use open(_ url, options) app is opening
If I am responsible for not registering LSApplicationQueriesSchemes, how do I register Universal's links to LSApplicationQueriesSchemes? (https://www.aaa.com)
<key>LSApplicationQueriesSchemes</key>
<array>
<string>https://www.aaa.com</string>
</array>
like this?
If AAA is the app that you're trying to open, there is another way to launch it from inside your app. You will need to grab the CFBundleURLSchemes that the target app uses.
For AAA its "aaamobile"
For AAA Auto Club its "aaamobileace"
You then need to add the url schemes into your plist:
<key>LSApplicationQueriesSchemes</key>
<array>
<string>aaamobile</string>
</array>
And then in your code you can check to see if the app is installed and launch the app or take user to website:
guard let url = URL(string: "aaamobile://") else { return }
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
UIApplication.shared.open(URL(string: "https://www.aaa.com")!, options: [:], completionHandler: nil)
}

Twitter URL Schemes set up in info.plist are throwing an exception

Someone pointed out that this was a duplicate question, Yes partly, I followed the advice in the other post and added the URLScheme as shown below, Also as suggested i forgot to add to my other target so i did and the URL twitter-13145245245624 Now what happens is my app redirects the user to their Twitter App and asks for permission to have my app use their info, once tapping the ok button user is redirected back to my app but still no Composer populates the View to Tweet. Still confused. Thanks for all the help.
let composer = TWTRComposer()
composer.setText("just setting up my Twitter Kit")
composer.setImage(UIImage(named: "twitterkit"))
// Called from a UIViewController
composer.show(from: self) { (result) in
if (result == .done) {
print("Successfully composed Tweet")
} else {
print("Cancelled composing")
}
}
This is my info.plist, however, I have no issue with posting to Facebook.
Any thoughts?
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>twitterkit-xxxxxxxxxx</string>
<string>fbxxxxxx</string>
</array>
</dict>
</array>
<key>LSApplicationQueriesSchemes</key>
<array>
<string>twitter</string>
<string>twitterauth</string>
</array>
Regards
J
The issue to my above question was that I did not have the below method in AppDelegate as so. This Solved my problem.
-(BOOL)application:(UIApplication*)app openURL:(nonnull NSURL *)url options:(nonnull NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options{
return [[TWTRTwitter sharedInstance]application:app openURL:url options:options];
}

facebook and google login open in application by sfsafariviewcontroller

In my app user can login with facebook and google account. but when user click on facebook login or google login he will redirect to safari browser. So apple reject my app with below reason.
We noticed that the user is taken to Safari to sign in or register for an account, which provides a poor user experience. Next Steps To resolve this issue, please revise your app to enable users to sign in or register for an account in the app.
For resolving the issue I added facebook login behavior as the web "FBSDKLoginBehaviorWeb". Now when I open it, it is opening in the app only. but when I click on "Not Now" button on here is the facebook screen shot facebook it is crashing my app in the second time. Can anyone have answer to this issue? Also, can any one suggest me how do I solve the google sign on the issue too?
Try this :
Swift 4.0 snippet:
let loginManager = FBSDKLoginManager()
loginManager.logIn(withReadPermissions: ["email","public_profile"], from: self) { (result, error) in
if ((error) != nil) {
// Process error
} else if (result?.isCancelled)! {
// Handle cancellations
} else {
// If you ask for multiple permissions at once, you
// should check if specific permissions missing
if (FBSDKAccessToken.current() != nil)
{
FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"email, name"]) .start(completionHandler: { (connectioon, result, error) in
if error == nil
{
print(result);
}
})
}
}
}
Make sure you have added these below items in info.plist:
1.
<key>LSApplicationQueriesSchemes</key>
<array>
<string>fbapi</string>
<string>fb-messenger-share-api</string>
<string>fbauth2</string>
<string>fbshareextension</string>
</array>
2.
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>fbxxxxxxxxxxxxxx</string>
</array>
</dict>
</array>
<key>FacebookAppID</key>
<string>xxxxxxxxxxxx</string>
<key>FacebookDisplayName</key>
<string>App name</string>
Note: iOS should be greater than iOS 9.0 or later to use SFSafariViewController.
Try to handle is cancelled , also i use facebook sdk version 4.17.0
and click not know and works perfectly
facebookLogin.logIn(withReadPermissions: ["public_profile","email", "user_friends"], from:self, handler:
{
(facebookResult, facebookError) -> Void in
if facebookError != nil
{
print("Facebook login failed. Error \(String(describing: facebookError))")
}
else if (facebookResult?.isCancelled)!
{
print("Facebook login was cancelled.")
}
else
{
// success
}
});
see in action

Fast App Switch iOS 11 Facebook Login

Currently my facebook login does the following:
Tap the login with fb button
Asks for permission to to use "facebook.com" to log in
Asks whether to use app or log in with an email or phone in a Safari View Controller
If selected app: Asks to open Facebook app
Asks for permission to let the fb app use your to log in
Returns to app
Those are too many steps for me. I'd like to do something like Uber app, it's facebook login flow is:
Tap the login with fb button
Asks to open Facebook app
Asks for permission to let the fb app use your account to log in
Returns to app
Uber's flow:
Facebook Fast App Switch
There's an article that says theres a Fast App Switch way to login which is what I want for my app, If facebook app is installed I want the login to be done directly to the app.
I've used this code at my sign in button action with no luck:
let fbLoginManager = FBSDKLoginManager()
fbLoginManager.loginBehavior = .native
fbLoginManager.logIn(withReadPermissions: ["public_profile", "email"], from: self) { (result, error) in
let credential = FacebookAuthProvider.credential(withAccessToken: accessToken.tokenString)
// Perform login by calling Firebase APIs..
}
At my Info.plist:
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>fbXXXXXXXX</string>
</array>
</dict>
</array>
<key>FacebookAppID</key>
<string>XXXXXXXX</string>
<key>FacebookDisplayName</key>
<string>My Beautiful App</string>
<key>LSApplicationQueriesSchemes</key>
<array>
<string>fbapi</string>
<string>fb-messenger-api</string>
<string>fbauth2</string>
<string>fbshareextension</string>
</array>
At my AppDelegate.swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions:launchOptions)
return true
}
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
let handled = FBSDKApplicationDelegate.sharedInstance().application(app, open: url, options: options)
return handled
}
Facebook SDK version: 4.27.1
Using FBSDKCoreKit (4.27.1)
FBSDKLoginKit (4.27.1)
FBSDKShareKit (4.27.1)

Resources