UIActivityViewController won't post URL to Twitter and Facebook - ios

I'm using the UIActivityViewController to share some text, a URL, and an image to email, Twitter, Facebook, etc.
// Define parameters
NSString *text = #"example text";
NSURL *url = [NSURL URLWithString:#"www.google.com"];
UIImage *image = self.myImage;
// Create and display activity view controller
NSArray *activityItems = #[text, url, image];
UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
[self presentViewController:activityController animated:YES completion:nil];
The text and image appear in the email and get posted to Twitter/Facebook, but the URL doesn't show up anywhere. Any ideas what the problem could be?

NSURL *url = [NSURL URLWithString:#"www.google.com"];
Is not a valid URL. It does not have a scheme. Try:
NSURL *url = [NSURL URLWithString:#"http://www.google.com"];

Related

UIActivityViewController for sending an sms with image, url and a text

I am using UIActivityViewController for sending an sms with image, URL and a text. But SMS is showing full url including all parameters, instead of a clickable short link.
NSURL *urlImage = [NSURL URLWithString:[NSString stringWithFormat:#"%#",imageUrl]];
NSData *data = [NSData dataWithContentsOfURL:urlImage];
UIImage *img = [[UIImage alloc] initWithData:data];
NSURL *url = [NSURL URLWithString:shareURl];
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:#[#"I just won this on test. Check it out!", url, img] applicationActivities:nil];
activityVC.excludedActivityTypes = #[UIActivityTypeSaveToCameraRoll, UIActivityTypePostToFacebook, UIActivityTypePostToTwitter];
[self presentViewController:activityVC animated:YES completion:nil];
What I am getting is:
+
What I need is:
Is there any way we can achieve this?

Objective-C: programmatically share text on instagram direct message or in feed

I want to share some text on Instagram either on feed or direct message from my app in iOS. Can anybody let me know How to do that? I've tried the Instagram API Doc but using this Caption is not working the only Image is sharing in feed or story. But in Android, it is sharing text as a direct message using intent.
I've tried the following code:
#property(nonatomic, strong) UIDocumentInteractionController *docController;
-(void)InstagramBtnClick: (id)sender {
NSURL *myImgUrl = [NSURL URLWithString: #"https://www.materials-world.com/bricks/glen-gery/red/images/red_cliff_w12-DA.jpg"];
NSData * imageData = [[NSData alloc] initWithContentsOfURL: myImgUrl];
UIImage *imgShare = [[UIImage alloc] initWithData:imageData];
NSURL *instagramURL = [NSURL URLWithString:#"instagram://app"];
if([[UIApplication sharedApplication] canOpenURL:instagramURL]) //check for App is install or not
{
UIImage *imageToUse = imgShare;
NSString *documentDirectory=[NSHomeDirectory() stringByAppendingPathComponent:#"Documents"];
NSString *saveImagePath=[documentDirectory stringByAppendingPathComponent:#"Image.igo"];
NSData *imageData=UIImagePNGRepresentation(imageToUse);
[imageData writeToFile:saveImagePath atomically:YES];
NSURL *imageURL=[NSURL fileURLWithPath:saveImagePath];
self.docController=[[UIDocumentInteractionController alloc]init];
self.docController = [UIDocumentInteractionController interactionControllerWithURL:imageURL];
self.docController.delegate = self;
self.docController.annotation = [NSDictionary dictionaryWithObjectsAndKeys:#"Testing Caption Text", #"InstagramCaption", nil];
self.docController.UTI = #"com.instagram.exclusivegram";
UIViewController *vc = [self topMostController];
[self.docController presentOpenInMenuFromRect:CGRectMake(1, 1, 1, 1) inView:vc.view animated:YES];
}
else {
NSLog(#"Instagram App not Found");
}
}
Is there any other option so that the text can be added in the sharing content in iOS.

UIActivityViewController social sharing (Facebook) only shows first Object in NSArray

I am trying to share content via Facebook throug the UIActivityViewController and it is working but not showing the correct data.
I am using the following code:
-(void)shareContent{
Item *nItem = [_items getItem:_currentPage];
NSString * urlString = #"http://www.foo.com";
NSString *url1=nItem.img;
NSMutableString *url2 = [[NSMutableString alloc]init];
[url2 appendString:url1];
[url2 appendString:800];
NSURL *url = [NSURL URLWithString:url2];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
NSArray * shareItems = #[urlString, img];
UIActivityViewController * avc = [[UIActivityViewController alloc] initWithActivityItems:shareItems applicationActivities:nil];
[self presentViewController:avc animated:YES completion:nil];
}
But is only showing the urlString message, not the image.
Hope you can help with this.
UPDATE - EDIT
Is working fine in Notes, it shows both of them so I think I should something different for Facebook.

Share Audio file using UIActivityViewController

I am not able to see any item in UIActivityViewController. I want to share Audio file to Available Sharing options in iPhone device.
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIDocumentInteractionController_class/Reference/Reference.html
Review this doc, it will help you.
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
NSString *filePath = [docPath stringByAppendingPathComponent:#"sound.aac"];//Audio file
NSURL *fileUrl = [NSURL fileURLWithPath:filePath isDirectory:NO];
NSArray *activityItems = #[fileUrl];
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
[self presentViewController:activityVC animated:YES completion:nil];
Above answer worked for me after little bet struggle.I think there is some issue to access file path .here is modified and tested code.
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"sampleFile" ofType:#"mp3"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
NSArray *activityItems = #[soundFileURL];
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
[self presentViewController:activityVC animated:YES completion:nil];

UIActivityViewController sharing image via email has no extension

The image attached is just "Attachment-1", no extension. How do I specify one ?
NSData *compressedImage = UIImageJPEGRepresentation(self.resultImage, 0.8 );
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:#[ #"Check this out!", compressedImage ] applicationActivities:nil];
[self.navigationController presentViewController:activityViewController animated:YES completion:nil];
According to this answer you should be able to use this workaround to specify a filename
NSData *compressedImage = UIImageJPEGRepresentation(self.resultImage, 0.8 );
NSString *docsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *imagePath = [docsPath stringByAppendingPathComponent:#"image.jpg"];
NSURL *imageUrl = [NSURL fileURLWithPath:imagePath];
[compressedImage writeToURL:imageUrl atomically:YES]; // save the file
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:#[ #"Check this out!", imageUrl ] applicationActivities:nil];
The obvious drawback of this approach is that you will have to save the image on disk.
Credit to timeuser's suggestion, converting UIImage to NSData.
NSData * data = UIImageJPEGRepresentation(original,1.0);
UIActivityViewController * activity =[[UIActivityViewController alloc] initWithActivityItems:#[#"Your Text", data] applicationActivities:nil];
[self presentViewController:activity animated:true completion:^{ }];

Resources