Xcode / Objective-C Facebook Delegate Attempt - objective-c

I'm trying to understand how a delegate works by using the Facebook SDK.
I have this as my view controller:
#import <UIKit/UIKit.h>
#import "FBConnect.h"
#import "FacebookClass.h"
#interface ViewController : UIViewController
#property (nonatomic, retain) Facebook *facebook;
- (IBAction)btnPress:(id)sender;
#end
#import "ViewController.h"
#implementation ViewController
#synthesize facebook;
- (void)viewDidLoad
{
[super viewDidLoad];
FacebookClass *delegate = [[FacebookClass alloc] init];
facebook = [[Facebook alloc] initWithAppId:#"228051460617035" andDelegate:delegate];
// Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)btnPress:(id)sender {
[facebook authorize:nil];
}
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
return [self.facebook handleOpenURL:url];
}
...and other default viewcontroller methods
Then in my FacebookClass ..which I want to use as the delegate...I have:
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "FBConnect.h"
#interface FacebookClass : NSObject
<UIApplicationDelegate, FBSessionDelegate>
#end
#import "FacebookClass.h"
#implementation FacebookClass
- (void)fbDidLogin {
NSLog(#"Logged in");
}
- (void)fbDidExtendToken:(NSString*)accessToken
expiresAt:(NSDate*)expiresAt {
NSLog(#"Extended it");
}
- (void)fbDidNotLogin:(BOOL)cancelled
{
NSLog(#"Did not login");
}
- (void)fbDidLogout
{
NSLog(#"Logged out");
}
- (void)fbSessionInvalidated
{
NSLog(#"Session not valid");
}
#end
So what I want to happen is for when I log in, I want it to log the string "Logged in" in the console. As of now, it doesn't. Clearly, I'm confused.
Update:
Added delegate instantiation.

Your object FBDelegate is nil. It is never instanced. You should in your
- (void)viewDidLoad
method instance it doing:
FBDelegate = [[FacebookClass alloc] init];

Related

delegate: no response, where is the secret?

I have a little trouble with my delegate example. I created a very simple code to learn how delegates work. I know that my delegate not will be called but i can't figure out why?
So here is the complete code. Please tell me what i do wrong. It is really important for me to understand the error in this code.
First Viewcontroller: h.file
#import <UIKit/UIKit.h>
#protocol ViewControllerDelegate;
#interface ViewController : UIViewController
#property (nonatomic, retain) id<ViewControllerDelegate> delegate;
#end
#protocol ViewControllerDelegate <NSObject>
- (void)transfer:(ViewController *)data number:(NSUInteger)value;
#end
First Viewcontroller: m.file
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize delegate;
- (void)viewDidLoad
{
[super viewDidLoad];
if ([delegate respondsToSelector:#selector(transfer:number:)]){
[delegate transfer:self number:65];
NSLog(#"delegate called");
}
[delegate transfer:self number:65]; //Try to call without if-statement.
}
#end
SecondViewcontroller: h.file
#import <UIKit/UIKit.h>
#import "ViewController.h"
#interface SecondViewController : UIViewController <ViewControllerDelegate>
#end
SecondViewcontroller: m.file
#import "SecondViewController.h"
#interface SecondViewController ()
#end
#implementation SecondViewController
- (void)transfer:(ViewController *)data number:(NSUInteger)value
{
NSLog(#"received information from ViewController");
}
- (void)viewDidLoad
{
[super viewDidLoad];
ViewController *viewcontroller = [[ViewController alloc] init];
viewcontroller.delegate = self;
}
#end
In the storyboard i use two container views so both ViewControllers will shown.
Your current code is fine. The problem appears to be that you are never loading the view controllers view(s) so the viewDidLoad method isn't being called.
To test, push the viewcontroller, or just request viewcontroller.view.

CALayer _layer is not getting initialized on application startup?! (OSX, Cocoa)

I'm very new to AVFoundation and QuartzCore development and I'm having troubles with CALayers. I'm sorry if this is a silly problem.
Here's my code:
.h
#import <Cocoa/Cocoa.h>
#import <AVFoundation/AVFoundation.h>
#import <QuartzCore/QuartzCore.h>
#interface Document : NSPersistentDocument
{
AVPlayer *player;
AVPlayerLayer *playerLayer;
NSView *playerView;
}
#property AVPlayerLayer *playerLayer;
#property AVPlayer *player;
#property IBOutlet NSView *playerView;
#end
.m
#import "Document.h"
#implementation Document
#synthesize playerView;
#synthesize player;
#synthesize playerLayer;
- (id)init
{
self = [super init];
if (self) {
}
return self;
}
- (NSString *)windowNibName
{
return #"Document";
}
- (void)windowControllerDidLoadNib:(NSWindowController *)aController
{
[super windowControllerDidLoadNib:aController];
[[aController window] setMovableByWindowBackground:YES];
// HERE the layer is nill, and I don't understand why it's not getting initialized?!
[[[self playerView] layer] setBackgroundColor:CGColorGetConstantColor(kCGColorBlack)];
}
+ (BOOL)autosavesInPlace
{
return YES;
}
#end
Any kind of help is very appreciated!
If the layer is nil, you should start by suspecting that its parent playerView is nil. Is it? If so, you probably haven't hooked up the outlet in the nib. (I see you have declared playerView as an outlet in your code, but that doesn't mean you've configured the nib correctly.)

Multiple windows mac application

I want to make a multiple windows mac application and I am stuck with the multiple window part !
I can display some windows by creating a xib file and using this method :
-(void)popView:(NSString *)viewName {
_windowController = [[AddProdutWindowController alloc] initWithWindowNibName:viewName];
[_windowController showWindow:nil];
}
With
#property (strong, nonatomic) AddProdutWindowController *windowController;
in my header file and AddProductViewController inherit from NSWindowViewController
I have linked a subclass of NSViewController in Xcode to my xib file.
Now I want to send some datas to my new view and show them in some NSTextField and I have not a single clue how to do it !
I'm very confused with the WindowsController and ViewController, I don't exactly know how / where use them.
Thank you for helping.
Try this:
your firstWindowController.h:
#import <Cocoa/Cocoa.h>
#class SecondWindowController;
#interface ResultWindowController : NSWindowController{
SecondWindowController *swc;
}
#property (assign) NSNumber *xyz;
...
your firstWindowController.m:
#import "FirstWindowController.h"
#import "SecondWindowController.h"
#implementation FirstWindowController
#synthesize xyz;
- (IBAction)openSecondWindow:(id *)sender {
if (!swc)
{
swc = [[SecondWindowController alloc] init];
}
[Swc setFwc:self];
[Swc showWindow:self];
your secondWindowController.h:
#import <Cocoa/Cocoa.h>
#class FirstWindowController;
#interface SecondWindowController : NSWindowController {
FirstWindowController *fwc;
}
#property (retain) IBOutlet FirstWindowController *fwc;
#end
your secondWindowController.m:
#import "SecondWindowController.h"
#import "FirstWindowController.h"
#implementation SecondWindowController
#synthesize fwc;
- (id)init
{
if(![super initWithWindowNibName:#"SecondWindow"])
return nil;
NSLog(#"_init: %#", NSStringFromClass([self class]));
return self;
}
- (void)windowDidLoad
{
[super windowDidLoad];
NSLog(#"swc didload self=%p", self); //thats your second window controller
NSLog(#"fwc value is %#", fwd.xyz); // here you should be able to see the value from FirtsWindowController
}

Objective C Protocol Delegate test

I'm studing protocol and delegates using an Example! When I try to re create this example I notice that this condition is not respected:
if([delegate respondsToSelector:#selector(amountEntered:)]) {blabla}
where is the mistake? Scripts:
First View .h
#import <UIKit/UIKit.h>
#import "EnterAmountViewController.h"
#interface DelegateExampleViewController : UIViewController <EnterAmountDelegate>{
IBOutlet UILabel *amountLabel;
}
-(IBAction)changeAmountPressed;
#end
First View .m
#import "DelegateExampleViewController.h"
#implementation DelegateExampleViewController
-(IBAction)changeAmountPressed
{
EnterAmountViewController * enterAmountVC = [[EnterAmountViewController alloc]init];
enterAmountVC.delegate = self;
}
-(void)amountEntered:(NSInteger)amount
{
amountLabel.text = [NSString stringWithFormat:#"%i" , amount];
}
#end
Second View .h
#import <UIKit/UIKit.h>
#protocol EnterAmountDelegate <NSObject>
-(void)amountEntered:(NSInteger)amount;
#end
#interface EnterAmountViewController : UIViewController {
IBOutlet UITextField *amountTextField;
id<EnterAmountDelegate> delegate;
}
-(IBAction)savePressed;
#property(nonatomic,retain) id<EnterAmountDelegate> delegate;
#end
Second View .m
#import "EnterAmountViewController.h"
#import "DelegateExampleViewController.h"
#implementation EnterAmountViewController
#synthesize delegate;
- (void)viewDidLoad {
[super viewDidLoad];
amountTextField.text = #"";
[amountTextField becomeFirstResponder];
}
-(IBAction)savePressed
{
if([delegate respondsToSelector:#selector(amountEntered:)])
{
[delegate amountEntered:[amountTextField.text intValue]];
NSLog(#"rugg");
}
}
#end
Thanks in advance!
In the method:
-(IBAction)changeAmountPressed
{
EnterAmountViewController * enterAmountVC = [[EnterAmountViewController alloc]init];
enterAmountVC.delegate = self;
}
you are creating an instance of EnterAmountViewController on the stack as a local variable. This variable will be inaccessible at the end of the scope. So, when you execute savePressed you are doing it on a different object where you did not set delegate.
In other words, when you check
if([delegate respondsToSelector:#selector(amountEntered:)])
it returns NO because delegate is nil...
The correct way to set the delegate is using the prepareForSegue mechanism:
#implementation DelegateExampleViewController
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
[(EnterAmountViewController*)segue.destinationViewController setDelegate:self];
}
...
You do not need the changeAmountPressed method nor the relative binding.

ABPersonViewControllerDelegate shouldPerformDefaultActionForPerson not working

For some reason, my shouldPerformDefaultActionForPerson function never gets called, in the simulator and the device. Could someone look at this and tell me what I'm missing? From all the examples I've found, I can't see what I'm doing wrong.
Here is my header:
#import <Foundation/Foundation.h>
#import <AddressBookUI/AddressBookUI.h>
#import <MessageUI/MessageUI.h>
#import "SWO.h"
#interface CustomABPersonViewController : ABPersonViewController
<MFMessageComposeViewControllerDelegate, ABPersonViewControllerDelegate>
{
SWO *theSWO;
}
- (id)initWithSWO:(SWO *)aSWO;
#end
And here is my implementation file:
#import "CustomABPersonViewController.h"
#import <MessageUI/MessageUI.h>
#import "SWO.h"
#implementation CustomABPersonViewController
- (id)initWithSWO:(SWO *)aSWO
{
if (self = [super init])
{
//Various init code
}
return self;
}
- (BOOL)personViewController:(ABPersonViewController *)personView shouldPerformDefaultActionForPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)IdentifierForValue
{
NSLog(#"In here!");
return YES;
}
#end
And here is when I call in the view:
CustomABPersonViewController *view = [[CustomABPersonViewController alloc] initWithSWO:aSWO];
view.personViewDelegate = self;
view.displayedPerson = aRecord;
[self.navigationController setToolbarHidden:NO animated:NO];
[self.navigationController pushViewController:view animated:YES];
Many default actions do not work on the Simulator. You have to try them on an actual device.