How to launch activity directly without showing share sheet in iOS 8? - ios

Currently my app has an option to share on Facebook and Twitter using SLComposeViewController.
SLComposeViewController *fbComposeViewController = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[fbComposeViewController setInitialText:text_short];
[fbComposeViewController addURL:url];
[self.navigationController presentViewController:fbComposeViewController
animated:YES
completion:^{
NSLog(#"fb activity completed");
}];
I can share to other sites like Gmail, Whatsapp, Message and Mail using
UIActivityViewController *controller = [[UIActivityViewController alloc] initWithActivityItems:#[text, url]
applicationActivities:nil];
controller.excludedActivityTypes = #[UIActivityTypePostToWeibo,
UIActivityTypePrint,
UIActivityTypeCopyToPasteboard,
UIActivityTypeAssignToContact,
UIActivityTypeSaveToCameraRoll,
UIActivityTypeAddToReadingList,
UIActivityTypePostToFlickr,
UIActivityTypePostToVimeo,
UIActivityTypePostToTencentWeibo,
UIActivityTypeAirDrop,
UIActivityTypePostToFacebook,
UIActivityTypePostToTwitter
];
[self presentViewController:controller animated:YES completion:nil];
However this launches a share sheet where user has to select an app he has to launch. Is there any way to directly launch the sharing dialog by specifying the activity name? For example, Whatsapp, Email and text invites in Uber.

