gpt4 book ai didi

objective-c - 标签栏 Controller 和 Facebook Connect 问题 Xcode 4.3 IOS 5.1

转载 作者:行者123 更新时间:2023-12-01 19:19:14 25 4
gpt4 key购买 nike

我正在尝试连接到 facebook 并获取用户名和图片,到目前为止我可以连接并获取用户名。我有一个工作代码可以从这里下载 (single view application)

但是,如果我将标签栏 Controller 放入代码中,它就无法收到来自 facebook 的响应。

我以编程方式添加了一个标签栏 Controller ,如下面的代码
在应用程序.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Regular Code
/*self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];

self.window.rootViewController = self.viewController;
*/
//Tab bar controller code
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
UIViewController *viewController1 = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
self.rootController = [[[UITabBarController alloc] init] autorelease];
self.rootController.viewControllers = [NSArray arrayWithObjects:viewController1, nil];
self.window.rootViewController = self.rootController;
[self.window makeKeyAndVisible];
return YES;
}

// This method will be used for iOS versions greater than 4.2.
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
return [[_viewController facebook] handleOpenURL:url];
}

如果我在handleOpenurl 行设置一个断点在NSUrl 中有这个= fbAPPID://authorize/#access_token=BABABSbbsabsbabb 我猜 access_token 意味着我已经成功登录。所以标签栏不会阻止我登录将我的 Facebook 方法放在 Viewcontroller.m 中

如果没有标签栏,当我添加标签栏和断点时,Viewcontroller.m 中的所有方法都很好,下面的 facebook 方法都不起作用。我没有收到任何错误。
-(void)request:(FBRequest *)request didLoad:(id)result
-(void)fbDidLogin

View Controller .m
#import "ViewController.h"
#import "FBConnect.h"
#import "Facebook.h"


@implementation ViewController
@synthesize btnLogin;
@synthesize btnPublish;
@synthesize lblUser;
@synthesize actView;
@synthesize facebook;
@synthesize permissions;
@synthesize isConnected;
@synthesize imageView;

// The alert view that will be shown while the game will upload to facebook.
UIAlertView *msgAlert;


- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}


-(void)checkForPreviouslySavedAccessTokenInfo{
// Initially set the isConnected value to NO.
isConnected = NO;

// Check if there is a previous access token key in the user defaults file.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:@"FBAccessTokenKey"] &&
[defaults objectForKey:@"FBExpirationDateKey"]) {
facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];

// Check if the facebook session is valid.
// If it’s not valid clear any authorization and mark the status as not connected.
if (![facebook isSessionValid]) {
[facebook authorize:nil];
isConnected = NO;
}
else {
isConnected = YES;
}
}
}


-(void)setLoginButtonImage{
UIImage *imgNormal;
UIImage *imgHighlighted;
UIImageView *tempImage;

// Check if the user is connected or not.
if (!isConnected) {
// In case the user is not connected (logged in) show the appropriate
// images for both normal and highlighted states.
imgNormal = [UIImage imageNamed:@"LoginNormal.png"];
imgHighlighted = [UIImage imageNamed:@"LoginPressed.png"];
}
else {
imgNormal = [UIImage imageNamed:@"LogoutNormal.png"];
imgHighlighted = [UIImage imageNamed:@"LogoutPressed.png"];
}

// Get the screen width to use it to center the login/logout button.
// We’ll use a temporary image view to get the appopriate width and height.
float screenWidth = [UIScreen mainScreen].bounds.size.width;
tempImage = [[UIImageView alloc] initWithImage:imgNormal];
[btnLogin setFrame:CGRectMake(screenWidth / 2 - tempImage.frame.size.width / 2, btnLogin.frame.origin.y, tempImage.frame.size.width, tempImage.frame.size.height)];

// Set the button’s images.
[btnLogin setBackgroundImage:imgNormal forState:UIControlStateNormal];
[btnLogin setBackgroundImage:imgHighlighted forState:UIControlStateHighlighted];

// Release the temporary image view.
[tempImage release];
}


-(void)showActivityView{
// Show an alert with a message without the buttons.
msgAlert = [[UIAlertView alloc] initWithTitle:@"My test app" message:@"Please wait..." delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
[msgAlert show];

// Show the activity view indicator.
actView = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0.0, 0.0, 40.0, 40.0)];
[actView setCenter:CGPointMake(160.0, 350.0)];
[actView setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
[self.view addSubview:actView];
[actView startAnimating];
}


-(void)stopShowingActivity{
[actView stopAnimating];
[msgAlert dismissWithClickedButtonIndex:0 animated:YES];
}


-(void)saveAccessTokenKeyInfo{
// Save the access token key info into the user defaults.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:@"FBAccessTokenKey"];
[defaults setObject:[facebook expirationDate] forKey:@"FBExpirationDateKey"];
[defaults synchronize];
}


#pragma mark - View lifecycle

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

// Set the permissions.
// Without specifying permissions the access to Facebook is imposibble.
permissions = [[NSArray arrayWithObjects:@"read_stream", @"publish_stream", nil] retain];

// Set the Facebook object we declared. We’ll use the declared object from the application
// delegate.
facebook = [[Facebook alloc] initWithAppId:@"331327710279153" andDelegate:self];

// Check if there is a stored access token.
[self checkForPreviouslySavedAccessTokenInfo];


// Depending on the access token existence set the appropriate image to the login button.
[self setLoginButtonImage];

