FB return crashes app xcode ios iphone - iphone

when intergrating FB to my app it comes up and varifies that its connected but when it comes to going back to the app it crashes,
This is the return process from the fb developer page and added it that it comes to my view controller page
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
return [[Ball_GameViewController facebook] handleOpenURL:url];
}
has an advisory on the line that starts
return
saying
warning: 'Ball_GameViewController' may not respond to '+facebook'
any idea am i directing it to the wrong areas.
Ball_GameViewController.h
#import <UIKit/UIKit.h>
#import "FBConnect.h"
#interface Ball_GameViewController : UIViewController <FBSessionDelegate, FBDialogDelegate>
{
Facebook *facebook;
}
#property (nonatomic, retain) Facebook *facebook;
#property (nonatomic, retain) IBOutlet UIWindow *window;
- (IBAction)facebookpost;
- (IBAction)start;
- (IBAction)reset;
- (void)countdown;
- (void)scorecount;
- (void)checkTime;
#end

In your Ball_GameViewController #interface you need to use:
#property (nonatomic, strong) Facebook * facebook;
In your Ball_GameViewController #implementation you need to #synthesize facebook;
Update
Try this:
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
Ball_GameViewController * ballGame = [[Ball_GameViewController alloc] init];
return [[ballGame facebook] handleOpenURL:url];
}

Related

How can I use Facebook instance in a ViewController?

I'm having troubles developing an iOS 5 App with Facebook SDK integration.
My App has many ViewsController and for each of them I want to get different data from Facebook using different requests (of course).
At the moment I can get data only on the AppDelegate (following the official tutorial) but I need to get data in the view controller.
I tried 2 option:
1) Instantiate different Facebook instances on each ViewController
-It dosent' work, I don't know why but it seems that
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation;
is never called
2 Instantiate the Facebook instance in the AppDelegate and then use that instance in ViewControllers
- It don't works because with this code (found here: How do I persist a Facebook instance in multiple IOS view classes?)
- (void)viewDidLoad
{
[super viewDidLoad];
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
Facebook *facebook = [appDelegate facebook];
}
facebook is nil.
Which is the proper way to work?
(I will edit my answer if you give me after that more feedback.)
I haven't used a lot the Facebook SDK but I remember having that problem when I tried.
The - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation implementation should be in your app delegate
If you need to access your Facebook instance in just one viewController (let's say MyFacebooViewController)
You can have something like this
#import <UIKit/UIKit.h>
#import "FBConnect.h"
#interface MyFacebookViewController : UIViewController <FBSessonDelegate, FBRequestDelegate>
#property (nonatomic, strong) Facebook *facebook;
...
#end
And your .m file should contain this
#import "MyFacebookViewController.h"
#import "MyAppDelegate.h"
#implementation MyFacebookViewController
#synthesize facebook = _facebook;
-(Facebook *)facebook
{
if(!_facebook){
_facebook = [[Facebook alloc] initWithAppId:MY_APP_ID andDelegate:self];
}
return _facebook;
}
-(void)viewDidLoad
{
[super viewDidLoad];
((MyAppDelegate *)([[UIApplication sharedApplication] delegate])).facebookViewController = self;
...
}
And of course you need to implement your FBSessionDelegate and FBRequestDelegate methods
In the AppDelegate :
#import <UIKit/UIKit.h>
#import "MyFacebookViewController.h"
#interface MyAppDelegate : UIResponder <UIApplicationDelegate>
...
#property (strong, nonatomic) MyFacebookConnectViewController *facebookViewController;
#end
And it's in the implementation file that you need to put the - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
#implementation MyAppDelegate
#synthesize facebookViewController = _facebookViewController;
...
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
return [self.facebookViewController.facebook handleOpenURL:url];
}
If you still have problems let me know!
I'm not editing my answer as this is code I've never tested.
If you want to use the same Facebook instance in different viewController this is one way to do it:
As already mentioned in your question, you should initialize it in the appDelegate (I would use a property.)
MyAppDelegate.h :
#import <UIKit/UIKit.h>
#import "FBConnect.h"
#interface MyAppDelegate : UIResponder <UIApplicationDelegate>
...
#property (strong, nonatomic) Facebook *facebook;
#end
MyAppDelegate.m :
#import "MyAppDelegate.h"
#implementation MyAppDelegate
#synthesize facebook = _facebook;
-(Facebook *)facebook
{
if(!_facebook){
_facebook = [Facebook alloc] initWithAppId:MY_APP_ID andDelegate:self];
}
return _facebook;
}
-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
return [self.facebook handleOpenURL:url];
}
Then in any ViewController that you would want you could have a Facebook property that you would not alloc/init but you would ask it from the appDelegate this way
AnyViewController.h :
#import "MyAppDelegate.h"
#interface AnyViewController : UIViewController
...
#property (strong, nonatomic) Facebook *facebook;
#end
AnyViewController.m :
#import "AnyViewController.h"
#implementation AnyViewController
#synthesize facebook = _facebook;
-(Facebook *)facebook
{
if(!_facebook){
MyAppDelegate *theAppDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
_facebook = theAppDelegate.facebook;
}
return _facebook;
}
And of course the different ViewControllers you'll be using should implement the different SDK protocols depending on your needs
You should have a look at the sample project by Facebook that's called Hackbook

