Facebook iOS problems callbacks etc - iphone

I integrated the facebook ios sdk into my application. When I first did this it was connected to my ONLY ViewController class. So it worked just like the demo. It also DID work. I was able to post to everything etc. My app delegate has the correct connect URL stuff in it etc..
The problem came when I changed my app to have a different main screen and the last screen has the facebook connection info. So now my App Delegate has the function:
-(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
SocialView *socialView = [[SocialView alloc] init];
return [[socialView facebook] handleOpenURL:url];
}
Due to this change my facebook variable is always null when returning from the login and the didLogin never gets called. I'm assuming this is because every time it returns the view is being recreated and the facebook var is re-inited as well.
What is a solution to this? Thanks for any and all help...
-d
Edit: Its worth noting that if I only had one view:
-(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
return [[viewController facebook] handleOpenURL:url];
}
works fine..

David, you are right. The solution you are showing allocate the alternative view controller each time the authentication is finished and the app is back from facebook-connect. This way nothing can be saved.
Here's what you should do:
assume your viewcontroller which handles facebook-connect is called facebookViewController, then the code you need is:
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
return [[viewController.facebookViewController facebook] handleOpenURL:url];
}
Try and let me know if you got it :)
Good Luck.

Related

is there a way to differentiate the IOS application launched using custom url and on clicking the application icon?

