Ios Facebook发送请求而不打开目标C中的FBWebDialog
我想在不打开后台对话框的情况下向我所有的facebook好友发送请求。我怎么做?我使用了以下代码,但它不起作用Ios Facebook发送请求而不打开目标C中的FBWebDialog,ios,objective-c,facebook-graph-api,Ios,Objective C,Facebook Graph Api,我想在不打开后台对话框的情况下向我所有的facebook好友发送请求。我怎么做?我使用了以下代码,但它不起作用 if (!FBSession.activeSession.isOpen) { // if the session is closed, then we open it here, and establish a handler for state changes [FBSession openActiveSessionWithReadPermission
if (!FBSession.activeSession.isOpen) {
// if the session is closed, then we open it here, and establish a handler for state changes
[FBSession openActiveSessionWithReadPermissions:nil
allowLoginUI:YES
completionHandler:^(FBSession *session,
FBSessionState state,
NSError *error) {
if (error) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error"
message:error.localizedDescription
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
}
else if (session.isOpen) {
FBRequest* friendsRequest = [FBRequest requestForMyFriends];
[friendsRequest startWithCompletionHandler: ^(FBRequestConnection *connection,
NSDictionary* result,
NSError *error) {
NSArray* friends = [result objectForKey:@"data"];
NSLog(@"Found: %i friends", friends.count);
for (NSDictionary<FBGraphUser>* friend in friends)
{
NSLog(@"I have a friend named %@ with id %@", friend.name, friend.id);
NSString *urlAsString =[NSString stringWithFormat:@"https://graph.facebook.com/%@/apprequests?message=hello&access_token=%@",friend.id,session.accessTokenData.accessToken];
urlAsString = [urlAsString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:urlAsString];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:urlRequest
queue:[[NSOperationQueue alloc] init]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
if (error==nil)
{
NSMutableData *webdata=[[NSMutableData alloc]initWithData:data];
NSString *myString = [[NSString alloc] initWithData:webdata encoding:NSUTF8StringEncoding];
NSLog(@"webdata myString == %@",myString);
}
}];
break;
}
}
}];
}
}];
如有任何需要,我们将不胜感激:
-(IBAction)inviteFriends:(id)sender
{
NSMutableString *text = [[NSMutableString alloc] init];
// we pick up the users from the selection, and create a string that we use to update the text view
// at the bottom of the display; note that self.selection is a property inherited from our base class
NSString * friendId = [finalEmailArr componentsJoinedByString:@","];
// NSLog(@"%@",friendId);
NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"Hi please add me as a friend.", @"message",
friendId, @"to",
nil];
[FBWebDialogs presentRequestsDialogModallyWithSession:nil
message:[NSString stringWithFormat:@"Hi, your friend has invited you to join chaga.com"]
title:nil
parameters:params
handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error) {
if (error) {
// Case A: Error launching the dialog or sending request.
// NSLog(@"Error sending request.");
} else {
if (result == FBWebDialogResultDialogNotCompleted) {
// Case B: User clicked the "x" icon
// NSLog(@"User canceled request.");
} else {
// NSLog(@"Request Sent.");
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:nil message:@"Successfully invited." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alert setDelegate:self];
[alert setTag:5];
[alert show];
}
}}];
if ([[NSUserDefaults standardUserDefaults] integerForKey:@"FVALCon"]==1) {
}else{
[self performSelector:@selector(remov_info) withObject:nil afterDelay:0.1];
}
}
- (void)viewDidLoad
{
//create sesion
//########## facebook ******
[self updateView];
// NSLog(@"appDelegate.session.isOpen %@",appDelegate.session.isOpen);
if (!delegateObj.session.isOpen) {
// create a fresh session object
delegateObj.session = [[FBSession alloc] init];
// if we don't have a cached token, a call to open here would cause UX for login to
// occur; we don't want that to happen unless the user clicks the login button, and so
// we check here to make sure we have a token before calling open
if (delegateObj.session.state == FBSessionStateCreatedTokenLoaded)
{
// even though we had a cached token, we need to login to make the session usable
[delegateObj.session openWithCompletionHandler:^(FBSession *session,
FBSessionState status,
NSError *error) {
[self updateView];
}];
}
}
}
-(void)Share_App:(id)sender{
[self unload_popView];
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[controller setInitialText:@"https://itunes.apple.com/"];
[self presentViewController:controller animated:YES completion:Nil];
}else{
// UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"Please set your facebook account to post" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
// [alert show];
Stus=1;
if (delegateObj.session.isOpen) {
[self updateView];
}else {
// NSLog(@"CLOSE SESION ");
if (delegateObj.session.state != FBSessionStateCreated) {
// Create a new, logged out session.
delegateObj.session = [[FBSession alloc] init];
}
[delegateObj.session openWithCompletionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
// and here we make sure to update our UX according to the new session state
[self updateView];
}];
}
}
}
//************************ load FB
- (void)updateView {
if (delegateObj.session.isOpen) {
NSLog(@"updateView delegateObj.session.isOpen");
// valid account UI is shown whenever the session is open
// NSLog(@"IF %@",appDelegate.session.accessTokenData.accessToken);
if (Stus==1) {
[delegateObj featch_Data:[NSString stringWithFormat:@"https://graph.facebook.com/me?access_token=%@",delegateObj.session.accessTokenData.accessToken]];
NSLog(@"updateView Send data");
}else if (Stus==2) {
[self featch_Data:[NSString stringWithFormat:@"https://graph.facebook.com/me?access_token=%@",delegateObj.session.accessTokenData.accessToken]];
}
Stus=0;
} else {
NSLog(@"updateView ELSE");
}
}
-(void)featch_Data:(NSString *)urlString{
Reachability *reachability = [Reachability reachabilityForInternetConnection];
NetworkStatus internetStatus = [reachability currentReachabilityStatus];
NSDictionary* jsonFB ;
if (internetStatus != NotReachable)
{
NSString *urlStr =urlString;
NSLog(@"login url...%@",urlStr);
NSURL *url = [NSURL URLWithString:urlStr];
NSError *error;
NSData* data = [NSData dataWithContentsOfURL:
url];
if (data==nil)
{
}
else
{
jsonFB = [NSJSONSerialization JSONObjectWithData:data //1
options:kNilOptions error:&error];
// NSLog(@"jsonFB fb..%@",jsonFB);
delegateObj.FbId =[jsonFB objectForKey:@"id"];
[self Share_appLink:delegateObj.FbId];
}
}
else{
UIAlertView *alert =[[UIAlertView alloc]initWithTitle:@"Error" message:@"No network coverage" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
//alert.tag = 1;
[alert show];
}
}
//*******************post on Wall
-(void)Share_appLink:(NSString *)UrserFBid{
NSArray *permissions = @[@"publish_stream"];//publish_actions//publish_stream//create_note
[FBSession openActiveSessionWithPublishPermissions:permissions defaultAudience:FBSessionDefaultAudienceEveryone allowLoginUI:YES completionHandler:^(FBSession *session,FBSessionState state,NSError *error)
{
NSArray *arr=[[total_record objectAtIndex:SmsTag] objectForKey:@"passenger"];
NSString *SeatString=[[NSString alloc]init];
for(int i=0;i<arr.count;i++)
{
SeatString = [SeatString stringByAppendingString:[NSString stringWithFormat:@"%@-%@\n",[[arr objectAtIndex:i] objectForKey:@"seat_number"],[[arr objectAtIndex:i] objectForKey:@"status"]]];
}
NSString *SmsString;
NSMutableDictionary* parameters1 = [NSMutableDictionary dictionaryWithObjectsAndKeys:
SeatString,@"message",
nil];
FBRequest *request1= [FBRequest requestWithGraphPath:[NSString stringWithFormat:@"/%@/feed",UrserFBid]
parameters:parameters1
HTTPMethod:@"POST"
];
[request1 startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if(error)
{
NSLog(@"result: %@, error: %@", result, error);
}else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert!" message:@"App link share successfully" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
// NSLog(@"Posted!");
}
}];
}];
}