AppDelegate never being called on iphone/ipad

I am developing an app that runs on both ipad/iphone, so for ipad
I am using a view controller in mainWindow_ipad.xib and then I am loading a new view using navigation vc, but I cant access my appDelegate method. The app directly starts and loads the newView which I am loading
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { }
How can I ensure this method is called? I am loading mainWindow_ipad.xib from info.plist
Update
this is my .m file
#implementation Ihope_test_sqlAppDelegate_iPad
#dynamic aNav;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
NSLog(#"IPAD");// but i cant find this log output
}
this is my .h file
#import "Ihope_test_sqlAppDelegate.h"
#class RootView;
#interface Ihope_test_sqlAppDelegate_iPad : Ihope_test_sqlAppDelegate
{
IBOutlet UINavigationController *aNav;
}
//#property (nonatomic, retain) UITextField *usernameField;
//#property (nonatomic, retain) UITextField *passwordField;
#property (nonatomic, retain) IBOutlet UINavigationController *aNav;
-(IBAction)removeKeyboard;
#end
Check that the class of the app delegate in mainWindow_ipad.xib is of the right class. You need to make sure that the MainWindow nib file for iPad has an App Delegate that is of the Ihope_test_sqlAppDelegate_iPad class rather than Ihope_test_sqlAppDelegate.

Custom delegate not working