I am developing a ios application. When i launch the application it should open the root view. If i am launching the application using custom url from a website, it should initialize with another nib file. Can someone help me with the problem.
Once again thank you for the answers.
In your app delegate you have to implement:
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
This will be called if your app is opened via URL scheme. In there you can react appropriately.
You can find the documentation here.
You have to implement open url in app delegate
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
if (!url)
{
return NO;
}
NSString *URLString = [[url host]stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
return YES;
}
Here is a nice link you can follow link`

UIApplication's openURL Crashes My App

I'm using UIApplication's openURL: method to open a website in Safari. When the user came back to the app (fast switching), it relaunched. It means that my app was quit instead of going to the background.
Does anyone have the same issue? Any ideas? Thank you in advance.
It may be possible that you need to retain the URL object. That was once the issue for me
make sure that you are using
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
in your appdelegate rather than
- (void)applicationDidFinishLaunching:(UIApplication *)application

iPhone Facebook SDK Login Problem

Hey guys
I just tried to implement the facebook sdk like in the DemoApp from Facebook itself into my application. I basically copied everything from the demoApp but I have a problem when I try to login to facebook.
I press the login button and then the facebook app opens. I authorize myself and press OK. Then I am redirected to my app, but then the fbDidLogin() method gets not called. I also defined my app ID in my view controller and the url schema in the info file.
Am I missing something?
Thanks,
Cheers,
Doonot
fbDidLogin() isn't an Objective C method. Is that the way you have it in the code?
Have you handled your custom URL scheme in your app delegate?
Ok after some trying I figured it out. I used the Interface Builder to set up my Tab Bar Application. I had to change the tab bar application to programmatically in order to have a reference to the view controller which implements the facebook login method:
...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
settingsController = [[SettingsViewController alloc] initWithNibName:#"SettingsViewController" bundle:nil];
...
As Jim mentioned, I forgot to implement the custom url scheme in my app delegate. Thanks to the settingsViewController I simply added:
// this method is needed by the facebook login procedure
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
return [[settingsController facebook] handleOpenURL:url];
}
That's it, after these changes I can finally login to facebook.
Cheers,
Doonot

Lauching App with URL (via UIApplicationDelegate's handleOpenURL) working under iOS 4, but not under iOS 3.2

I have implemented UIApplicationDelegate's
application:didFinishLaunchingWithOptions:
and
application:handleOpenURL:
according to specification, i.e.,
application:didFinishLaunchingWithOptions:
returns YES
and
application:handleOpenURL: opens the URL.
The code works under iOS 4 (in both cases, i.e., when the app is launched and when it becomes active from suspended state). However, the code does not work under iOS 3.2.
I give an answer to my own question. Finding out the solution took me a while and was quite frustrating. If you do an internet search you find some partial answers, but it still took me a while to work out the following solution and I do hope it adds some clarity.
So first, the recommended behavior of your app appears to be the following (see Opening Supported File Types in iOS Ref Lib):
Do not implement applicationDidFinishLaunching: (see the note at UIApplicationDelegate).
Implement application:didFinishLaunchingWithOptions: and check the URL, return YES if you can open it, otherwise NO, but do not open it.
Implement application:handleOpenURL: and open the URL, return YES if successful, otherwise NO.
In iOS 4, passing an URL to an app results in one of the following two behaviors:
If the app is launched then application:didFinishLaunchingWithOptions: is called and application:handleOpenURL: is called if and application:didFinishLaunchingWithOptions: returned YES.
If the app is becoming active from suspended state then application:didFinishLaunchingWithOptions: is not called but application:handleOpenURL: is called.
However, in iOS 3.2 it appears as if application:handleOpenURL: is never called! A hint that the behavior is different under iOS 3.2 can be found in Handling URL Requests. There you find that application:handleOpenURL: is called if application:didFinishLaunchingWithOptions: is not implemented, but applicationDidFinishLaunching: is implemented. But application:handleOpenURL: is not called if application:didFinishLaunchingWithOptions: is implemented.
Hence, one solution to make the code work under 3.2 and 4.0 is:
Open the URL in application:didFinishLaunchingWithOptions:, but then return NO to prevent that application:handleOpenURL: is called.
Open the URL in application:handleOpenURL:, in case you are under 4.0 and the app was in suspended state.
I found this solution in another post, but I was confused, because it contradicted the recommendation in iOS Ref Lib documentation (namely that we should return YES in application:didFinishLaunchingWithOptions:). (At that point I did not realize that the documentation contradicts it self).
I believe that the current iOS 4.0 behavior will be the future behavior I prefer the following solution:
Do not implement applicationDidFinishLaunching:.
Implement application:didFinishLaunchingWithOptions: and check the URL, return YES if you can open it, otherwise NO, but do not open it. If we are on 3.2, open the URL.
Implement application:handleOpenURL: and open the URL, return YES if successful, otherwise NO.
So in summary, I implement the iOS 4 behavior and added the following line to application:didFinishLaunchingWithOptions:
if([[[UIDevice currentDevice] systemVersion] hasPrefix:#"3.2"]) {
[self application:application handleOpenURL:url];
}
which make the code work under 3.2.
application:handleOpenURL: is now DEPRECATED.
As of iOS 4.2, you can use this for opening URLs:
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
Documentation:
https://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIApplicationDelegate_Protocol/Reference/Reference.html
I started writing application which used Dropbox api. To understand concept, I ran a sample application using my Key/secret mentioned at dropbox/developer documentation.
Once sample app started working, I used same key/secret values for my application.
For sample app, implementation of handleOpenURL (or openURL on iOS 4.2) gets executed as expected. For some odd reason, it wasn't the case for my app. My app entered background in order to show login screen and authentication page of dropbox. After successful login and authentication, my app never entered foreground. It was true for both platform Simulator and device (iPad)
I tried almost everything listed on internet including this post. Thanks. There was NO success, though.
At last, it STARTED working for my application when I did following:
On simulator, select "iOS Simulator --> Reset Content and Settings", and reset.
On device, I deleted sample application related executable and which in turn delete cache associated to it.
Add the following to the end of application:DidFinishLaunchingWithOptions:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
...
NSURL *url = (NSURL *)[launchOptions valueForKey:UIApplicationLaunchOptionsURLKey];
if (url != nil && [url isFileURL]) {
return YES;
} else return NO;
} // End of application:didFinishLaunchingWithOptions:
// New method starts
-(BOOL) application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
mvc = [nc.viewControllers objectAtIndex:0];
if (url != nil && [url isFileURL]) {
[mvc handleOpenURL:url];
}
return YES;
}
where mvc is my main ViewController, and nc my navigation controller.
Then in the MainViewController, do something like this:
- (void)handleOpenURL:(NSURL *)url {
[self.navigationController popToRootViewControllerAnimated:YES];
// Next bit not relevant just left in as part of the example
NSData *jsonData = [NSData dataWithContentsOfURL:url];
NSError *error;
NSDictionary *dictionary = [[NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&error] objectAtIndex:0];
[self managedObjectFromStructure:dictionary withManagedObjectContext:self.context];
...
}
after declaring handleOpenURL in the .h of course.
Thanks goes to Christian for putting in the effort for this.

Multitasking aware applications in iOS 4 and Custom URL Schemes

I'm trying to implement OAuth securely as detailed here: http://fireeagle.yahoo.net/developer/documentation/oauth_best_practice#custom-url-osx. I seem to have hit a stumbling block, as I am unable to figure out how to handle a url which launches my application when in the background.
I have registered my application to handle oauthtest. I have confirmed that oauthtest:// and oauthtest://callbacktest both launch my application and operate as intended when my application is not running in the background.
I am implementing
application:(UIApplication *) didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
which is successfully called when my application starts up cold. I can easily get the url passed to my application.
However, if my application is already running in the background, neither
application:(UIApplication *) didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
nor
application:(UIApplication *) handleOpenURL:(NSURL *)url
is called and I have no way of getting the parameters passed to my application as part of the URL.
Does anyone know how to get the parameters passed to a backgrounded application by a custom url scheme?
I am aware that I could work around this issue by disabling multitasking, but I would rather not do that for obvious reasons. Thanks in advance.
Here's some sample code that seemed to work for me, tested in iOS4:
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
NSLog(#"handleOpenURL - %#", [url absoluteURL]);
return YES;
}
- (void)applicationDidFinishLaunching:(UIApplication *)application {
NSLog(#"applicationDidFinishLaunching");
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSLog(#"didFinishLaunchingWithOptions - %#", [launchOptions objectForKey:#"UIApplicationLaunchOptionsURLKey"]);
return NO;
}
If I launch the application for the first time, didFinishLaunching: handles the URL. If I then put the app in the background, go back to Safari, and tap a link that brings the app back into the foreground, then handleOpenURL: takes care of the URL. Good luck!