How to share photos to Facebook in Objective-C? - ios

I want to share photos upon my custom button click, am able to share links and messages but when it comes to the sharing of photos am not able to do.
I am using latest Facebook SDK framework (4.15.1).
This is my code in inside the button click.
- (IBAction)btnsharecustom:(id)sender {
//Get aws s3 Image.
NSString* strS3ImageURL = #"https://TEST_IMAGE_URL/1473497380077.jpg";
//Convert to URL.
NSURL* url1 = [NSURL URLWithString:strS3ImageURL];
//Convert it into data.
NSData* data = [NSData dataWithContentsOfURL:url1];
//Convert it to image.
UIImage* image = [UIImage imageWithData:data];
FBSDKSharePhoto *photo = [[FBSDKSharePhoto alloc]init];
photo.image = image;
photo.caption = #"Test Caption";
photo.userGenerated = YES;
FBSDKSharePhotoContent *content = [[FBSDKSharePhotoContent alloc] init];
content.photos = #[photo];
}
I included this code in app delegate.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[[FBSDKApplicationDelegate sharedInstance] application:application
didFinishLaunchingWithOptions:launchOptions];
return YES;
}
//FaceBook URLs.
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
BOOL handled = [[FBSDKApplicationDelegate sharedInstance] application:application
openURL:url
sourceApplication:sourceApplication
annotation:annotation
];
// Add any custom logic here.
return handled;
}
Also included this code in info.plist
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>fb15771408xxx409xx</string>
</array>
</dict>
<key>FacebookAppID</key>
<string>15771408xxx409xx</string>
<key>FacebookDisplayName</key>
<string>Cedar iOS</string>
<key>LSApplicationQueriesSchemes</key>
<array>
<string>fbapi</string>
<string>fb-messenger-api</string>
<string>fbauth2</string>
<string>fbshareextension</string>
<string>fbapi20130214</string>
<string>fbapi20130410</string>
<string>fbapi20130702</string>
<string>fbapi20131010</string>
<string>fbapi20131219</string>
<string>fbapi20140410</string>
<string>fbapi20140116</string>
<string>fbapi20150313</string>
<string>fbapi20150629</string>
<string>fbapi20160328</string>
<string>fbauth</string>
<string>fb-messenger-api20140430</string>
</array>
When I click upon my custom button Facebook App or safari browser is not launching, so can anyone tell me what I am missing here?

You should present the dialog yourself after modelling the content.
Add this
[FBSDKShareDialog showFromViewController:self
withContent:content
delegate:nil];
to the end of your - (IBAction)btnsharecustom:(id)sender method.
Note: It will take time to download your image because it uses synchronous request.
//Convert it into data.
NSData* data = [NSData dataWithContentsOfURL:url1];
Read the documentation here https://developer.apple.com/reference/foundation/nsdata/1547245-datawithcontentsofurl
Do not use this synchronous method to request network-based URLs. For network-based URLs, this method can block the current thread for tens of seconds on a slow network, resulting in a poor user experience, and in iOS, may cause your app to be terminated.
A solution for this problem can be by downloading your image asynchronously before user tap button (if the image to upload is predictable). Usually you can do this in viewDidLoad method. Try this https://github.com/rs/SDWebImage library to make it easy for async request image.

try this code.
- (IBAction)btnsharecustom:(id)sender {
//Get aws s3 Image.
NSString* strS3ImageURL = #"https://placeholdit.imgix.net/~text?txtsize=15&txt=image1&w=120&h=120";
//Convert to URL.
NSURL* url1 = [NSURL URLWithString:strS3ImageURL];
//Convert it into data.
NSData* data = [NSData dataWithContentsOfURL:url1];
//Convert it to image.
UIImage* image = [UIImage imageWithData:data];
FBSDKSharePhoto *photo = [[FBSDKSharePhoto alloc]init];
photo.image = image;
photo.caption = #"Test Caption";
photo.userGenerated = YES;
FBSDKSharePhotoContent *content = [[FBSDKSharePhotoContent alloc] init];
content.photos = #[photo];
[FBSDKShareAPI shareWithContent:content delegate:self];
;
}
- (void)sharer:(id<FBSDKSharing>)sharer didCompleteWithResults:(NSDictionary *)results{
}