// Specify the lblUser label's message depending on the isConnected value.
// If the access token not found and the user is not connected then prompt him/her to login.
if (!isConnected) {
[lblUser setText:@"Tap on the Login to connect to Facebook"];
}
else {
// Get the user's name from the Facebook account.
[facebook requestWithGraphPath:@"me" andDelegate:self];


}

// Initially hide the publish button.
[btnPublish setHidden:YES];
}


- (void)viewDidUnload
{
[self setBtnLogin:nil];
[self setLblUser:nil];
[self setBtnPublish:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}


-(void)request:(FBRequest *)request didReceiveResponse:(NSURLResponse *)response {
// Keep this just for testing purposes.
NSLog(@"received response");
}


-(void)request:(FBRequest *)request didLoad:(id)result{
// With this method we’ll get any Facebook response in the form of an array.
// In this example the method will be used twice. Once to get the user’s name to
// when showing the welcome message and next to get the ID of the published post.
// Inside the result array there the data is stored as a NSDictionary.
if ([result isKindOfClass:[NSArray class]]) {
// The first object in the result is the data dictionary.
result = [result objectAtIndex:0];
}



// Check it the “first_name” is contained into the returned data.
if ([result objectForKey:@"first_name"]) {
// If the current result contains the "first_name" key then it's the user's data that have been returned.
// Change the lblUser label's text.
[lblUser setText:[NSString stringWithFormat:@"Welcome %@!", [result objectForKey:@"first_name"]]];
// Show the publish button.
[btnPublish setHidden:NO];
}
else if ([result objectForKey:@"id"]) {
// Stop showing the activity view.
[self stopShowingActivity];

// If the result contains the "id" key then the data have been posted and the id of the published post have been returned.
UIAlertView *al = [[UIAlertView alloc] initWithTitle:@"My test app" message:@"Your message has been posted on your wall!" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[al show];
[al release];
}
}


-(void)request:(FBRequest *)request didFailWithError:(NSError *)error{
NSLog(@"%@", [error localizedDescription]);

// Stop the activity just in case there is a failure and the activity view is animating.
if ([actView isAnimating]) {
[self stopShowingActivity];
}
}


-(void)fbDidLogin{
// Save the access token key info.
[self saveAccessTokenKeyInfo];

// Get the user's info.
[facebook requestWithGraphPath:@"me" andDelegate:self];
}


-(void)fbDidNotLogin:(BOOL)cancelled{
// Keep this for testing purposes.
//NSLog(@"Did not login");

UIAlertView *al = [[UIAlertView alloc] initWithTitle:@"My test app" message:@"Login cancelled." delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[al show];
}


-(void)fbDidLogout{
// Keep this for testing purposes.
//NSLog(@"Logged out");

// Hide the publish button.
[btnPublish setHidden:YES];
}


- (IBAction)LoginOrLogout {
// If the user is not connected (logged in) then connect.
// Otherwise logout.
if (!isConnected) {
[facebook authorize:permissions];

// Change the lblUser label's message.
[lblUser setText:@"Please wait..."];
}
else {
[facebook logout:self];
[lblUser setText:@"Tap on the Login to connect to Facebook"];
}

isConnected = !isConnected;
[self setLoginButtonImage];
}

- (IBAction)Publish {
// Show the activity indicator.
[self showActivityView];

// Create the parameters dictionary that will keep the data that will be posted.
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
@"My test app", @"name",
@"http://www.google.com", @"link",
@"FBTestApp app for iPhone!", @"caption",
@"This is a description of my app", @"description",
@"Hello!\n\nThis is a test message\nfrom my test iPhone app!", @"message",
nil];

// Publish.
// This is the most important method that you call. It does the actual job, the message posting.
[facebook requestWithGraphPath:@"me/feed" andParams:params andHttpMethod:@"POST" andDelegate:self];
}

- (void)viewWillAppear:(BOOL)animated
{

[super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{

[super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (void)dealloc {
[btnLogin release];
[lblUser release];
[btnPublish release];
[actView release];
[facebook release];
[permissions release];
[super dealloc];
}

@end

我很困惑如何使这个标签栏控制工作。

最佳答案

我改变了设计

现在登录 View 首先显示,然后它继续到具有标签栏 Controller 的真实应用程序。

问题是 _viewcontroller facebook,当 _viewcontroller 被调用时它没有返回任何东西

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
return [[_viewController facebook] handleOpenURL:url];
}

所以我将我的代码(尤其是 _viewcontroller 行)更改为以下代码
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

UIViewController * viewController1 = [[[VoteMe alloc] initWithNibName:@"VoteMe" bundle:nil] autorelease];
UIViewController *viewController2 = [[[PostController alloc] initWithNibName:@"PostController" bundle:nil] autorelease];
UIViewController *viewController3 = [[[FriendsController alloc] initWithNibName:@"FriendsController" bundle:nil] autorelease];
UIViewController *viewController4 = [[[Responses alloc] initWithNibName:@"Responses" bundle:nil] autorelease];
UIViewController *viewController5 = [[[User alloc] initWithNibName:@"User" bundle:nil] autorelease];


self.rootController = [[[UITabBarController alloc] init] autorelease];
self.rootController.viewControllers = [NSArray arrayWithObjects:viewController1,viewController2,viewController3,viewController4,viewController5, nil];
self.window.rootViewController = self.rootController;


[self.window makeKeyAndVisible];


loginView=[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
[self.window addSubview:loginView.view];

return YES;

}

// This method will be used for iOS versions greater than 4.2.
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
return [[loginView facebook] handleOpenURL:url];
}

它工作正常

关于objective-c - 标签栏 Controller 和 Facebook Connect 问题 Xcode 4.3 IOS 5.1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11129606/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com