i have been looking at tutorials for weeks now, not been able to find ANY storyboard examples implementing FB in iOS.
i got my app to open the FBloginscreen, request user auth for app, and then return to app...
like some other users , fbdidlogin and handleopenurl method are not called. i am sure i am doing something wrong but just don't know what.
i am using the default view controller in my storyboard (i did not create a vc programatically
) so i might need to do something with that.
from what i understand that handleopenurl method needs to be in my app delegate.m file and not in my view controller.m file, but i am not sure how it should be written and how to connect a UIViewController object i create to the VC in my storyboard (the one with the buttons and labels that i am using).
ViewController.h (relevant to FB)
#import "FBConnect.h"
#interface ViewController : UIViewController
<UIImagePickerControllerDelegate,
UINavigationControllerDelegate, FBSessionDelegate, FBDialogDelegate>
{
Facebook *facebook;
}
#property (nonatomic,retain) Facebook *facebook;'
ViewController.m (relevant to FB)
#import "ViewController.h"
#implementation ViewController;
#synthesize facebook;
-(void)LogintoFB
{
facebook = [[Facebook alloc] initWithAppId:#"345872345487883" andDelegate:self];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:#"FBAccessTokenKey"]
&& [defaults objectForKey:#"FBExpirationDateKey"]) {
facebook.accessToken = [defaults objectForKey:#"FBAccessTokenKey"];
facebook.expirationDate = [defaults objectForKey:#"FBExpirationDateKey"];
}
if (![facebook isSessionValid]) {
[facebook authorize:nil];
NSLog(#"did log in");
}
}
// Pre iOS 4.2 support
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
NSLog(#"-4.2 got calleld");
return [facebook handleOpenURL:url];
}
// For iOS 4.2+ support
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
NSLog(#"4.2+ got called");
return [facebook handleOpenURL:url];
}
}
- (void)fbDidLogin {
NSLog(#"fbDidLogin");
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:#"FBAccessTokenKey"];
[defaults setObject:[facebook expirationDate] forKey:#"FBExpirationDateKey"];
[defaults synchronize];
[facebook dialog:#"feed" andDelegate:self];
}'
also if you can direct me to a tutorial that used storyboard when implementing Facebook in iOS that would be great.
thank you!
-(BOOL)application:openURL:sourceApplication:annotation: should be implemented in the app delegate.
Add a property to the appDelegate :
#property (nonatomic, strong) Facebook *facebook;
Synthesize it!
In your logintoFB method add this after creating the Facebook object
(AppDelegate should be whatever your appDelegate class name is)
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
appDelegate.facebook = facebook;
Related
There should be a registration page with Full Name, User Name, Password, Email and Register Button.
When we Click on Register Button we then go to Login Page where it validate the username and password that we created in registration page using NSUserDefaults.
Now if we give right credentials then it should redirect to logout page and when we next time open the app it should directly redirect to logout page.
Data Should be stored locally using NSUserDefaults(value should be stored in string format).
Login view controller .h
#import <UIKit/UIKit.h>
#interface ViewController2 : UIViewController
#property(strong, nonatomic)NSString *dataString;
#property(strong, nonatomic)NSString *dataString2;
#property (strong, nonatomic) IBOutlet UILabel *lblOutput;
#property (strong, nonatomic) IBOutlet UITextField *inputTxt1;
#property (strong, nonatomic) IBOutlet UITextField *inputTxt2;
- (IBAction)btnAction:(id)sender;
#end
Login view controller .m
#import "ViewController2.h"
#interface ViewController2 ()
#end
#implementation ViewController2
- (void)viewDidLoad {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *firstName = [defaults objectForKey:#"firstName"];
NSString *lastName = [defaults objectForKey:#"lastname"];
firstName = _inputTxt1.text;
lastName = _inputTxt2.text;
[super viewDidLoad];
NSLog(#"%#",self.dataString);
NSLog(#"%#",self.dataString2);
self.navigationItem.title = #"Login Page";
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)btnAction:(id)sender {
NSString *firstName = [_inputTxt1 text];
NSString *lastName = [_inputTxt2 text];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:firstName forKey:#"firstName"];
[defaults setObject:lastName forKey:#"lastname"];
[defaults synchronize];
// [Defaults setObject:#"Eezy"forKey:#"iOS"];
NSLog(#"%#",[defaults stringForKey:#"firstName"]);
NSLog(#"%#",[defaults stringForKey:#"lastName"]);
if ([self.dataString isEqualToString:[defaults objectForKey:#"firstName"]] && [self.dataString2 isEqualToString:[defaults objectForKey:#"lastName"]])
{
NSLog(#"goog");
[self performSegueWithIdentifier:#"segueToNextPage2" sender:self];
}
else
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Hey Listen" message:#"Wrong User Name or Password" delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show];
}
}
#end
The problem is that I have passed the value from registration page but when I am storing it in NSUserDefault then it is storing the value of username but in case of password it is showing null value.
FOR Saving login values you can use
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// saving an NSString
[prefs setObject:txtUsername.text forKey:#"userName"];
[prefs setObject:txtPassword.text forKey:#"password"];
[prefs synchronize];
FOR RETRIEVING Values
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// getting an NSString
NSString *savedUsername = [prefs stringForKey:#"userName"];
NSString *savedPassword = [prefs stringForKey:#"password"];
For logout:
[[NSUserDefaults standardUserDefaults] removeObjectForKey:#"Your Key"];
Here is the Solution i got :-
Step 1: In login page define a bool variable and set its value as true on clicking the login button
- (IBAction)btnAction:(id)sender {
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setBool:YES forKey:#"registered"];
[prefs synchronize];
}
Step 2. Create a storyBoard in identity inspector as "UITabBarController". Keep in mind this will be the page where you want to go.
Step 3. In AppDelegate.h file just below #implemention write following code :-
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if ([[NSUserDefaults standardUserDefaults] boolForKey:#"registered"]) {
UITabBarController *controller = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:#"UITabBarController"];
self.window.rootViewController = controller;
}
return YES;
}
That's it very simple.
Save the details in NSUserDefaults from registration page. Also make entry in NSUserDefaults to check whether user logged in or not.
[[NSUserDefaults standardUserDefaults]setValue:#"YES" forKey:#"isLoggedIn"];
In your AppDelegate's didFinishLaunching method, change the initialViewController based on isLoggedIn key value e.g.
if ([[[NSUserDefaults standardUserDefaults]valueForKey:#"isLoggedIn"] isEqualToString:#"YES"])
{
// redirect to logout screen
}
else
{
//show login or register screen
}
Passing data just isn't doing the trick...I need to pull string data from a view controller when a function in the AppDelegate is called. Here's the code:
In App Delegate:
- (void)applicationDidEnterBackground:(UIApplication *)application
{
AMViewController *viewController = [[AMViewController alloc] initWithNibName:#"AMViewController" bundle:nil];
self.xpData = viewController.xpLabel.text;
NSLog(#"Value of xpString in AD: %#", self.xpData);
}
In my ViewController, I'm not using an action to pass/retrieve the string. I'm basically pulling it from the ViewController when the user hits the home button. I'm doing this because I'd like to save my data on exit. Thanks!
In your AppDelegate.h
#property (atomic, copy) NSString *yourString;
Where you have changed xpLabel.text, do this
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.yourString = xpLabel.text;
Now in
- (void)applicationDidEnterBackground:(UIApplication *)application
{
NSLog(#"Value of xpString in AD: %#", self.yourString);
}
Another Method
Set your value in AMViewController class like this
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:xpLabel.text forKey:#"yourkey"];
[prefs synchronize];
Get your Value in applicationDidEnterBackground like this
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
yourString = [prefs stringForKey:#"yourkey"];
Did you tried working with NSNotificationCenter
Or shared delegate:
http://www.roostersoftstudios.com/2011/04/12/simple-delegate-tutorial-for-ios-development/
http://coderchrismills.wordpress.com/2011/05/05/basic-delegate-example/
I followed the Facebook SDK tutorial from Facebook step by step. I have everything working besides fbDidLogin getting called... the authorization process works fine. I've been told to do:
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
return [[controller facebook] handleOpenURL:url];
}
from fbDidLogin not called
the problem is is that I'm not exactly sure how to alloc init my view controller. Here is my .h for app delegate:
#import <UIKit/UIKit.h>
#import "FBConnect.h"
#interface fb2AppDelegate : UIResponder <UIApplicationDelegate, FBSessionDelegate> {
Facebook *facebook;
UIViewController *fb2ViewController;
}
#property (strong, nonatomic) UIWindow *window;
#property (strong,nonatomic) Facebook *facebook;
#property (nonatomic, strong) UIViewController *fb2ViewController;
#end
and the .m :
#import "fb2AppDelegate.h"
#implementation fb2AppDelegate
#synthesize window = _window;
#synthesize facebook;
#synthesize fb2ViewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.fb2ViewController = [[UIViewController alloc] init];
facebook = [[Facebook alloc] initWithAppId:#"key_here" andDelegate:self];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:#"FBAccessTokenKey"]
&& [defaults objectForKey:#"FBExpirationDateKey"]) {
facebook.accessToken = [defaults objectForKey:#"FBAccessTokenKey"];
facebook.expirationDate = [defaults objectForKey:#"FBExpirationDateKey"];
}
if (![facebook isSessionValid]) {
[facebook authorize:nil];
}
return YES;
}
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
return [[fb2ViewController facebook] handleOpenURL:url];
}
- (void)fbDidLogin {
NSLog(#"fbdidlogin");
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:#"FBAccessTokenKey"];
[defaults setObject:[facebook expirationDate] forKey:#"FBExpirationDateKey"];
[defaults synchronize];
NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"key_here", #"app_id",
#"http://developers.facebook.com/docs/reference/dialogs/", #"link",
#"http://fbrell.com/f8.jpg", #"picture",
#"Facebook Dialogs", #"name",
#"Reference Documentation", #"caption",
#"Using Dialogs to interact with users.", #"description",
nil];
[facebook dialog:#"feed" andParams:params andDelegate:self];
}
as for the view controllers... I haven't put any code in them.
the error that I am receiving is:
no visible #interface for 'UIViewController' declares the selector 'Facebook'
what am i doing wrong?
thx!
Okay, You need to do:
[facebook handleOpenURL:url].
You are currently using facebook as a selector instead of an object.
Also.. you need to set facebook.sessionDelegate = self, so that the delegate method fbDidLogin can get called.
Hope this helps!
The problem is that the runtime believes that your UIViewController has the class method +facebook, when in reality, that was an example singleton the other question's answer used (rather confusingly I might add). You need to send it to an instance of the Facebook object and call your code from there.
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
return [facebook handleOpenURL:url];
}
Also remember that in order for delegate methods to be called, your objects actually has to be a delegate. So set facebook.sessionDelegate = self and conform to the protocol in the header with <FBSessionDelegate> to silence the compiler warnings.
I am a newbie for StackOverflow and iOS. I am trying get user informations like groups, books etc. and I need use the Facebook graph API for that. To use graph API you must use an access token. I read article on the Facebook developer page about this topic and I wrote some code, but it didn't work. Can you help me? Is my code right or shall I use another way?
my .h file:
#import <UIKit/UIKit.h>
#import "FBConnect.h"
#interface FirstViewController : UIViewController <FBSessionDelegate,FBLoginDialogDelegate,FBRequestDelegate>{
Facebook *face;
}
#property(nonatomic,retain)Facebook *face;
-(IBAction)facePushed:(id)sender;
-(IBAction)logoutPushed:(id)sender;
my .m file:
-(IBAction)facePushed:(id)sender{
NSLog(#"FacePushed Began");
face=[[Facebook alloc]initWithAppId:#"313714785358239" andDelegate:self];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:#"FBAccessTokenKey"]
&& [defaults objectForKey:#"FBExpirationDateKey"]) {
face.accessToken = [defaults objectForKey:#"FBAccessTokenKey"];
face.expirationDate = [defaults objectForKey:#"FBExpirationDateKey"];
}
if (![face isSessionValid]) {
[face authorize:nil ];
}
else {
NSLog(#"Session is valid");
}
NSLog(#"FacePushed End");
}
}
- (void)fbDidLogin {
NSLog(#"aslan");
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[face accessToken] forKey:#"FBAccessTokenKey"];
[defaults setObject:[face expirationDate] forKey:#"FBExpirationDateKey"];
NSLog(#"%#", [face accessToken]);
[defaults synchronize];
}
- (void)fbDialogLogin:(NSString *)token expirationDate:(NSDate *)expirationDate {
NSLog(#"%#",token);
}
you have to check the following in your implementation :
you need to get a permissions before you authorize your app
if (![face isSessionValid])
{
NSArray *permissions = [[NSArray alloc] initWithObjects:
#"user_likes",
#"read_stream",
#"publish_stream",
nil];
[face authorize:permissions];
[permissions release];
}
you need to add the following functions in your application delegate(the facebook object here is your instance value of FaceBook Class).
// Pre 4.2 support
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
return [facebook handleOpenURL:url];
}
// For 4.2+ support
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
return [facebook handleOpenURL:url];
}
in your info.plist add to URL types > URL Schemes > your facebok app ID with fb prefix (finally your value will be like this fb313714785358239).
Good Luck.
I have a problem with connecting to my main view after logging in with the Facebook Connect sdk with my app. After a user logs in I want it to essentially load up the main view of my application (a camera), but so far I can't figure out how to do it. At the moment it just logs in and I am taken to a blank black screen, and I can't reach my view. Before I added the Facebook functionality I could reach my main view no problem.
Below is my appDelegate.h and appDelegate.m code, I can add the viewController code and class if it will help, my xib file is called "ViewController".
appDelegate.h
#import "FBConnect.h"
#import <UIKit/UIKit.h>
#class ViewController;
#interface AppDelegate : UIResponder <UIApplicationDelegate, FBSessionDelegate>
{
Facebook *facebook;
}
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) ViewController *viewController;
#property (nonatomic, retain) Facebook *facebook;
#end
appDelegate.m
#import "AppDelegate.h"
#import "ViewController.h"
#implementation AppDelegate
#synthesize window = _window;
#synthesize viewController = _viewController;
#synthesize facebook;
- (void)dealloc
{
[_window release];
[_viewController release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
//if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
// self.ViewController = [[[ViewController alloc] initWithNibName:#"nil" bundle:nil] autorelease];
//}
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
facebook = [[Facebook alloc] initWithAppId:#"326350974080015" andDelegate:self];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:#"FBAccessTokenKey"]
&& [defaults objectForKey:#"FBExpirationDateKey"]) {
facebook.accessToken = [defaults objectForKey:#"FBAccessTokenKey"];
facebook.expirationDate = [defaults objectForKey:#"FBExpirationDateKey"];
}
if (![facebook isSessionValid]) {
NSArray *permissions = [[NSArray alloc] initWithObjects:
#"user_photos",
nil];
[facebook authorize:permissions];
[permissions release];
}
return YES;
}
- (void)fbDidLogin {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:#"FBAccessTokenKey"];
[defaults setObject:[facebook expirationDate] forKey:#"FBExpirationDateKey"];
[defaults synchronize];
self.ViewController = [[[ViewController alloc] initWithNibName:#"ViewController" bundle:nil] autorelease];
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
return [facebook handleOpenURL:url];
}
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
return [facebook handleOpenURL:url];
}
#end
I am also getting this error when I try and run my app:
2012-03-01 21:04:51.103 friendSpotted[619:f803] Applications are expected to have a root view controller at the end of application launch
I actually have code for this in my appDelegate.m
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
Before self.window.rootViewController = self.viewController; you need to initialize the view controller self.viewController = [[UIViewController alloc] initWithNibName:#"ViewController" bundle:nil]; assuming your xib is called "ViewController.xib"
EDIT
Also, I would put all the Facebook related stuff in your view controller not the app delegate