iOS how to implement the official share function - ios

Include facebook, twitter, message, email and so on ,like this:

In your ViewController.m :
NSString *message = #"The Flyer http://flyerdream.tumblr.com";
UIImage *image = [UIImage imageNamed:#"flyer"];
NSArray *arrayOfActivityItems = [NSArray arrayWithObjects:message, image, nil];
UIActivityViewController *activityVC = [[UIActivityViewController alloc]
initWithActivityItems:arrayOfActivityItems applicationActivities:nil];
[self.navigationController presentViewController:activityVC animated:YES completion:nil];

Related

Sharing link in twitter with a preview iOS?

I had an application in which i am sharing a link to twitter using Uiactivityviewcontroller.its just showing the text and link in twitter in my TL.i want it to be shown like as in Facebook with an image extracted from the link.I am doing like this
NSURL *shareUrl = [NSURL URLWithString:#“http://dhdhdh”];
NSString* someText = #“fdfdfd”;
UISimpleTextPrintFormatter *printData = [[UISimpleTextPrintFormatter alloc]
initWithText:#"http://dhdhdh"];
NSArray* dataToShare = #[someText,shareUrl,printData]; // ...or whatever pieces of data you want to share.
NSArray *appActivities = [NSArray arrayWithObjects:[[UrlActivity alloc] init], nil];
UIActivityViewController* activityViewController =
[[UIActivityViewController alloc] initWithActivityItems:dataToShare
applicationActivities:appActivities];
activityViewController.excludedActivityTypes=#[UIActivityTypeAddToReadingList,UIActivityTypeAirDrop,UIActivityTypePrint,UIActivityTypeAssignToContact];
// activityViewController.
[self.navigationController presentViewController:activityViewController animated:YES completion:nil];
Can anybody help me on this?
Try this
go to Build settings in targer-> link Binary with Library - > add "Social" framework
Import #import <Social/Social.h>
NSURL *url = [NSURL URLWithString:#"https://upload.wikimedia.org/wikipedia/en/3/38/Avatarjakeneytiri.jpg"];
UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) {
SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
[controller setInitialText:#"myShare"];
[controller addImage:img];
[self presentViewController:controller animated:YES completion:nil];
}
else {
//wont work in simulator
}

Quirk in UIActivityViewController integration

I integrated a sharing option into my app and everything seems fine, basically I excluded every type of UIActivity except for Message, Twitter, and Facebook, and when I tested to see if it functions, what I see in the text box is
"I got a new highscore!
com.apple.UIKit.activity.Message
com.apple.UIKit.activity.PostToFacebook
com.apple.UiKit.activity.PostToTwitter".
and the text appears in each of activities.
Is there anyway to remove the text, or is it supposed to be that way?
Here is a method in my MainViewController which presents the UIActivityViewController.
-(void)sharedView {
APActivityProvider *APActivityPro = [[APActivityProvider alloc] init];
NSArray *activites = #[APActivityPro,UIActivityTypeMessage, UIActivityTypePostToFacebook, UIActivityTypePostToTwitter];
activityController = [[UIActivityViewController alloc] initWithActivityItems:activites applicationActivities:nil];
activityController.excludedActivityTypes = #[UIActivityTypePostToWeibo,
UIActivityTypeMail,
UIActivityTypePrint,
UIActivityTypeCopyToPasteboard,
UIActivityTypeAssignToContact,
UIActivityTypeSaveToCameraRoll,
UIActivityTypeAddToReadingList,
UIActivityTypePostToFlickr,
UIActivityTypePostToVimeo,
UIActivityTypePostToTencentWeibo,
UIActivityTypeAirDrop];
[self presentViewController:activityController animated:YES completion:nil];
[activityController setCompletionHandler:^(NSString *activityType, BOOL completed)
{
if (completed)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#""
message:NSLocalizedString(#"Post Sent!",#"the confirmation of the sent message/post")
delegate:nil
cancelButtonTitle:NSLocalizedString(#"OK",#"done")
otherButtonTitles:nil];
[alert show];
}
}
];
}
And also my code in my APActivityProvider.m file.
-(id) activityViewController:(UIActivityViewController*)activityViewController
itemForActivityType:(NSString *)activityType
{
if ([activityType isEqualToString:UIActivityTypeMessage])
return NSLocalizedString(#"I got a new highscore!",#"Comment for message");
if ([activityType isEqualToString:UIActivityTypePostToFacebook])
return NSLocalizedString(#"I got a new highscore!",#"Comment for Facebook");
if ([activityType isEqualToString:UIActivityTypePostToTwitter])
return NSLocalizedString(#"I got a new highscore!",#"Comment for twitter");
return nil;
}
- (id) activityViewControllerPlaceholderItem:(UIActivityViewController *)
activityViewController
{
return #"";
}
It would appear you are using the API incorrectly. Specifically:
NSArray *activites = #[APActivityPro,UIActivityTypeMessage, UIActivityTypePostToFacebook, UIActivityTypePostToTwitter];
activityController = [[UIActivityViewController alloc] initWithActivityItems:activites applicationActivities:nil];
Activity Items are your content (Strings, Images, etc) while applicationItems are the custom activity options you add to the default system ones (something you code such as an open in app store activity). You do not need to tell the UIActivityViewController what system activities you want. All are enabled by default.
For an example of how to use this API, I have included the following:
NSNumber *AppStoreID = [NSNumber numberWithInt:550008982];
NSArray *activityItems =
#[ AppStoreID ];
NSArray *applicationActivities =
#[ [ATFAppStoreActivity alloc] ];
UIActivityViewController *activityViewController =
[[UIActivityViewController alloc] initWithActivityItems:activityItems
applicationActivities:applicationActivities];
[self presentViewController:activityViewController animated:YES completion:nil];
Here, I have my subclassed UIActivity (ATFAppStoreActivity) that takes an NSNumber to display the App Store page for that app.

UIActivityViewController - Facebook/Twitter not showing in iOS Simulator

I've set up my UIActivityViewController and mail shows up. But I can't figure out how to get Facebook/Twitter to work? I have read somewhere that since obviously the Xcode doesn't have Facebook/Twitter signed in, iOS7 doesn't show the app?
My code:
NSArray *activityItems = #[#"Hello"];
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
activityViewController.excludedActivityTypes = #[UIActivityTypePostToWeibo, UIActivityTypeAssignToContact ];
[self presentViewController:activityViewController animated:YES completion:NULL];

UIActivityViewController won't share xlsx file

I'm trying to share and XLSX via email using UIActivityViewController but when it just opens an empty email with no attachments.
When I try to add PDF and XLSX files - it opens an email with the PDF file attached - but no XLSX file...
Here's the code:
NSMutableArray *itemsToShare = [[NSMutableArray alloc] init];
[itemsToShare addObject:pdfNSDATA];
[itemsToShare addObject:xlsxNSDATA];
UIActivityViewController *controller = [[UIActivityViewController alloc] initWithActivityItems:itemsToShare applicationActivities:nil];
[self presentViewController:controller animated:YES completion:nil];
Try inserting the items as file URLs to your array like so:
NSURL *urlToPDF = [NSURL fileURLWithPath:pathToPDF]; //pathToPDF is an nsstring with a path to your file
NSURL *urlToXSLX = [NSURL fileURLWithPath:pathToXSLX]; //So is
NSMutableArray *itemsToShare = [[NSMutableArray alloc] init];
[itemsToShare addObject:urlToPDF];
[itemsToShare addObject:urlToXSLX];
UIActivityViewController *controller = [[UIActivityViewController alloc] initWithActivityItems:itemsToShare applicationActivities:nil];
[self presentViewController:controller animated:YES completion:nil];
Of course you'll need to save the NSData to the file system in order to be able to do this. You can use NSData's writeToURL:atomically to achieve this.

UIActivityViewController Messenger not working on iOS Simulator

I'm getting the following result when I try to initialize the Messenger component of UIActivityViewController:
I'm using this code to instantiate my UIActivityViewController:
NSString *textToShare = #"Text that will be shared";
NSArray *itemsToShare = [[NSArray alloc] initWithObjects:textToShare, nil];
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:itemsToShare applicationActivities:nil];
activityVC.excludedActivityTypes = [[NSArray alloc] initWithObjects: UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll, UIActivityTypePostToWeibo,UIActivityTypePostToFacebook,UIActivityTypePostToTwitter, nil];
[activityVC setValue:#"My Subject Text" forKey:#"subject"];
[self presentViewController:activityVC animated:YES completion:nil];
I then get the following error in my logs when I attempt to send with Message
Launch Services: Unable to find app identifier com.apple.MobileSMS
2013-07-31 01:27:15.180 Coolist[54111:c07] Application tried to push a nil view controller on target <MFMessageComposeViewController: 0xa38db30>.
Is this just a simulator problem?

Resources