Facebook Connect iPhone questions
I am using Facebook Connect for sharing something on my appliction. So I want to implement the Facebook Connect API with 2 buttons, "Login / share on Facebook" via UIACtionSheet
.
Now, I have some questions:
Assuming I have 2 buttons on the UIActionSheet
with title of "Share on Facebook" "Login".
I want, when the user have logged in on Facebook, my login button title to change to "Log Out". I know I should use this function:
- (void)session:(FBSession*)session didLogin:(FBUID)uid {
开发者_JAVA百科 //a code that change my login button title to LogOut
}
2- I have logged in Facebook. When I am going to go out from my applicaton and again open the application I should login again! How could I prevent this?
3- Finally I want to share some text from UIWebView to Facebook. My webview outlet names "myWeb". How could I connect Facebook Connect with UIWebView to share it?
Should I use
-(void)publishFeed:(id)target
?
#pragma mark FBDialog delegate methods
- (void)dialogDidSucceed:(FBDialog *)dialog {
if ([dialog isMemberOfClass:[FBLoginDialog class]]) {
NSLog(@"[FBLoginDialog::dialogDidSucceed] just did succeed");
} else if ([dialog isMemberOfClass:[FBPermissionDialog class]]) {
NSLog(@"[FBPermissionDialog::dialogDidSucceed] update user status");
[self facebookUpdateUserStatus];
}
}
- (void)dialogDidCancel:(FBDialog *)dialog {
}
- (void)dialog:(FBDialog *)dialog didFailWithError:(NSError *)error {
NSLog(@"dialog:%@ didFailWithError:%@", dialog, error);
}
#pragma mark FBSession delegate methods
- (void)session:(FBSession *)session didLogin:(FBUID)uid {
NSLog(@"User with id %lld logged in.", uid);
[self facebookCheckForPermission];
}
- (void)request:(FBRequest*)request didReceiveResponse:(NSURLResponse*)response {
NSLog(@"did r response");
}
- (void)request:(FBRequest*)request didLoad:(id)result {
if ([@"facebook.Users.hasAppPermission" isEqualToString: request.method]) {
if ([@"1" isEqualToString: result]) {
// post comment
NSLog(@"[Users.hasAppPermission::dialogDidSucceed] succeed, update status");
[self facebookUpdateUserStatus];
} else {
// show dialog
NSLog(@"[Users.hasAppPermission::dialogDidSucceed] fail, show dialog");
FBPermissionDialog* dialog = [[[FBPermissionDialog alloc] init] autorelease];
dialog.delegate = self;
dialog.permission = @"status_update";
[dialog show];
}
} else if ([@"facebook.Users.setStatus" isEqualToString: request.method]) {
if ([@"1" isEqualToString: result]) {
NSLog(@"facebook update did succeed");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Facebook"
message: @"L'article a bien été publié sur votre profil"
delegate: nil
cancelButtonTitle: @"OK"
otherButtonTitles: nil];
[alert show];
[alert release];
} else {
NSLog(@"facebook update did fail");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Facebook"
// Slava, change text here
message: @"Update did fail"
delegate: nil
cancelButtonTitle: @"OK"
otherButtonTitles: nil];
[alert show];
[alert release];
}
}
}
- (void)request:(FBRequest*)request didFailWithError:(NSError*)error {
NSLog(@"did fail: %@", [error localizedDescription]);
}
#pragma mark FBSession helper functions
- (FBSession *)fbSessionWithDelegate:(id)theDelegate {
if (nil != [FBSession session]) {
return [[FBSession session] retain]; // fuckup this leak =)
}
FBSession *session = [FBSession sessionForApplication: kFBAPIKeyEncoded
secret: kFBAPISecretEncoded
delegate: theDelegate];
return session;
}
- (void) facebookCheckForPermission {
NSLog(@"[facebookCheckForPermission] make a call");
NSDictionary *d = [NSDictionary dictionaryWithObjectsAndKeys: @"status_update", @"ext_perm", nil];
// [[FBRequest requestWithDelegate: self] call: @"facebook.Users.hasAppPermission" params: d];
FBSession *fbSession = [self fbSessionWithDelegate: self];
[[FBRequest requestWithSession: fbSession delegate: self] call: @"facebook.Users.hasAppPermission" params: d];
}
- (void) facebookUpdateUserStatus {
NSLog(@"[facebookUpdateUserStatus] updating status");
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys: [NSString stringWithFormat: @"Je te recommande cet article: %@", postURL],
@"status", @"true", @"status_includes_verb", nil];
FBSession *fbSession = [self fbSessionWithDelegate: self];
updateRequest = [FBRequest requestWithSession: fbSession delegate: self];
[updateRequest call: @"facebook.Users.setStatus" params: params];
}
Use [session resume] which returns YES if the user have used your application, otherwise it returns NO. By using this mehhod there is no need to do login again.
精彩评论