Thursday 1 October 2015

How To Work Social Sharing Facebook And Twiter in Ios /Iphone

==>First To For Sharing :
==> This Code Ss Usefull For Sharing your Contant On Facebook Twiter:
.

=>This code write in SocialShare.h file.

=> Social Sharing Imporeted Some Framwork Like Social And Accounts :

#import <Foundation/Foundation.h>
#import <Social/Social.h>
#import <Accounts/Accounts.h>
@interface SocialShare : NSObject
{
   
}

+ (id) instantiateSociaShare;
- (void)shareWithTwitter:(NSMutableDictionary *)dictShareInfo withViewController:(UIViewController *)viewController;
- (void)shareWithFacebook:(NSMutableDictionary *)dictShareInfo withViewController:(UIViewController *)viewController;

@end
-------------------------------------------------------
==>This code will be write in SocialShare.m file.

#import "SocialShare.h"

@implementation SocialShare

==> Cretaed Instances Singlton Object Of Social Shares:

+ (id) instantiateSociaShare
{
    static SocialShare *singletonInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if(!singletonInstance) {
            singletonInstance = [[SocialShare alloc] init];
        }
    });
    return singletonInstance;
}

=> Now This Method Usiing For Share With Twitter:

- (void)shareWithTwitter:(NSMutableDictionary *)dictShareInfo withViewController:(UIViewController *)viewController
{
   
       
        if([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
        {
            dispatch_async(dispatch_queue_create("Twitter", NULL), ^{
               
                SLComposeViewController *aSLCVCTwitter = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
                SLComposeViewControllerCompletionHandler completionBlock = ^(SLComposeViewControllerResult result) {
                    if (result == SLComposeViewControllerResultDone)
                    {
                        [[[UIAlertView alloc]initWithTitle:@"Your app name." message:@"Shared on twitter successfully." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]show];
                    }
                    else
                    {
                        //[[[UIAlertView alloc]initWithTitle:kAppTitle message:@"" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]show];
                    }
                    [aSLCVCTwitter dismissViewControllerAnimated:YES completion:Nil];
                };
               
                aSLCVCTwitter.completionHandler = completionBlock;
                [aSLCVCTwitter setInitialText:[dictShareInfo objectForKey:@"textToShare"]];
                if([dictShareInfo objectForKey:@"imageFromURLToShare"])
                {
                    UIImage *aImgToShare = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[dictShareInfo objectForKey:@"imageFromURLToShare"]]]];
                    if(aImgToShare)
                    {
                        [aSLCVCTwitter addImage:aImgToShare];
                    }
                }
               
                [viewController presentViewController:aSLCVCTwitter animated:YES completion:^{
                   
                }];
            });
        }
        else
        {
          
            UIAlertView *aUIAlert = [[UIAlertView alloc]initWithTitle:@"App name" message:@"Your twitter account is not configured. Please configure it from your iphone's settings." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [aUIAlert show];
        }
   
}
=> Share With Facebook In Social Sharing:

- (void)shareWithFacebook:(NSMutableDictionary *)dictShareInfo withViewController:(UIViewController *)viewController
{

      
       
        if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
        {
            dispatch_async(dispatch_queue_create("Facebook", NULL), ^{
               
                SLComposeViewController *aSLCVCFacebook = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
                SLComposeViewControllerCompletionHandler completionBlock = ^(SLComposeViewControllerResult result) {
                    if (result == SLComposeViewControllerResultDone)
                    {
                        [[[UIAlertView alloc]initWithTitle:@"Your app name" message:@"Shared on facebook successfully." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]show];
                    }
                    else
                    {
                        // [[[UIAlertView alloc]initWithTitle:kAppTitle message:@"" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]show];
                    }
                    [aSLCVCFacebook dismissViewControllerAnimated:YES completion:Nil];
                };
               
                aSLCVCFacebook.completionHandler = completionBlock;
                [aSLCVCFacebook setInitialText:[dictShareInfo objectForKey:@"textToShare"]];
                if([dictShareInfo objectForKey:@"imageFromURLToShare"])
                {
                    UIImage *aImgToShare = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[dictShareInfo objectForKey:@"imageFromURLToShare"]]]];
                    if(aImgToShare)
                    {
                        [aSLCVCFacebook addImage:aImgToShare];
                    }
                }
               
                [viewController presentViewController:aSLCVCFacebook animated:YES completion:^{
                   
                }];
            });
        }
        else
        {
==> Relaeted Sharing Its Display UIAlertVIew:
           
            UIAlertView *aUIAlert = [[UIAlertView alloc]initWithTitle:@"Your app name" message:@"Your facebook account is not configured. Please configure it from your iphone's settings." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [aUIAlert show];
        }
 
}