for example if you want to share in whatsapp
// this is your share content message
NSString * msg = #"ApplNSString YOUR MSG";
NSString * urlWhats = [NSString stringWithFormat:#"whatsapp://send?text=%#",msg];
NSURL * whatsappURL = [NSURL URLWithString:[urlWhats stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
// this is identify if whatsapp is already install your device , if yes it open the whatsapp and share the content
if ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) {
[[UIApplication sharedApplication] openURL: whatsappURL];
} else {
// it shows the alert for no application found
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:#"WhatsApp not installed." message:#"Your device has no WhatsApp installed." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
additional reference

Related

mailto:recipients is not working with UIActivityViewController in iOS

I am using ActivityViewController for sending mail from App.
I was able to auto fill subject and body but not participants.
Example code :
NSURL *recipients = [NSURL URLWithString:#"mailto:foo#bar.com"]; //Not working
UIActivityViewController *activityViewController =
[[UIActivityViewController alloc] initWithActivityItems:#[recipients, #"bodyText"]
applicationActivities:nil];
[activityViewController setValue:mailSubject forKey:#"Subject"];
activityViewController.excludedActivityTypes = #[UIActivityTypeMessage, UIActivityTypePostToFacebook, UIActivityTypePostToFlickr, UIActivityTypePostToVimeo, UIActivityTypePostToTencentWeibo, UIActivityTypePostToWeibo];
[self.navigationController presentViewController:activityViewController
animated:YES
completion:^{
}];
MailTo participants is not working.
When i run the code, pop up is coming with selected mail app but to field is empty.

Sending user location to whatsapp and facebook

I am trying to send user location to facebook and whatsapp from my app by UIActivityViewController.
- (IBAction)shareUserLocation:(id)sender {
shareArray = [[NSArray alloc]initWithObjects:[NSURL URLWithString:[NSString stringWithFormat:#"http://maps.apple.com/maps?q=%f,%f",location.coordinate.latitude,location.coordinate.longitude]],nil];
UIActivityViewController *controller = [[UIActivityViewController alloc] initWithActivityItems:shareArray applicationActivities:nil];
controller.excludedActivityTypes = #[];
[self presentViewController:controller animated:YES completion:nil];
}
Its working but opening the location in apple maps.
Is there a way I can open the location in Google maps.
P.S- I have not tried whatsApp i have only tested for facebook.
If you want to open a Google Maps URL in your Google Maps iOS app, you can use comgooglemapsurl://maps.google.com/?q=%f,%f. If you want to show it in browser, you can do https://maps.google.com/?q=%f,%f.
Sample code:
-(void)testURL:(CLLocation*)location {
NSArray *shareArray;
if ([[UIApplication sharedApplication] canOpenURL:[[NSURL alloc] initWithString:#"comgooglemaps://"]]) {
shareArray = [[NSArray alloc]initWithObjects:[NSURL URLWithString:[NSString stringWithFormat:#"comgooglemapsurl://maps.google.com/?q=%f,%f",location.coordinate.latitude,location.coordinate.longitude]],nil];
} else {
shareArray = [[NSArray alloc]initWithObjects:[NSURL URLWithString:[NSString stringWithFormat:#"https://maps.google.com/?q=%f,%f",location.coordinate.latitude,location.coordinate.longitude]],nil];
}
UIActivityViewController *controller = [[UIActivityViewController alloc] initWithActivityItems:shareArray applicationActivities:nil];
controller.excludedActivityTypes = #[];
[self presentViewController:controller animated:YES completion:nil];
}

Is there way to get list of installed app on device, which is able to share and receive text data

Like for sharing text data with Whatsapp code is below.
i want to know all installed apps on device which is able to get text data, like below mention code.
NSString * msg = #"YOUR MSG"; NSString * urlWhats = [NSString stringWithFormat:#"whatsapp://send?text=%#",msg]; NSURL * whatsappURL = [NSURL URLWithString:[urlWhats stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; if ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) {
[[UIApplication sharedApplication] openURL: whatsappURL]; } else {
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:#"WhatsApp not installed." message:#"Your device has no WhatsApp installed." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show]; }
Sharing text,data with other social app than twitter/facebook. You can try the code below :
NSString *shareString = #"text...";
UIImage *shareImage = [UIImage imageNamed:#"image.png"];
NSURL *shareUrl = [NSURL URLWithString:#"http://www.test.com"];
NSArray *activityItems = [NSArray arrayWithObjects:shareString, shareImage, shareUrl, nil];
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
activityViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentViewController:activityViewController animated:YES completion:nil];
It will display activity view showing all other text sharing app.
Or You can also create your custom UIActivity also.
In your custom UIActivity subclass you have to simply override one method:
+ (UIActivityCategory)activityCategory
{
return UIActivityCategoryShare;
}
There is UIActivityViewController to list all apps, where you can share your text. And you can customize the list via "More" option there. Also apps having share extension will automatically lists there.

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.

Send e-mail via mail.app from my app on iOS

I have seen a nice feature in the iOS App Scanner Pro. This app allows to send scanned documents as email attachments via the original mail app from Apple but without leaving the Scanner Pro app. I ask me how did they do it? Is there a special API call?
implement MFMailComposeViewControllerDelegate like this:
#interface YourViewController<MFMailComposeViewControllerDelegate >
Then where you want to instantiate this email viewcontroller just do the following:
if([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController *mailController = [[MFMailComposeViewController alloc] init];
[mailController setMailComposeDelegate:self];
[mailController setSubject:#"Mail Subject!"];
[mailController setMessageBody:#"Here is your message body" isHTML:NO];
[mailController setToRecipients:[NSArray arrayWithObject:#"yourrecipent#domain.com"]];
NSData *imageData = UIImageJPEGRepresentation(imageToUpload, 1.0f);
if(imageData.length)
{
[mailController addAttachmentData:imageData mimeType:#"image/jpeg" fileName:#"Your_Photo.jpg"];
[self presentModalViewController:mailController animated:YES];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Invalid Image" message:#"The image couldn't be converted." delegate:nil cancelButtonTitle:nil otherButtonTitles:#"Okay", nil];
[alert show];
}
}
Last implement mailComposerViewController delegate method
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
[self dismissViewControllerAnimated:YES completion:nil];
// or you can check for the status first and implement different task if you wish
}
You can use UIActivityViewController, for example:
UIImage *image = [UIImage imageNamed:#"image_file_name"];
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:#[image] applicationActivities:nil];
[self presentViewController:activityViewController animated:YES completion:nil];
it gives user even more options, than just send email.
Yes, the so called UIActivityViewController. You use it like this:
NSArray *itemsToShare = #[[NSString stringWithFormat:#"This is a string that is sent via mail as well."], NSURLtoTheFileToSend];
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:itemsToShare applicationActivities:nil];
activityVC.excludedActivityTypes = #[UIActivityTypeAssignToContact]; // Here you can say what you dont want to give the opportunity to share.
activityVC.completionHandler = ^(NSString *activityType, BOOL completed) {
if (completed) {
UIAlertView *alert = [[UIAlertView alloc] init];
alert.title = #"Export successfull";
[alert show];
[alert performSelector:#selector(dismissWithClickedButtonIndex:animated:) withObject:nil afterDelay:1];
}
};
[self presentViewController:activityVC animated:YES completion:^{}];

Resources