import
// Delegate FBSDKSharingDelegate
-(void)shareImageWithFacebook:(UIImage *)image {
if(![[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:#"fb://"]]) {
//Your device doesn't have Facebook app.
return;
}
FBSDKSharePhoto *photo = [FBSDKSharePhoto alloc];
photo.image = _imageSaved;
[photo setUserGenerated:YES];
photo.caption = AppName;
FBSDKSharePhotoContent *photoContent = [FBSDKSharePhotoContent alloc];
photoContent.photos = #[photo];
FBSDKShareDialog *dialog = [[FBSDKShareDialog alloc] init];
dialog.fromViewController = self;
dialog.delegate = self;
dialog.shareContent = photoContent;
[dialog show];
}
pragma mark- FBShareKit Delegate
(void)sharer:(id)sharer didCompleteWithResults:(NSDictionary *)results {
}
(void)sharer:(id)sharer didFailWithError:(NSError *)error {
}
(void)sharerDidCancel:(id)sharer {
}

Related

Convert URL into hyperlink

I am working on a app in which I am sharing event image with some url
When sharing the event image I am using this content :=
"Hey! I just saw "Venue Name" is having "Event Name" through the XXXX App! We
should check it out. https://app.com/site/redirectlink?event_id=%#"
Now can I make this "https://app.com/site/redirectlink?event_id=%#" link to hyperlink "here"?
I am sharing this content via "UIActivityViewController".
Actually this URL (https://app.com/site/redirectlink?event_id=%#) is used for deep linkling later on when user click this it will be redirect to particular event depend on the event_id.
Instead on the whole URL I want to use here(as hyperlink) only.
might be help you this way..
NSString *url=#"https://app.com/site/redirectlink?event_id=%#";
NSString * title =[NSString stringWithFormat:#"Hey! I just saw \"Venue Name\" is having \"Event Name\" through the XXXX App! We should check it out <html> <body> <a href = '%#'>here</a> </body> </html>",url];
NSArray* dataToShare = #[title];
UIActivityViewController* activityViewController =[[UIActivityViewController alloc] initWithActivityItems:dataToShare applicationActivities:nil];
activityViewController.excludedActivityTypes = #[UIActivityTypeAirDrop];
[self presentViewController:activityViewController animated:YES completion:^{}];
You may want deep linking for url : projectName://myapp?event_id=12345
Add info.plist
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLName</key>
<string>com.erimkurt.projectName</string>
<key>CFBundleURLSchemes</key>
<array>
<string>projectName</string>
</array>
</dict>
</array>
Add appdelegete.m
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
NSLog(#"Calling Application Bundle ID: %#", sourceApplication);
NSLog(#"URL scheme:%#", [url scheme]);
NSLog(#"URL query: %#", [url query]);
return YES;
}
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
if (!url) { return NO; }
UIAlertView *alertView;
alertView = [[UIAlertView alloc] initWithTitle:#"Launch by URL" message:#"This app was launched from a URL" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
NSString *urlString = [url absoluteString];
[[NSUserDefaults standardUserDefaults] setObject:urlString forKey:#"url"];
[[NSUserDefaults standardUserDefaults] synchronize];
return YES;
}
Add viewController.m
NSString *url = [[NSUserDefaults standardUserDefaults] objectForKey:#"url"];
NSLog(#"MYAPPURL: %#",url);

Download image from iCloud Drive iOS objective C

I have gone through many links, but nothing worked for me. All i need to do is fetch all iCloud Drive files and download images. I succeeded fetching all files using below code
- (void)presentDocumentPicker {
NSArray *documentTypes = #[#"public.content", #"public.text", #"public.source-code ", #"public.image", #"public.audiovisual-content", #"com.adobe.pdf", #"com.apple.keynote.key", #"com.microsoft.word.doc", #"com.microsoft.excel.xls", #"com.microsoft.powerpoint.ppt"];
UIDocumentPickerViewController *documentPickerViewController = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:documentTypes inMode:UIDocumentPickerModeOpen];
documentPickerViewController.delegate = self;
[self presentViewController:documentPickerViewController animated:YES completion:nil];
}
#pragma mark - UIDocumentPickerDelegate
- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentAtURL:(NSURL *)url {
self.documentURL = url;
QLPreviewController *previewController = [[QLPreviewController alloc] init];
previewController.delegate = self;
previewController.dataSource = self;
[self.navigationController pushViewController:previewController animated:YES];
}
now i don't know how to proceed further to download image.
i was able to download images using this
#pragma mark - UIDocumentPickerDelegate
- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentAtURL:(NSURL *)url {
self.documentURL = url;
NSNumber *isDownloadedValue = NULL;
BOOL success = [url getResourceValue:&isDownloadedValue forKey: NSURLUbiquitousItemIsDownloadingKey error:NULL];
if (success && ![isDownloadedValue boolValue]) {
[[NSFileManager defaultManager]startDownloadingUbiquitousItemAtURL:url error:nil];
// load image from Ubiquity Documents directory
NSData *imageData = [NSData dataWithContentsOfURL:url];
UIImage * image = [UIImage imageWithData:imageData];
}
QLPreviewController *previewController = [[QLPreviewController alloc] init];
previewController.delegate = self;
previewController.dataSource = self;
[self.navigationController pushViewController:previewController animated:YES];
}

facebook sharing in ios 9 using objective c

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
{
}

FBSDKShareDialog delegate not called

I use the latest Facebook SDK for iOS platform.I use FBSDKShareDialog to share image to Facebook,the code can share image to Facebook.But I want to get the share result delegate.
- (void)sharedImage:(UIImage *) sharedImage
fromViewController:(UIViewController *) fromViewController {
FBSDKSharePhoto *photo = [[FBSDKSharePhoto alloc] init];
photo.image = sharedImage;
photo.userGenerated = YES;
FBSDKSharePhotoContent *content = [[FBSDKSharePhotoContent alloc] init];
content.photos = #[photo];
FBSDKShareDialog *dialog = [[FBSDKShareDialog alloc] init];
dialog.fromViewController = fromViewController;
dialog.shareContent = content;
dialog.delegate = self;
dialog.mode = FBSDKShareDialogModeShareSheet;
[dialog show];
}
#pragma mark - FBSDKSharingDelegate
- (void)sharer:(id<FBSDKSharing>)sharer didCompleteWithResults:(NSDictionary *)results {
}
- (void)sharer:(id<FBSDKSharing>)sharer didFailWithError:(NSError *)error {
}
- (void)sharerDidCancel:(id<FBSDKSharing>)sharer {
}
Also I add suggest code in AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[FBSDKApplicationDelegate sharedInstance] application:application
didFinishLaunchingWithOptions:launchOptions];
return YES;
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
[FBSDKAppEvents activateApp];
}
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
[[FBSDKApplicationDelegate sharedInstance] application:application
openURL:url
sourceApplication:sourceApplication
annotation:annotation];
}
But the FBSDKSharingDelegate method has been never called.
You are setting fromViewController properly but that controller should be the delegate not the object.Below is code that is working and delegate is getting called
- (IBAction)facebookButtonClick:(UIButton *)sender {
// FacebookShare *facebookShare = [[FacebookShare alloc] init];
// [facebookShare sharedImage:[UIImage imageNamed:#"facebook_share"]
// fromViewController:self];
FBSDKSharePhoto *photo = [[FBSDKSharePhoto alloc] init];
photo.image = [UIImage imageNamed:#"facebook_share"];
photo.userGenerated = YES;
FBSDKSharePhotoContent *content = [[FBSDKSharePhotoContent alloc] init];
content.photos = #[photo];
FBSDKShareDialog *dialog = [[FBSDKShareDialog alloc] init];
dialog.fromViewController = self;
dialog.delegate = self;
dialog.shareContent = content;
dialog.mode = FBSDKShareDialogModeShareSheet;
[dialog show];
}
//FBSDKSharingDelegate
- (void)sharer:(id<FBSDKSharing>)sharer didCompleteWithResults:(NSDictionary *)results {
NSLog(#"completed");
}
- (void)sharer:(id<FBSDKSharing>)sharer didFailWithError:(NSError *)error {
NSLog(#"fail %#",error.description);
}
- (void)sharerDidCancel:(id<FBSDKSharing>)sharer {
NSLog(#"cancel");
}
In my case ,that 's because my view controller has dismiss , so that's impossible to call my delegate function. So please check weather your view controller was dismissed . Hope would help .

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