What is the new function for handleOpenURL in Xcode - ios

I am Trying to make an app using a old tutorial. so many things have changed since the tutorial was published. I need to make an facebook login, so going through some posts i learned, FBAppCalls have changed to FBSDKApplicationDelegate, but there was a call like this in the old tutorial
FBAppCall.handleOpenURL(url, sourceApplication:sourceApplication, withSession:PFFacebookUtils.session())
So what will it be in the new Updates of swift/xcode/fb framworks etc.?

Here is the method you can use
func application(application: UIApplication,
openURL url: NSURL,
sourceApplication: String?,
annotation: AnyObject?) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(
application,
openURL: url,
sourceApplication: sourceApplication,
annotation: annotation)
}
for iOS 9 this method gets called & you should use like this
func application(application: UIApplication,
openURL url: NSURL, options options: [String: AnyObject]) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(url,
sourceApplication: options[UIApplicationOpenURLOptionsSourceApplicationKey]! as! String,
annotation: options[UIApplicationOpenURLOptionsAnnotationKey])
}
func applicationDidBecomeActive(application: UIApplication) {
FBSDKAppEvents.activateApp()
}

Related

I have to call this two functions depending on which social network I come from in App Delegate

I'm creating an app that uses social networks to sign in. My issue is that google calls func application(application: UIApplication,
openURL url: NSURL, options: [String: AnyObject]) ->Bool and Facebook that calls func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool.
The thing is that having that two functions in AppDelegate, Facebook doesn't take me back to app but Google does. I think there's a conflict between these two functions.
I've tried to set a flag var but I can't set an if/else statement outside of any function.
What can I do?
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation)
}
func application(application: UIApplication,
openURL url: NSURL, options: [String: AnyObject]) -> Bool {
return GIDSignIn.sharedInstance().handleURL(url, sourceApplication: options[UIApplicationOpenURLOptionsSourceApplicationKey] as? String, annotation: options[UIApplicationOpenURLOptionsAnnotationKey])
}
Each of those third party handleURL functions returns a Bool to let you know whether that particular library actually handled the URL in question. You can combine the checks into one individual delegate method and return true if either one of them returns true. I modified your code to show you what that might look like. Notice I am calling the iOS 8 deprecated function in the newer one because I am assuming you are trying to support iOS 8 as well. If that's not the case, you can get rid of that one entirely and move its body into the iOS 9 function.
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool
{
if FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation)
{
return true
}
else if GIDSignIn.sharedInstance().handleURL(url, sourceApplication: sourceApplication, annotation: annotation)
{
return true
}
else
{
// put more logic here as you need to but for now just return false if you didn't handle the URL
return false
}
}
func application(application: UIApplication, openURL url: NSURL, options: [String: AnyObject]) -> Bool
{
return self.application(application, openURL: url, sourceApplication: options[UIApplicationOpenURLOptionsSourceApplicationKey] as? String, annotation: options[UIApplicationOpenURLOptionsAnnotationKey]!)
}
edit: As a side note, the reason why your Facebook function wasn't getting called is because on an iOS 9+ system, if it detects you have implemented the newer function, it will never call the older one. If you removed the newer one entirely, you would notice the old one would still get called (and your Facebook handling would work).

Facebook and Google sign in with Swift