I have started working more with delegate as suggested in another question I made. Now, I have made a UIViewController called ProfileViewController in which I would like to load the News Feed from Facebook. I also have subclass of NSObject called FBFeed. This subclass loads the Facebook News Feed and should pass it back to the view controller. All my requests to Facebook are sent through a singleton named FBRequestWrapper which (in this case) should pass the result to FBFeed.
I call getFBRequestWithGraphPath:andDelegate: in FBRequestWrapper which will call a method in the Facebook iOS SDK which takes a delegate as a parameter. If I put in self (which will be the FBRequestWrapper) it works just fine. If I put in _delegate (which is an instance of FBFeed) the application crashes with EXC_BAD_ACCESS.
I have a theory why it might crash. I have read that
#property (nonatomic, retain) id <FBFeedDelegate> delegate;
should be
#property (nonatomic, assign) id <FBFeedDelegate> delegate;
But when doing so I get the following error:
Existing ivar 'delegate' for unsafe_unretained property 'delegate'
must be __unsafe_unretained
Also, I don't know if that enough to make the application crash.
Here is my code.
ProfileViewController.h
#import <UIKit/UIKit.h>
#import "FeedTableView.h"
#import "FBFeed.h"
#interface ProfileViewController : UIViewController <FBFeedDelegate>
{
FeedTableView *tableView;
}
- (void)loadFeed;
#end
ProfileViewController.m
#implementation ProfileViewController
- (void)loadFeed
{
FBFeed *feed = [[FBFeed alloc] init];
[feed loadNewsFeedWithDelegate:self];
}
#pragma mark -
#pragma FBFeedDelegate
- (void)finishedLoadingFeed:(FBFeed *)_feed
{
NSLog(#"ProfileViewController: FBFeed Finished.");
}
- (void)failedToLoadFeed:(FBFeed *)_feed
{
NSLog(#"ProfileViewController: FBFeed Failed.");
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Load feed
[self loadFeed];
}
#end
FBFeed.h
#import <Foundation/Foundation.h>
#import "FBRequestWrapper.h"
#protocol FBFeedDelegate;
#interface FBFeed : NSObject <FBRequestDelegate>
{
id <FBFeedDelegate> delegate;
}
#property (nonatomic, retain) id <FBFeedDelegate> delegate;
- (void)loadNewsFeedWithDelegate:(id)_delegate;
#end
#protocol FBFeedDelegate <NSObject>
#required
- (void)finishedLoadingFeed:(FBFeed *)_feed;
- (void)failedToLoadFeed:(FBFeed *)_feed;
#end
FBFeed.m
#import "FBFeed.h"
#implementation FBFeed
#synthesize delegate;
- (void)loadNewsFeedWithDelegate:(id)_delegate
{
self.delegate = _delegate;
[[FBRequestWrapper defaultManager] getFBRequestWithGraphPath:#"me" andDelegate:self];
}
#pragma mark -
#pragma mark FBRequest Delegate
- (void)request:(FBRequest *)request didLoad:(id)result
{
NSLog(#"FBFeed: FBRequest Did Load.");
NSLog(#"%#", result);
}
- (void)request:(FBRequest *)request didFailWithError:(NSError *)error
{
NSLog(#"FBFeed: FBRequest Failed.");
NSLog(#"%#", error);
}
#end
FBRequestWrapper.h
#import <Foundation/Foundation.h>
#import "FBConnect.h"
#interface FBRequestWrapper : NSObject <FBRequestDelegate, FBSessionDelegate>
{
Facebook *facebook;
BOOL isLoggedIn;
}
#property (nonatomic, assign) BOOL isLoggedIn;
+ (id)defaultManager;
- (void)setIsLoggedIn:(BOOL)_loggedIn;
- (void)FBSessionBegin:(id<FBSessionDelegate>)_delegate;
- (void)FBLogout;
- (void)getFBRequestWithGraphPath:(NSString *)_path andDelegate:(id)_delegate;
- (void)getFBRequestWithMethodName:(NSString *)_methodName andParams:(NSMutableDictionary *)_params andDelegate:(id)_delegate;
#end
FBRequestWrapper.m
#import "FBRequestWrapper.h"
static FBRequestWrapper *defaultWrapper = nil;
#implementation FBRequestWrapper
#synthesize isLoggedIn;
+ (id)defaultManager
{
if(!defaultWrapper)
{
defaultWrapper = [[FBRequestWrapper alloc] init];
}
return defaultWrapper;
}
- (void)getFBRequestWithGraphPath:(NSString *)_path andDelegate:(id)_delegate
{
if (_path != nil)
{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
if (_delegate == nil)
{
_delegate = self;
}
[facebook requestWithGraphPath:_path andDelegate:_delegate];
}
}
#pragma mark -
#pragma mark FBRequestDelegate
- (void)request:(FBRequest *)request didLoad:(id)result
{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
NSLog(#"%#", result);
}
- (void)request:(FBRequest *)request didFailWithError:(NSError *)error
{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
NSLog(#"FBRequest Failed: %#", error);
}
#end
I guess you are using ARC and the new iOS5 SDK which is still under NDA. Instead of using retain, use the Keyword 'strong'. Using strong will have the same behaviour intended as using retain. Set up your property like this:
#property (nonatomic, strong) id <FBFeedDelegate> delegate;
I think there are two ways for you:
Don't use ARC and leave your code as it was
If you want to use ARC, be aware that delegates normally shouldn't be retained. But when using ARC, an ivar declaration like this:
id <FBFeedDelegate> delegate
defaults to
id <FBFeedDelegate> __strong delegate
So when you declare a property like this:
#property (readwrite, unsafe_unretained) id <FBFeedDelegate> delegate
The ivar should look like this:
id <FBFeedDelegate> __unsafe_unretained delegate
I had the same problem but then I was able to make it work correctly under iOS 5.1 on XCode 4.3.2
Use this code in your .h file
#protocol AnimationManagerCountdownTimerDelegate <NSObject>
#required
-(void)countdownCompleted;
#end
#interface AnimationManager : NSObject{
....
}
#property (nonatomic, strong) id <AnimationManagerCountdownTimerDelegate> countdownTimerDelegate;
#end
Then in the .m file you simply synthesize:
#synthesize countdownTimerDelegate;

Undeclared Error and iPhone Simulator Crashing

Here's my code:
delegate.h
#import <UIKit/UIKit.h>
#class _4_Control_FunViewController;
#interface _4_Control_FunAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
_4_Control_FunViewController *viewController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet _4_Control_FunViewController *viewController;
#end
delegate.m:
#import "_4_Control_FunAppDelegate.h"
#import "_4_Control_FunViewController.h"
#implementation _4_Control_FunAppDelegate
#synthesize window;
#synthesize viewController;
#pragma mark -
#pragma mark Application lifecycle
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions {
[window addSubview:viewController.view];
[window makeKeyAndVisible];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
}
- (void)applicationWillTerminate:(UIApplication *)application {
}
#pragma mark -
#pragma mark Memory management
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
}
- (void)dealloc {
[viewController release];
[window release];
[nameField release];
[numberField release];
[super dealloc];
}
#end
ViewController.h
#import <UIKit/UIKit.h>
#interface _4_Control_FunViewController : UIViewController {
UITextField *nameField;
UITextField *numberField;
}
#property (nonatomic, retain) IBOutlet UITextField *nameField;
#property (nonatomic, retain) IBOutlet UITextField *numberField;
#end
ViewController.m
#import "_4_Control_FunViewController.h"
#implementation _4_Control_FunViewController
#synthesize nameField;
#synthesize numberField;
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
#end
This is what I'm trying to get:
This is what I'm getting:
Looks like it "should" work, but based on your screenshot, I'd say you haven't got things connected correctly in Interface Builder.
Go to Interface Builder and make sure they are hooked up. Where ever you have an IBOutlet declared in a header file, that needs to be hooked up to the corresponding UI object in Interface Builder.
You also have some redundant code (shown below). They are not causing problems, but they double up because you have them declared as properties at the bottom your .h file. You can delete the lines below from your headers, but make sure you leave their corresponding #property declarations in place.
UIWindow *window;
_4_Control_FunViewController *viewController;
UITextField *nameField;
UITextField *numberField;

Loading a new view before the old one iphone sdk

Probably just a simple question, but ive been building a little puzzle game and no decided i could do with a menu for it. Ive created the menu no problem i just cant get it to show before the puzzle. Ive tried changing the code in the appdelegate but its not liking it.
slider appdelegate.h
#import <UIKit/UIKit.h>
#class SliderViewController;
#class MainMenu;
#interface SliderAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
MainMenu *viewController1;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet SliderViewController *viewController;
#property (nonatomic, retain) IBOutlet MainMenu *viewController1;
#end
Slider appdelegate.m
#import "SliderAppDelegate.h"
#import "SliderViewController.h"
#implementation SliderAppDelegate
#synthesize window;
#synthesize viewController;
#synthesize viewController1;
#pragma mark -
#pragma mark Application lifecycle
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// Add the view controller's view to the window and display.
[self.window addSubview:viewController1.view];
[self.window makeKeyAndVisible];
return YES;
}
But doing that throws out an error:
error: request for member 'view' in
something not a structure or union
I know its probably a stupid question but could anyone help please.
You need to import that class and make alloc the object first see this
Slider appdelegate.m
#import "SliderAppDelegate.h"
#import "SliderViewController.h"
#import "MainMenu.h"//change here
#implementation SliderAppDelegate
#synthesize window;
#synthesize viewController;
#synthesize viewController1;
#pragma mark -
#pragma mark Application lifecycle
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
viewController = [[[MainMenu alloc] initWithNibName#"MainMenu" bundle:nil] autorelease]; // and here
[self.window addSubview:viewController1.view];
[self.window makeKeyAndVisible];
return YES;
}