facebook sharing in ios 9 using objective c - ios

I have facebook sharing concept in my app. I need to share the image url, description and when the user click on the image in FB, this will goto website. i used many methods like SLComposeViewController, FBSDKShareLinkContent, FBShareDialogParams.Sometimes these are all working fine. sometimes not working. Now FBShareDialogParams is deprecated in ios 9. Which code is working fine in ios9. Is any one know???

Try This Code:-
FBSDKShareLinkContent *sharecontent = [[FBSDKShareLinkContent alloc]init];
sharecontent.contentTitle = #"Hello ... My Dear Friends";
sharecontent.contentDescription =#"share this images";
sharecontent.imageURL = [NSURL URLWithString:#"http://www.picgifs.com/glitter-gifs/c/congratulations/picgifs-congratulations-178787.gif"];
sharecontent.contentURL = [NSURL URLWithString:#"https://itunes.apple.com"];
FBSDKShareDialog *dialog = [[FBSDKShareDialog alloc] init];
dialog.fromViewController = self;
dialog.delegate = self;
dialog.shareContent = sharecontent;
[dialog show];
// PRAGMA :- Delegate Method
- (void)sharer:(id<FBSDKSharing>)sharer didCompleteWithResults:(NSDictionary *)results
{
NSLog(#"Finished");
}
- (void)sharer:(id<FBSDKSharing>)sharer didFailWithError:(NSError *)error
{
}
- (void)sharerDidCancel:(id<FBSDKSharing>)sharer
{
}

Related

Facebook Share opens new instance of an App - iOS

I am using Facebook iOS SDK and FBSDKShareLinkContent for sharing url and image with text. Everything seems OK while sharing. But the problem is after sharing, the new instance of app is opened. The one sharing has been done is still open in background.
Is there something am I doing wrong? How to avoid it?
Here is the code I am using for sharing...
FBSDKShareLinkContent *content = [[FBSDKShareLinkContent alloc] init];
content.contentDescription = [[MusicPlayerHelper playerManager] getTextToShare];
content.contentURL = [NSURL URLWithString:[[MusicPlayerHelper playerManager] getURLToShare]];
content.imageURL=[NSURL URLWithString:[[MusicPlayerHelper playerManager] getImageURLToShare]];
FBSDKShareDialog *dialog = [[FBSDKShareDialog alloc] init];
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:#"fbauth2://"]]){
dialog.mode = FBSDKShareDialogModeNative;
}
else {
dialog.mode = FBSDKShareDialogModeAutomatic; //or FBSDKShareDialogModeAutomatic
}
dialog.shareContent = content;
dialog.delegate = _delegate;
dialog.fromViewController = _delegate;
[dialog show];

I can't share a content from my iOS app to the Facebook when Facebook app is installed

if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
{
imageUser.hidden=NO;
// [imageUser sendSubviewToBack:_horizontalScrollView];
SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
UIImage *goImage=[[UIImage alloc]init];
goImage=[self scaleImage:image123];
[controller setInitialText:#"Check out my pic from "];
[controller addURL:[NSURL URLWithString:#"www.awesomeapp.com"]];
[controller addImage:goImage];
imageUser.hidden=YES;
[self presentViewController:controller animated:YES completion:Nil];
}
I have gone through many stackoverflow questions and tried almost all. Some users reported it as bug, If so do we have any other way through which I can achieve my goal. It works like charm when Facebook app is not there but when the app is there this won't work :
[controller addURL:[NSURL URLWithString:#"www.awesomeapp.com"]];
Please help me achieve my goal. Thanks in advance.
I too headed up with is problem,some how i managed with FBSDKShareKit
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:#"fb://"]]) {
FBSDKShareLinkContent *content = [[FBSDKShareLinkContent alloc] init];
NSString * UrlString = [NSString stringWithFormat:#"https://www.google.co.in/"];
NSString * Subject = [NSString stringWithFormat:#"FBSDKShareKit is an alternative to this issue"];
content.contentURL = [NSURL URLWithString:UrlString];
content.contentTitle = #"FBSDKShareKit";
content.contentDescription = Subject;
content.imageURL = [NSURL URLWithString:#"https://i.vimeocdn.com/portrait/58832_300x300.jpg"];
FBSDKShareDialog *dialog = [[FBSDKShareDialog alloc] init];
dialog.fromViewController = self;
dialog.shareContent = content;
dialog.mode = FBSDKShareDialogModeFeedBrowser;
[dialog show];
}else{
//Go with your own method to share in FaceBook
}
In my pod file:
pod 'FBSDKShareKit', '~> 4.6.0'
dialog.mode = FBSDKShareDialogModeFeedBrowser it opens a fbShareDialog within your app which consist of all given datas.
Try some other alternatives for mode such as FBSDKShareDialogModeShareSheet,FBSDKShareDialogModeBrowser,FBSDKShareDialogModeWeb... it may also help you. I found FBSDKShareDialogModeFeedBrowser as satisfying. Try it,Let me know whether it works or not.

How to post or share in Facebook iOS SDK?

I'm trying to post a message or share a photo in my fb using FacebookSDK. I already follow the steps on how to install it. I'm done applying the login tutorial, but the problem now is how to post a message or share it into my FB using my app. There are a lot of tutorial like this one. I think this is already old for FacebookSDK. Sorry If I'm so noob for this. I know this not the right page to post this, but I don't have any idea now. If you have a tutorial on how to post or share. Please give me link. If ever, can you provide steps for this.
I already try this, but It doesn't post or share.
UPdate
this is my code
import "ShareController.h"
#import <FBSDKShareKit/FBSDKShareKit.h>
#import <FBSDKMessengerShareKit/FBSDKMessengerShareKit.h>
#implementation ShareController
- (void)viewDidLoad {
[super viewDidLoad];
FBSDKShareLinkContent *content = [[FBSDKShareLinkContent alloc] init];
content.contentURL = [NSURL URLWithString:#"https://developers.facebook.com"];
FBSDKShareButton *button = [[FBSDKShareButton alloc] init];
button.shareContent = content;
[self.view addSubview:button];
}
- (IBAction)shareButton:(id)sender {
UIImage * image = [UIImage imageNamed:#"sample.png"];
FBSDKSharePhoto *photo = [[FBSDKSharePhoto alloc] init];
photo.image = image;
photo.userGenerated = YES;
FBSDKSharePhotoContent *content = [[FBSDKSharePhotoContent alloc] init];
content.photos = #[photo];
FBSDKShareDialog *dialog = [[FBSDKShareDialog alloc] init];
dialog.fromViewController = self;
dialog.shareContent= content;
dialog.mode = FBSDKShareDialogModeShareSheet;
[dialog show];
}
here's the code for my login. I think this is simple code
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
FBSDKLoginButton *loginButton = [[FBSDKLoginButton alloc] init];
loginButton.center = self.view.center;
[self.view addSubview:loginButton];
// Do any additional setup after loading the view, typically from a nib.
}
You did not set permissions, to set the permissions:
FBSDKLoginButton *loginButton = [[FBSDKLoginButton alloc] init];
loginButton.center = self.view.center;
[loginButton logInWithPublishPermissions: #[#"publish_actions"] fromViewController:self handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
if (error) {
NSLog(#"Process error");
} else if (result.isCancelled) {
NSLog(#"Cancelled");
} else {
NSLog(#"Logged in");
}
}];
Also it is a good practice to check if user has granted the permission or not in your IBAction method:
- (IBAction)shareButton:(id)sender {
if ([[FBSDKAccessToken currentAccessToken] hasGranted:#"publish_actions"])
{
// code to share
}
}

Facebook deep linking/App link not working on iOS

I am trying to open my app from facebook when I tap on the post I made from my app.
I registered my app in developers.facebook.
I enabled the deep linking in my facebook app settings.
I have created the URL scheme for the app.
Note:My app is not published into app store. (Appple Appstore & Facebook app center)
I tried the following ways: However the post is made successful but facebook is not opening my app when I tap on that post.
One Way:
// Create an object
NSDictionary *properties = #{
#"og:type": #"books.book",
#"og:title": #"A Game of Thrones",
#"og:description": #"In the frozen wastes to the north of Winterfell, sinister and supernatural forces are mustering.",
#"books:isbn": #"0-553-57340-3",
};
FBSDKShareOpenGraphObject *object = [FBSDKShareOpenGraphObject objectWithProperties:properties];
// Create an action
FBSDKShareOpenGraphAction *action = [[FBSDKShareOpenGraphAction alloc] init];
action.actionType = #"books.reads";
[action setObject:object forKey:#"books:book"];
// Create the content
FBSDKShareOpenGraphContent *content = [[FBSDKShareOpenGraphContent alloc] init];
content.action = action;
content.previewPropertyName = #"books:book";
FBSDKShareDialog *dialog = [[FBSDKShareDialog alloc] init];
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:#"fbauth2://"]]){
dialog.mode = FBSDKShareDialogModeNative;
}
else {
dialog.mode = FBSDKShareDialogModeBrowser; //or FBSDKShareDialogModeAutomatic
}
dialog.shareContent = content;
dialog.delegate = self;
dialog.fromViewController = self;
[dialog show];
Another Way:
FBSDKShareLinkContent *content = [[FBSDKShareLinkContent alloc] init];
content.contentURL = [NSURL URLWithString:#"https://developers.facebook.com"];
content.contentTitle=#"TestSHare";
FBSDKShareDialog *dialog = [[FBSDKShareDialog alloc] init];
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:#"fbauth2://"]]){
dialog.mode = FBSDKShareDialogModeNative;
}
else {
dialog.mode = FBSDKShareDialogModeBrowser; //or FBSDKShareDialogModeAutomatic
}
dialog.shareContent = content;
dialog.delegate = self;
dialog.fromViewController = self;
[dialog show];
Facebook is opening the app center page. But not my app.
Does the deeplink works only when app is pubished ?
Any reasons ?

Sending image + url in UIActivityViewController in Facebook Messenger

using a simple UIActivityViewController
-(void)share{
NSString *textToShare = _mytext;
NSURL *url = [NSURL URLWithString:#"http://www.google.com"];
UIImage *imageToShare = _myimage;
NSArray *activityItems = #[textToShare, url, imageToShare];
UIActivityViewController *activityVC =
[[UIActivityViewController alloc] initWithActivityItems:activityItems
applicationActivities:nil];
[self presentViewController:activityVC animated:YES completion:nil];
}
I want to share a text, url and image where appropriate.
So if the user chooses mail, everything appears. Etc with the rest of the apps (pinterest, facebook, twitter)
On Facebook Messenger - If a url and an image is shared, the share screen crashes. Is this a known issue (can't send image with a url)?
* EDIT: Updated to the latest SDK
FBSDKShareLinkContent *content = [[FBSDKShareLinkContent alloc] init];
content.contentTitle = #"Your title";
content.contentDescription = #"Some description text maybe...";
content.contentURL = [NSURL URLWithString:#"http://yourlink.com"];
content.imageURL = [NSURL URLWithString:#"http://yourlink.com/theImage.png"];
[FBSDKMessageDialog showWithContent:content delegate:self];
// Delegate
- (void)sharer: (id<FBSDKSharing>)sharer didCompleteWithResults: (NSDictionary *)results
{
BOOL complete = [[results valueForKeyPath:#"didComplete"] boolValue];
NSString *completionGesture = [results valueForKeyPath:#"completionGesture"];
if (completionGesture && [completionGesture isEqualToString:#"cancel"]) {
// action was canceled by the user
}
if (complete) {
// the message/link/image was sent
}
}
- (void) sharer:(id<FBSDKSharing>)sharer didFailWithError:(NSError *)error
{
// handle error...
}
You can give it a try :)

Resources