Is there anyone who know how to receive NSString sent over AirDrop on my app? So far, my app can send NSString over AirDrop. But I don't know how to open the NSString I send over Airdrop by using my app.
#interface ComposeViewController ()
#end
#implementation ComposeViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
-(BOOL) textFieldShouldReturn: (UITextField *) textField
{
[textField resignFirstResponder];
return YES;
}
- (IBAction)sendButton:(id)sender {
NSString* message= self.inputTextField.text;
NSLog(#"you type: %#", self.inputTextField.text);
// enabling airdrop
UIActivityViewController *controller = [[UIActivityViewController alloc] initWithActivityItems:#[message] applicationActivities:nil];
// Exclude all activities except AirDrop.
NSArray *excludedActivities = #[UIActivityTypePostToTwitter, UIActivityTypePostToFacebook,
UIActivityTypePostToWeibo,
UIActivityTypeMessage, UIActivityTypeMail,
UIActivityTypePrint, UIActivityTypeCopyToPasteboard,
UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll,
UIActivityTypeAddToReadingList, UIActivityTypePostToFlickr,
UIActivityTypePostToVimeo, UIActivityTypePostToTencentWeibo];
controller.excludedActivityTypes = excludedActivities;
// Present the controller
[self presentViewController:controller animated:YES completion:nil];
//[self.readTextView setText:self.inputTextField.text];
}
Look into using UIActivityItemSource to send the NSString as NSData + a custom UTI for your app. Basically define a new file format, that could be as simple as NSString encoded as UTF8 data, and then on the receiving side in your app code you convert this back into an NSString. The AirDrop sample code will likely be helpful.
Related
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.
Trying to share application on social median, and email, and message.
Since Email and message is working fine for me.
But Facebook,and twitter is not all showing to UIActivityViewController
Below is code which i try.
NSArray *excludeActivities = #[UIActivityTypeAirDrop,
UIActivityTypePostToFacebook,
UIActivityTypePostToTwitter];
UIActivityViewController *activityViewController =
[[UIActivityViewController alloc] initWithActivityItems:#[string, URL]
applicationActivities:nil];
activityViewController.excludedActivityTypes = excludeActivities;
[self presentViewController:activityViewController
animated:YES
completion:^{
}];
- (id)activityViewController:(UIActivityViewController *)activityViewController
itemForActivityType:(NSString *)activityType
{
if ([activityType isEqualToString:UIActivityTypePostToFacebook]) {
return NSLocalizedString(#"calling facebook", );
} else if ([activityType isEqualToString:UIActivityTypePostToTwitter]) {
return NSLocalizedString(#"calling twitter", );
} else {
return nil;
}
}
#All
Please let me know what i need to do.
For that just create one class for that like this.
SharingActivityProvider.h
#import <Foundation/Foundation.h>
#interface SharingActivityProvider : UIActivityItemProvider
#end
**SharingActivityProvider.m**
#import "SharingActivityProvider.h"
#implementation SharingActivityProvider {
}
- (id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(NSString *)activityType {
// Create the default sharing string
NSString *shareString = #"";
// customize the sharing string for facebook, twitter, weibo, and google+
if ([activityType isEqualToString:UIActivityTypePostToFacebook]) {
shareString = [NSString stringWithFormat:#"Attention Facebook:%#", shareString];
} else if ([activityType isEqualToString:UIActivityTypePostToTwitter]) {
shareString = [NSString stringWithFormat:#"Attention Twitter:%#", shareString];
} else if ([activityType isEqualToString:UIActivityTypePostToWeibo]) {
shareString = [NSString stringWithFormat:#"Attention Weibo:%#", shareString];
}
return shareString;
}
- (id)activityViewControllerPlaceholderItem:(UIActivityViewController *)activityViewController {
return #"";
}
#end
Then after just use below code, suppose button action.
-(IBAction)shareAppTapped:(id)sender{
SharingActivityProvider *sharingActivityProvider = [[SharingActivityProvider alloc] init];
NSArray *activityProviders = #[sharingActivityProvider,#"what ever text"];
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:activityProviders applicationActivities:nil];
// tell the activity view controller which activities should NOT appear
activityViewController.excludedActivityTypes = #[UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll];
activityViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentViewController:activityViewController animated:YES completion:nil];
// [self.btnShareApp setBackgroundColor:OnebackgroundColorTable];
}
One mistake here is exclude activities also have facebook and twitter. Hence it will not show by any means.
Note excludedActivityTypes should be which you does not need in controller.
For example it should be:
NSArray *excludeActivities = #[UIActivityTypePostToWeibo,
UIActivityTypeMessage,
UIActivityTypeAssignToContact,
UIActivityTypeSaveToCameraRoll,
UIActivityTypeAddToReadingList,
UIActivityTypePostToFlickr,
UIActivityTypePostToVimeo,
UIActivityTypePostToTencentWeibo];
I want to show the Alert message When no mail is configured on the device.But when i click Mail from document interaction it just simply dismiss the controller none of the two following Delegate method will call.Please refer Image for better understanding.
Please help. Thanks in Advance
- (void)openAppList:(FileInfo *)fileinfo {
NSURL *fileURL = [NSURL fileURLWithPath:fileinfo.fullName];
UIDocumentInteractionController *interactionController = [UIDocumentInteractionController interactionControllerWithURL:fileURL];
[interactionController retain];
interactionController.delegate = self;
BOOL present = [interactionController presentOptionsMenuFromRect:CGRectZero inView:self.tabBarController.view animated:YES];
if (!present) {
[MainteOrErrorDialog initWithErrorCode:kAlertNotOpenInFileId filename:fileInfo.filename target:nil action:nil];
} else {
[interactionController retain];
}
}
#pragma UIDocumentInteractionDelegate
- (void)documentInteractionController:(UIDocumentInteractionController *)controller
willBeginSendingToApplication:(NSString *)application
{
}
- (void)documentInteractionController:(UIDocumentInteractionController *)controller
didEndSendingToApplication:(NSString *)application
{
}
Unfortunately, you can't really interact with the Mail delegates through UIDocumentInteractionController. UIDocumentInteractionController is extremely limited to what you can do with properties and attributes of all apps it supports, you can't even set a mails message body to a custom message. In fact, documentInteractionController:canPerformAction: is deprecated in iOS 6.0 because Apple was moving towards UIActivityViewController. They're own deprecated statement is :
Apps should use UIActivityViewController for actions
So alternatively, with UIActivityViewController you can.
A work around for this, implement UIActivityViewController, they both support the same apps and check if a user can send emails, if not, exclude Mail from the menu :
if ([MFMailComposeViewController canSendMail]) {
NSArray *shareItems;
shareItems = #[[Config emailMessage], snapshot]; //emailMessage is an NSString containing the body of the mail or mms message located in a different VC
UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:shareItems applicationActivities:nil];
[activityController setCompletionWithItemsHandler:(UIActivityViewControllerCompletionWithItemsHandler)^(NSString *string, BOOL completed) {
}
[self presentViewController:activityController animated:YES completion:nil];
} else {
NSArray *shareItems;
shareItems = #[[Config emailMessage], snapshot]; //emailMessage is an NSString containing the body of the mail or mms message located in a different VC
UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:shareItems applicationActivities:nil];
activityController.excludedActivityTypes = #[UIActivityTypeMail];
[activityController setCompletionWithItemsHandler:(UIActivityViewControllerCompletionWithItemsHandler)^(NSString *string, BOOL completed) {
}
[self presentViewController:activityController animated:YES completion:nil];
}
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.
I have seen this format (Image shown below) of share option in most of the iOS applications that support iOS 7.
Is there a default code/framework available to implement this share option as it is shown in the image below?
What you are looking for is the UIActivityViewController.
Since you asked a general question I can't do more than give you a link to the documentation
In addition to the accepted answer, a small piece of example code
- (void)shareText:(NSString *)text andImage:(UIImage *)image andUrl:(NSURL *)url
{
NSMutableArray *sharingItems = [NSMutableArray new];
if (text) {
[sharingItems addObject:text];
}
if (image) {
[sharingItems addObject:image];
}
if (url) {
[sharingItems addObject:url];
}
UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:sharingItems applicationActivities:nil];
[self presentViewController:activityController animated:YES completion:nil];
}
Call shareText, leave the things that you don't want to share at nil.
[self shareText:#"Hello world" andImage:nil andUrl:nil];
The Controller in the image you posted is the UIActivitiyViewController this is a link to the class documentation
some good example code:
How to display the default iOS 6 share action sheet with available share options?
I know this question is particular to iOS 7, and the code example specifies iOS 6, but AFAICT they are very similar one might find the example code as helpful as I did.
UIActivityViewController is what you are looking for.
You can specify either the items or the applications
UIActivityViewController *actCont = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
Just use following code for Default Sharing. You can able to add more items into shareItems array as per your requirement.
NSMutableArray *shareItems = [[NSMutableArray alloc] initWithObjects:
#"Hello",
[UIImage imageNamed:#"your_image.png"],
#"http://google.com/", nil];
[self shareItemToOtherApp:shareItems];
Following method is for default sharing Text or Image into other Apps:-
-(void)shareItemToOtherApp:(NSMutableArray *)shareItems{
UIActivityViewController *shareController = [[UIActivityViewController alloc]
initWithActivityItems: shareItems applicationActivities :nil];
[shareController setValue:#"Sharing" forKey:#"subject"];
shareController.excludedActivityTypes = #[UIActivityTypePostToWeibo, UIActivityTypeAssignToContact, UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeSaveToCameraRoll];
shareController.completionHandler = ^(NSString *activityType, BOOL completed)
{
//NSLog(#" activityType: %#", activityType);
//NSLog(#" completed: %i", completed);
};
[self presentViewController: shareController animated: YES completion: nil];
}
If you want to make Custom Sharing sheet then use following code. For this, you have to import <Social/Social.h> framework.
-(void)shareOnFacebook:(id)sender {
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
{
SLComposeViewController *faceSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
// NSLog(#"%#", messageField.text);//This returns the appropriate string
[faceSheet setInitialText:#"Hellooooooo"];
//The facebook VC appears, but initial text is not set to messageField.text
[self presentViewController:faceSheet animated:YES completion:nil];
}
else
{
NSLog(#"Please first install Application and login in Facebook");
}
}
-(void)shareOnTwitter:(id)sender {
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
SLComposeViewController *tweetSheet = [SLComposeViewController
composeViewControllerForServiceType:SLServiceTypeTwitter];
[tweetSheet setInitialText:#"Hello"];
[self presentViewController:tweetSheet animated:YES completion:nil];
}
else{
NSLog(#"Please first install Application and login in Twitter");
}
}
Hope, this is what you're looking for. Any concern get back to me. :)