I have two custom sign in Buttons for Google and Facebook on the screen. Here is my code written in AppDelegate which causes the problem which I will explain after the code
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation)
return true
}
func application(application: UIApplication,
openURL url: NSURL, options: [String: AnyObject]) -> Bool {
if #available(iOS 9.0, *) {
return GIDSignIn.sharedInstance().handleURL(url,
sourceApplication: options[UIApplicationOpenURLOptionsSourceApplicationKey] as? String,
annotation: options[UIApplicationOpenURLOptionsAnnotationKey])
} else {
// Fallback on earlier versions
}
return true
}
If I write this code then after clicking the OK button here:
The screen doesn't close up and stays there. But the Google screen works perfectly if we click the "Allow" button. But If I use this code only:
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation)
return true
}
And remove other function then the Facebook "OK" button works fine. Why doesn't Facebook work when I add this function?
func application(application: UIApplication,
openURL url: NSURL, options: [String: AnyObject]) -> Bool {
if #available(iOS 9.0, *) {
return GIDSignIn.sharedInstance().handleURL(url,
sourceApplication: options[UIApplicationOpenURLOptionsSourceApplicationKey] as? String,
annotation: options[UIApplicationOpenURLOptionsAnnotationKey])
} else {
// Fallback on earlier versions
}
return true
}
This solution works for me
func application(application: UIApplication,
openURL url: NSURL, options: [String: AnyObject]) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: options[UIApplicationOpenURLOptionsSourceApplicationKey] as! String, annotation: nil) ||
GIDSignIn.sharedInstance().handleURL(url,
sourceApplication: options[UIApplicationOpenURLOptionsSourceApplicationKey] as? String,
annotation: options[UIApplicationOpenURLOptionsAnnotationKey] as? String)
}
iOS Facebook and Google login at the same time?
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool
{
return FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation)
}
// Please test this condition first, if it's works put (or) condition int return statement for GIDSignIn flag values
This below func in Swift 3.0 works for iOS 8.0 and above.
// Fallback on earlier versions
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
return (FBSDKApplicationDelegate.sharedInstance().application( application,openURL: url,sourceApplication: sourceApplication,annotation: annotation) || GIDSignIn.sharedInstance().handleURL(url, sourceApplication: sourceApplication, annotation: annotation))
}

Use of unresolved identifier sourceApplication Facebook SDK

The following code to implement FB login into my app
func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation)
However I keep getting two error messages both similar,
"Use of unresolved identifier 'annotation'
"Use of unresolved identifier 'sourceApplication'
I'm not exactly sure what's causing this error but I have a feeling its a bit of code that's been deprecated. Does anyone know what the current alternative is?
appdelegate.swift
I´m using this code in my app delegate and works perfect
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
}
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation)
}
You have a variable name mismatch:
You declare your UIApplication as app in the first line, but try to reference it by sourceApplication in your return statement. Replace sourceApplication with app. Same with annotation, change that to options:
func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: app, annotation: options)

FacebookSDK not able to login, giving blank white screen after authorization.

Added URL Type , LSApplicationQueriesSchemes and other settings.
Tried every possible solution, Not able to debug. Please help with this
I solved this by implementing BOTH of these functions, even though facebook's developer website only lists one:
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation)
}
func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool {
let shouldOpen :Bool = FBSDKApplicationDelegate.sharedInstance().application(
app,
openURL: url,
sourceApplication: options["UIApplicationOpenURLOptionsSourceApplicationKey"] as! String,
annotation: nil)
return shouldOpen
}
For anyone fighting this for swift3 + ios10, make sure your appdelegate looks like:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
}
func applicationWillResignActive(_ application: UIApplication) {
FBSDKAppEvents.activateApp()
}
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(application, open: url, sourceApplication: sourceApplication, annotation: annotation)
}
source
If this is helpful for anyone - I had the Facebook login button on a detached controller (specifically within a custom view that presented a separate View Controller). Once I placed the button inside the parent view controller, things worked instantly.

ParseUI error when working to add Facebook using swift

So I am following the steps on the parse website ( https://www.parse.com/docs/ios_guide#fbusers/iOS ) to add Facebook login. However Xcode is giving me the error '() -> FBSDKApplicationDelegate!' does not have a member named "application' for the following line of code in my app delegate file
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject?) -> Bool {
return FBSDKApplicationDelegate.sharedInstance.application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation)
}
What am I missing?
It looks like you are missing the parentheses for sharedInstance.
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String, annotation: AnyObject?) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation)
}

Resources