0

My rss app is almost complete but I need to have a "share" button on the top right corner of the web view for the articles. I have the code setup so far to show the "action" button right next to the title of the article but since there is no real code implemented it doesn't do anything :

enter image description here

I implemented that by basing it on code that was used for the table view cells where there is a "refresh" option implemented and I just changed the icon. I used a free open source project to put this app together and i'm having a little bit of trouble figuring out how to make that button show the share sheet that pulls up from the bottom of the app, like this:

enter image description here (image from google)

I tried to implement a button dirtectly into the nav bar but I can't since the web view covers the whole screen:

enter image description here

So, since I couldn't put a button directly into the controller I had to encode it in like so:

In my RSSDetail.m class inside the " -(void)viewDidLoad { " :

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

and I also have this inside the same class file :

-(void) shareButton { 

  self.webView.userInteractionEnabled = YES; 
  self.webView.aplha = 0.3;  

  } 

So as you can see, this little bit of code in that class puts the icon where I want it. But my question is, is there code i can implement into my " -(void) shareButton " method that will implement the sharing functionality?

Also, I need for the button to actually say "Share" as opposed to the icon, is there anyway I can change the code for the "Action" rightBarButton method to allow me to input the word instead of an icon?

Thanks in advance

Christophorus
  • 154
  • 12

1 Answers1

0

As I understand you want to implement iOS native sharing functionality. Here is an example of implementation:

- (void)shareButtonPressed {
    // 1
    // If you want to use UIImage, make sure you have image with that name, otherwise it will crash. 
    NSArray *activityItems = @[@"Message to share", [UIImage imageNamed:@"imageToShare"]];

    // 2
    UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];

    // 3
    activityViewController.excludedActivityTypes = @[UIActivityTypeCopyToPasteboard, UIActivityTypeAirDrop, UIActivityTypeAssignToContact, UIActivityTypeAddToReadingList, UIActivityTypePrint, UIActivityTypeSaveToCameraRoll, UIActivityTypeMessage, UIActivityTypeMail];

    // 4
    [activityViewController setCompletionHandler:^(__unused NSString *activityType, __unused BOOL completed){
        // Custom completion implementation
    }];
    [self presentViewController:activityViewController animated:YES completion:NULL];
}

Description:

  1. Prepare activity items which you want to share such string, image, URL.
  2. Initialize activity view controller with the items you want to share + custom activities (only if needed). Custom activities means that you will create your own buttons using UIActivity with own actions, images, titles (more info here). For example, implement sharing to a third party app which does not have share extension.
  3. Here you can specify excluded activity types. In this example I have removed everything such as copy, airdrop and etc.
  4. In completion block you can see which activityType was selected and whether user cancelled or not.

More information on UIActivityViewController can be found here.

Regarding second part of your question about UIBarButtonItem. UIBarButtonItem can be initialised with custom title using this:

UIBarButtonItem *shareButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Share" style:UIBarButtonItemStylePlain target:self action:@selector(shareButtonPressed)];
self.navigationItem.rightBarButtonItem = shareButtonItem;

Be aware that UIBarButtonItem has UIBarButtonItemStyle which you can change. UIBarButtonItemStylePlain (will create a button with a regular font) and UIBarButtonItemStyleDone (will create a button with a bold font).

Community
  • 1
  • 1
gontovnik
  • 690
  • 5
  • 9
  • Thanks for the help. I was able to change the icon to the word 'Share" but as far as coding the native sharing functionality I can't seem to get it right. I tried the code you gave me and editing it around but when I press the share button i get "Thread 1: signal SIGABRT" error. All i need it to do is share the title and link of the article to whichever choice the user makes to share it on – Christophorus Aug 24 '15 at 16:14
  • It is probably because you did not change/removed **[UIImage imageNamed:@"imageToShare"**. It gave it only as an example. Make sure you specify correct items. If you don't, application will try to initialize array with *nil* object and will crash. – gontovnik Aug 24 '15 at 16:22
  • I did remove it, I looked at the example from the link you posted and replaced it with some of that. Trying to use "feedTitle" and "feedURL" from my project but still errors – Christophorus Aug 24 '15 at 16:28
  • Could you please upload screenshot? – gontovnik Aug 24 '15 at 16:28
  • Cannot see any. The best way to show image in comments is to give a URL to any thirdparty website. – gontovnik Aug 24 '15 at 16:34
  • Sure, I know I need to edit it more but here it is so far: http://imgur.com/mtMX6zZ – Christophorus Aug 24 '15 at 16:36
  • You see, you are getting error when you try to cast NSString to NSURL. To create NSURL from NSString you should use: `NSURL *url = [NSURL URLWithString:string];` – gontovnik Aug 24 '15 at 16:38
  • Can you capture screenshot of debug panel? Also would be great if you could add exception breakpoint so we can see where it crashes. – gontovnik Aug 24 '15 at 19:11