Search code examples
iosobjective-cfacebookfacebook-graph-apifacebook-ios-sdk

How can I share the contents of my page in iPhone app on facebook?


I'm providing a facebook share option on my iPhone app screen and need to share some images and URL on the user's facebook profile . I know there are several third party frameworks like ShareKit and other features like facebook sdk for iOS, but which is the best way I could so the same and i need to get the share button with the number of shares until now as shown in the image below

enter image description here

and on clicking should redirect to authentication. And should be compatible with iOS5 and above, hope my question is clear.

Thanks in advance


Solution

  • Solved the question myself through research

    I used Facebook Graph API since my application needs support from iOS4+, would have been simple if it was for iOS6 as iOS6 has Facebook integration and needs just a few lines of code to implement it. Imported Graph APi files and added the following code:

    @synthesize facebook;
    
    //login with facebook
    - (IBAction) facebookButtonPressed {
        if (!facebook || ![facebook isSessionValid]) {
            self.facebook = [[[Facebook alloc] init] autorelease];
            NSArray *perms = [NSArray arrayWithObjects: @"read_stream", @"create_note", nil];
            [facebook authorize:FACEBOOK_API_KEY permissions:perms delegate:self];
        }
        else {
            [self fbDidLogin];
        }
    }
    
    //upload image once you're logged in
    -(void) fbDidLogin {
        [self fbUploadImage];
    }
    
    - (void) fbUploadImage
    {
        NSMutableDictionary * params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                        resultImage, @"picture",
                                        nil];
        [facebook requestWithMethodName: @"photos.upload" 
                              andParams: params
                          andHttpMethod: @"POST" 
                            andDelegate: self]; 
    
        self.currentAlertView = [[[UIAlertView alloc]
                                 initWithTitle:NSLocalizedString(@"Facebook", @"")  
                                 message:NSLocalizedString(@"Uploading to Facebook.", @"")  
                                 delegate:self 
                                 cancelButtonTitle:nil
                                 otherButtonTitles: nil] autorelease];
    
        [self.currentAlertView show];
    }
    
    //facebook error
    - (void)request:(FBRequest*)request didFailWithError:(NSError*)error
    {
    
        [self.currentAlertView dismissWithClickedButtonIndex:0 animated:YES];
        self.currentAlertView = nil;
    
        UIAlertView *myAlert = [[UIAlertView alloc]
                                initWithTitle:NSLocalizedString(@"Error", @"")  
                                message:NSLocalizedString(@"Facebook error message", @"")
                                delegate:self 
                                cancelButtonTitle:nil
                                otherButtonTitles:@"OK", nil];
        [myAlert show];
        [myAlert release];
    }
    
    //facebook success
    - (void)request:(FBRequest*)request didLoad:(id)result
    {
        [self.currentAlertView dismissWithClickedButtonIndex:0 animated:YES];
        self.currentAlertView = nil;
    
        UIAlertView *myAlert = [[UIAlertView alloc]
                                initWithTitle:NSLocalizedString(@"Facebook", @"") 
                                message:NSLocalizedString(@"Uploaded to Facebook message", @"")
                                delegate:self 
                                cancelButtonTitle:nil
                                otherButtonTitles:@"OK", nil];
        [myAlert show];
        [myAlert release];
    }
    

    And to get the share count of a particular URL in facebook, use

    http://graph.facebook.com/?id=url
    

    But this returns the count of all likes,shares and posts altogether, so better find some alternatives to do so.

    Hope this helps Thanks