如何在Facebook上分享iphone图书应用程序中的选定文本

时间:2014-01-02 01:38:02

标签: ios iphone facebook sdk share

我有iphone图书应用程序像所有其他文本一样点击并按住特定单词你会看到一个(复制,定义)菜单,我想添加另一项是(共享)当用户点击它共享行动表将弹出,用户可以分享(Facebook,Twitter,邮件或文本)上的特定单词。 请帮助我详细说明,任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

社会框架:

share Fb or Twitter

- (IBAction)socialSheet:(id)sender {



       // create Facebook controller
        SLComposeViewController *socialController = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];

        // add initial text
        [socialController setInitialText:@"Hello Facebook!"];

        // add an image for you optional 
        [socialController addImage:[UIImage imageNamed:@"picture.jpg"]];

        // add a URL for you optional 
        [socialController addURL:[NSURL URLWithString:@"http://wpguru.co.uk"]];

        // present controller
        [self presentViewController:socialController animated:YES completion:nil];
    }

Facebook Api

  -(void) postWithText: (NSString*) message_text
               ImageName: (NSString*) image_name
                     URL: (NSString*) url
                 Caption: (NSString*) caption
                    Name: (NSString*) name
          andDescription: (NSString*) description
    {

        NSMutableDictionary* params = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
                                       url, @"link",
                                       name, @"name",
                                       caption, @"caption",
                                       description, @"description",
                                       message, @"message_name",
                                       UIImagePNGRepresentation([UIImage imageNamed: image_name]), @"picture",
                                       nil];

        if ([FBSession.activeSession.permissions indexOfObject:@"publish_actions"] == NSNotFound)
        {
            // No permissions found in session, ask for it
            [FBSession.activeSession requestNewPublishPermissions: [NSArray arrayWithObject:@"publish_actions"]
                                                  defaultAudience: FBSessionDefaultAudienceFriends
                                                completionHandler: ^(FBSession *session, NSError *error)
            {
                 if (!error)
                 {
                     // If permissions granted and not already posting then publish the story
                     if (!m_postingInProgress)
                     {
                         [self postToWall: params];
                     }
                 }
             }];
        }
        else
        {
            // If permissions present and not already posting then publish the story
            if (!m_postingInProgress)
            {
                [self postToWall: params];
            }
        }
    }

    -(void) postToWall: (NSMutableDictionary*) params
    {
        m_postingInProgress = YES; //for not allowing multiple hits

        [FBRequestConnection startWithGraphPath:@"me/feed"
                                     parameters:params
                                     HTTPMethod:@"POST"
                              completionHandler:^(FBRequestConnection *connection,
                                                  id result,
                                                  NSError *error)
         {
             if (error)
             {
                 //showing an alert for failure
                 UIAlertView *alertView = [[UIAlertView alloc]
                                           initWithTitle:@"Post Failed"
                                           message:error.localizedDescription
                                           delegate:nil
                                           cancelButtonTitle:@"OK"
                                           otherButtonTitles:nil];
                 [alertView show];
             }
             m_postingInProgress = NO;
         }];
    }