I am using the latest facebook ios sdk in my app. In order to implement SSO, Facebook says:
Modify your application's main AppDelegate class as follows:
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
In this method, call your application's Facebook object's handleOpenURL method, making sure to pass in the url parameter.
My query is:
What is the use of the above method? Why to call it? If I hide the method in the facebook demo app provided it always redirects 开发者_运维知识库me to login page again and again. If I unhide this method it redirects me to logout with other buttons option (which is right)
Now as this method is defined in ApplicationDelegate.m file and we have to call facebook object in it. How can I refer to the facebook object in this method as my facebook object is defined in a view controller class which is 4 times down the hierarchy.
NOTE: I have tried the DemoApp. I do not understand how did they refer to the facebook object in the AppDelegate.m file inspite of the facebook object declared in ViewController file.
The method is used to determine if your application will 'handle' the url that is passed to it. For example, your application can handle a specific URL 'scheme' and then choose at runtime if it will handle the specific URL. IF you return NO, it will move on to other application that may also handle the same URL scheme. Although, in the case of the Facebook iOS SDK, your application would be the only application handling the URL scheme.
When you attempt to login to Facebook, the SDK passes values to either the Facebook application or to the Facebook website and it includes a callback URL so that the authorization process can be completed. Without this method in the app delegate, that callback won't work properly.
For more information on oAuth, see:
http://developers.facebook.com/docs/authentication/
Second, this is a bigger question than just the Facebook SDK. This has to do with a core architectural issue. You can include the Facebook object in your application delegate and then reference it in the view controller. This is the easiest method. Within the view controller, you could just do the following:
Facebook *facebook = [(YourAppDelegate *)[UIApplication sharedApplication] delegate] facebook];
This option isn't always ideal, because it makes the app delegate a dumping ground for a lot of shared instances.
Another option is to have the application delegate pass the Facebook object to the view controller when you create it (the view controller would have a property for the Facebook object - and then the application delegate would pass it to the view controller after init).
MyViewController *vc = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];
vc.facebook = self.facebook;
There are also other options for sharing instances of classes such as the Objection iOS framework, but these are a bit more advanced.
精彩评论