Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Saturday, November 26, 2016

How do I create a standard iOS Share button?

How do I create a standard iOS Share button?


The iOS Human Interface Guidelines say:

Use the system-provided Share button. Users are familiar with the meaning and behavior of this button, so it?s a good idea to use it when possible. The main exception to this is if your app does not contain a toolbar or navigation bar[, as] the Share button can only be used in a toolbar or navigation bar.

OK, but how do I ?use the system-provided Share button?? A search of the documentation turns up nothing useful.

I've gathered that I should use UIActivityViewController in my response to the button being tapped, but how would I create the standard Share button in the first place?

Answer by Peter Hosey for How do I create a standard iOS Share button?


The standard Share button is a UIBarButtonItem (so it can only go on a navigation bar or toolbar). You need to create a ?system item?; specifically, an ?action item?. The ?action? bar button item is the Share button.

Answer by SimpleApp for How do I create a standard iOS Share button?


Here is the code. I am assuming you are inside ViewController so self has navigationItem property.

UIBarButtonItem *shareButton = [[UIBarButtonItem alloc]  initWithBarButtonSystemItem:UIBarButtonSystemItemAction                       target:self                       action:@selector(shareAction:)];  self.navigationItem.rightBarButtonItem = shareButton;  

Answer by dzensik for How do I create a standard iOS Share button?


The system-provided Action button can also be fully created with an Interface Builder. To do so just drag & drop the UIToolbar to your view. Than drag & drop the UIBarButtonItem into the UIToolbar. As next step select in the view hierarchy the UIBarButtonItem, go to the Attribute Inspector and select as System Item the "Action". Done!

Additionally it is possible to connect the UIBarButtonItem with your class as IBOutlet or IBAction.

Please note: I use as a reference an Xcode 7.

Answer by danielrosero for How do I create a standard iOS Share button?


This goes in your viewDidLoad:

UIBarButtonItem *shareButton = [[UIBarButtonItem alloc]                                  initWithBarButtonSystemItem:UIBarButtonSystemItemAction                                  target:self                                  action:@selector(compartir:)];  self.navigationItem.rightBarButtonItem = shareButton;  

And define your selector method to be called as action (in my case named as "compartir"):

- (void) compartir:(id)sender{    //Si no      NSLog(@"shareButton pressed");      NSString *stringtoshare= @"This is a string to share";  UIImage *imagetoshare = img; //This is an image to share.    NSArray *activityItems = @[stringtoshare, imagetoshare];  UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];  activityVC.excludedActivityTypes = @[UIActivityTypeAssignToContact, UIActivityTypePrint, UIActivityTypePostToTwitter, UIActivityTypePostToWeibo];  [self presentViewController:activityVC animated:TRUE completion:nil];  }  

Answer by saneryee for How do I create a standard iOS Share button?


Main.storyboard -> Bar Button Item -> Inspector -> System Item chose "Action"enter image description here

Answer by jarora for How do I create a standard iOS Share button?


Here is the code for Swift 3.

func addShareBarButtonItem() {        let shareButton = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(PPMyQRCodeViewController.shareButtonPressed))        self.navigationItem.rightBarButtonItem = shareButton  }    func shareButtonPressed() {      //Do something now!  }  


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.