@end
-----------------------
=?This code will be written in ViewController.h file.
=> Now Create One ViewController:


#import <UIKit/UIKit.h>
#import "SocialShare.h"
#import <Accounts/Accounts.h>
#import <Social/Social.h>
@interface ViewController : UIViewController
{
    __weak IBOutlet UILabel *lblName;
    ACAccountStore *accountStore;
    __weak IBOutlet UILabel *lblEmail;
    __weak IBOutlet UIImageView *imgVwProfilePic;
    __weak IBOutlet UIImageView *imgVwCoverPic;
}
- (IBAction)btnFacebookAction:(id)sender;
- (IBAction)btnGetTimelineAction:(id)sender;
- (IBAction)btnTwitterAction:(id)sender;

@end
----------------------------------------------

=>This code will be written in ViewController.m file.
 
#import "ViewController.h"
#import "SocialShare.h"
@interface ViewController ()

@end

@implementation ViewController

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

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)btnFacebookAction:(id)sender {
    NSMutableDictionary *aDict = [NSMutableDictionary dictionary];
    [aDict setObject:@"This is a test post!!!" forKey:@"textToShare"];
    [aDict setObject:@"http://www.indianic.com/newsite_v5/wp-content/themes/indianic/images/enterprise/indianic-footer-symbole.png" forKey:@"imageFromURLToShare"];
    [[SocialShare instantiateSociaShare] shareWithFacebook:aDict withViewController:self];
   
}
- (IBAction)btnGetTimelineAction:(id)sender {
 
    ACAccountType *aAccountType=[accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
   

    NSDictionary *dictOfId=[NSDictionary dictionaryWithObjectsAndKeys:@"354921344713153",ACFacebookAppIdKey,ACFacebookAudienceOnlyMe,ACFacebookAudienceKey,@[@"email"],ACFacebookPermissionsKey, nil];
    [accountStore requestAccessToAccountsWithType:aAccountType options:dictOfId completion:^(BOOL granted, NSError *error) {
       
        if (granted) {
            ACAccount *account=[[accountStore accountsWithAccountType:aAccountType] lastObject];
            ACAccountCredential *fbCredential = [account credential];
            NSString *accessToken = [fbCredential oauthToken];
            // profile details
            NSString *urlString = [NSString
                                   stringWithFormat:@"https://graph.facebook.com/me?fields=id,name,email,first_name,last_name,cover,picture{url}&access_token=%@",
                                   [accessToken stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
            urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            NSURL *url = [NSURL URLWithString:urlString];
            NSDictionary *aDictFbResponse=[NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfURL:url] options:NSJSONReadingMutableLeaves error:nil];
           
            NSString *strEmail=[aDictFbResponse objectForKey:@"email"];
//            NSString*  strFbId=[aDictFbResponse objectForKey:@"id"];
            lblEmail.text = strEmail;
            lblName.text = [aDictFbResponse objectForKey:@"name"];
            imgVwCoverPic.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[[aDictFbResponse objectForKey:@"cover"] objectForKey:@"source"]]]];
            imgVwProfilePic.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[[[aDictFbResponse objectForKey:@"picture"] objectForKey:@"data"] objectForKey:@"url"]]]];

            
           
        }
    }];
}

- (IBAction)btnTwitterAction:(id)sender {
    NSMutableDictionary *aDict = [NSMutableDictionary dictionary];
    [aDict setObject:@"This is a test post!!!" forKey:@"textToShare"];
    [aDict setObject:@"http://www.indianic.com/newsite_v5/wp-content/themes/indianic/images/enterprise/indianic-footer-symbole.png" forKey:@"imageFromURLToShare"];
    [[SocialShare instantiateSociaShare] shareWithTwitter:aDict withViewController:self];
}
@end