UIActivityViewController issue iOS 7 and iOS 8?

15,622

Solution 1

Following line is the issue

AVC.popoverPresentationController.sourceView = _webView;

You will have to put iOS8 condition in order popoverPresentationController is introduced for iOS 8 and later so you can not use it with iOS 7

For checking for iOS8 you can define a macro like found from here

#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)

And use it in following way.

     NSURL *linkURL = [NSURL URLWithString:_DetailModal1[4]];//article url
     NSMutableAttributedString *stringText = [[NSMutableAttributedString alloc]  initWithString:_DetailModal1[0]];//_DetailModal1[0] contain article title////
    [stringText addAttribute:NSLinkAttributeName value:linkURL range:NSMakeRange(0, stringText.length)];
    NSArray * itemsArray = @[[NSString stringWithFormat:@"%@",_DetailModal1[0]], [NSURL URLWithString:_DetailModal1[4]]];
    NSArray * applicationActivities = nil;
    UIActivityViewController * AVC = [[UIActivityViewController alloc] initWithActivityItems:itemsArray applicationActivities:applicationActivities];

   if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")){

        AVC.popoverPresentationController.sourceView = _webView;
   }
   [self presentViewController:AVC animated:YES completion:nil];

Refer this for more info about what has changed for UIActivityViewController in iOS8

Solution 2

A lot might argue that checking for existence of the class explicitly is better than checking a hard coded version number. UIPopoverPresentationController may be deprecated at some future point, or there might be a (future ?) device which does not support the class, like the iPhone never used to support UIPopoverController or UISplitViewController..

if (  NSClassFromString(@"UIPopoverPresentationController")   ) {

AVC.popoverPresentationController.sourceView = _webView;


}
Share:
15,622
Daljeet
Author by

Daljeet

Updated on June 04, 2022

Comments

  • Daljeet
    Daljeet almost 2 years

    I’m building an article reading app for iPad. I have integrated a social sharing functionality which means user can share articles on Facebook, and google mail. I’m using UIActivityViewController for sharing.

    There is a bar button item,when user click on that UIActivityViewController opens.I updated Xcode 6 When I run on simulator it runs fine But I run on real device(iPad) with iOS 7,the app get crash on clicking on bar button item. this is my code:

         - (IBAction)ysshareAction:(id)sender
           {
    
             NSURL *linkURL = [NSURL URLWithString:_DetailModal1[4]];//article url
             NSMutableAttributedString *stringText = [[NSMutableAttributedString alloc]  initWithString:_DetailModal1[0]];//_DetailModal1[0] contain article title////
            [stringText addAttribute:NSLinkAttributeName value:linkURL range:NSMakeRange(0, stringText.length)];
            NSArray * itemsArray = @[[NSString stringWithFormat:@"%@",_DetailModal1[0]], [NSURL URLWithString:_DetailModal1[4]]];
            NSArray * applicationActivities = nil;
            UIActivityViewController * AVC = [[UIActivityViewController alloc] initWithActivityItems:itemsArray applicationActivities:applicationActivities];
            AVC.popoverPresentationController.sourceView = _webView;
            [self presentViewController:AVC animated:YES completion:nil];
            [AVC setCompletionHandler:^(NSString *act, BOOL done)
            {
    
            if([act isEqualToString:UIActivityTypeMail]) {
             ServiceMsg = @"Mail sent!";
         } else if([act isEqualToString:UIActivityTypePostToTwitter]) {
             ServiceMsg = @"Article Shared!";
         } else if([act isEqualToString:UIActivityTypePostToFacebook]) {
             ServiceMsg = @"Article Shared!";
         } else if([act isEqualToString:UIActivityTypeMessage]) {
             ServiceMsg = @"SMS sent!";
         } else if([act isEqualToString:UIActivityTypeAddToReadingList]) {
             ServiceMsg = @"Added to Reading List";
         } else if([act isEqualToString:UIActivityTypeCopyToPasteboard]){
             ServiceMsg = @"Copied Link";
         }
    
         if ( done )
         {
             UIAlertView *Alert = [[UIAlertView alloc] initWithTitle:ServiceMsg message:@"" delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil];
             [Alert show];
    
              }
           }];
    
        }
    

    Help is appreciated!

  • Daljeet
    Daljeet over 9 years
    Thanks,could you please tell me what is isGreaterThaniOS8 here. It shows an error: Use of undeclared identifier 'isGreaterThaniOS8'
  • Bhumit Mehta
    Bhumit Mehta over 9 years
    Please check my edited answer , its a macro used to check if iOS version is greater than or equal to 8.0
  • Bhumit Mehta
    Bhumit Mehta over 9 years
    Okey , it seems like there was some problem in macro , i have updated my answer.
  • mluisbrown
    mluisbrown over 9 years
    Agree with checking existence of a class, however, as of iOS 4.2, the recommended way of checking the existence of a class is to use the NSObject class method. In this case: if ([UIPopoverPresentationController class]) {...} The advantage is you get compiler type checking.
  • eugen
    eugen about 8 years
    The solution I was looking for. Thanks.