IOS 7 App Crash when NSAttributedString + NSString sent to UIActivityViewController and Mail app selected

3

I wanted to confirm that the following functionality is supported:

I have a simple app where I am trying to send an array of strings to UIActivityViewController. The array contains an attributed string and also a regular string. When I click on the share button and open the Mail app, I get a crash and error. However when I only send the attributed string by itself, it works.

I have tried this on IOS6 and it works fine on that.

Here is the configuration:

 NSAttributedString *appName=[[NSAttributedString alloc] initWithString:@"TasteBank\n"     
       attributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:[UIFont  
       systemFontSize]],NSForegroundColorAttributeName:[UIColor brownColor]}];

//crash seen when @"test" added to array without that no crash when mail app opened on ios7

NSMutableArray *dataToShare = [[NSMutableArray alloc ] initWithObjects: appName, @"test", nil];

UIActivityViewController* activityViewController =
        [[UIActivityViewController alloc] initWithActivityItems:dataToShare applicationActivities:nil];

[self presentViewController:activityViewController animated:YES completion:^{}];

IS there something wrong with this? Is this configuration supported in IOS7? if so, could this be a bug?

I see the following error when I run on my iPhone 4 with IOS 7:

2013-10-17 11:43:50.609 testing12[2499:60b] -[NSConcreteMutableAttributedString appendString:]:        unrecognized selector sent to instance 0x165ae670
2013-10-17 11:43:50.612 testing12[2499:60b] Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSConcreteMutableAttributedString appendString:]: unrecognized selector sent to instance 0x165ae670' 
First throw call stack:
(0x2ff53f53 0x3a32c6af 0x2ff578e7 0x2ff561d3 0x2fea5598 0x32c389a1 0x328e2f2b 0x328e2bb1 0x328e2791 0x328e23b7 0x328e20dd 0x3284c101 0x3284c101 0x326f9601 0x326f468d 0x326c9a25 0x326c8221 0x2ff1f18b 0x2ff1e65b 0x2ff1ce4f 0x2fe87ce7 0x2fe87acb 0x34ba8283 0x32729a41 0xbd1ed 0x3a834ab7)

I see this error when run on IOS7.0 simulator in XCODE 5:

2013-10-17 11:23:42.054 testing12[3258:a0b] -[NSConcreteMutableAttributedString appendString:]: unrecognized selector sent to instance 0xa181470
2013-10-17 11:23:42.085 testing12[3258:a0b] Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSConcreteMutableAttributedString appendString:]: unrecognized selector sent to instance 0xa181470'
First throw call stack:
(0   CoreFoundation                      0x017345e4 exceptionPreprocess + 180 
1    libobjc.A.dylib                     0x014b78b6 objc_exception_throw + 44
2   CoreFoundation                      0x017d1903 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
3   CoreFoundation                      0x0172490b forwarding___ + 1019
4   CoreFoundation                      0x017244ee _CF_forwarding_prep_0 + 14
5   UIKit                               0x008bdfa3 -[UIMailActivity prepareWithActivityItems:] + 2346
6   UIKit                               0x008b9242 -[UIActivityViewController _executeActivity] + 424
7   UIKit                               0x008ba824 -[UIActivityViewController _performActivity:] + 1359
8    libobjc.A.dylib                     0x014c981f -[NSObject performSelector:withObject:] + 70
9   UIKit                               0x0063c75a -[UIActivityGroupViewController collectionView:didSelectItemAtIndexPath:] + 148
10  UIKit                               0x0083929b -[UICollectionView _selectItemAtIndexPath:animated:scrollPosition:notifyDelegate:] + 605
11  UIKit                               0x008516d8 -[UICollectionView _userSelectItemAtIndexPath:] + 189
12  UIKit                               0x00851895 -[UICollectionView touchesEnded:withEvent:] + 437
13  libobjc.A.dylib                     0x014c9874 -[NSObject performSelector:withObject:withObject:] + 77
14  UIKit                               0x00382f92 forwardTouchMethod + 271
15  UIKit                               0x00383002 -[UIResponder touchesEnded:withEvent:] + 30
16  libobjc.A.dylib                     0x014c9874 -[NSObject performSelector:withObject:withObject:] + 77
17  UIKit                               0x00382f92 forwardTouchMethod + 271
18  UIKit                               0x00383002 -[UIResponder touchesEnded:withEvent:] + 30
19  UIKit                               0x0059dd7f _UIGestureRecognizerUpdate + 7166
20  UIKit                               0x00268d4a -[UIWindow _sendGesturesForEvent:] + 1291
21  UIKit                               0x00269c6a -[UIWindow sendEvent:] + 1030
22  UIKit                               0x0023da36 -[UIApplication sendEvent:] + 242
23  UIKit                               0x00227d9f _UIApplicationHandleEventQueue + 11421
24  CoreFoundation                      0x016bd8af CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
25  CoreFoundation                      0x016bd23b __CFRunLoopDoSources0 + 235
26  CoreFoundation                      0x016da30e __CFRunLoopRun + 910
27  CoreFoundation                      0x016d9b33 CFRunLoopRunSpecific + 467
28  CoreFoundation                      0x016d994b CFRunLoopRunInMode + 123
29  GraphicsServices                    0x036859d7 GSEventRunModal + 192
30  GraphicsServices                    0x036857fe GSEventRun + 104
31  UIKit                               0x0022a94b UIApplicationMain + 1225
32  testing12                           0x00002efd main + 141
33  libdyld.dylib                       0x01d70725 start + 0
34  ???                                 0x00000001 0x0 + 1
)
iphone
ios
ios7
nsattributedstring
uiactivityviewcontroller
asked on Stack Overflow Oct 17, 2013 by shvetaP

2 Answers

4

I don't know if this will help but I fixed my problem with a custom activity provider that just gave a NSString to email types and the attributed string to all the others. The email controller autodetected that the string was HTML and formatted it correctly anyway.

I guess if you can get the same effect from HTML that you can with your attributed string this might help?

@implementation ProductActivityItemProvider

...

- (id)placeholderItem {
    return [[NSAttributedString alloc] initWithString:@""];
}

- (NSString *)activityViewController:(UIActivityViewController *)activityViewController subjectForActivityType:(NSString *)activityType {

    if ([activityType isEqualToString:UIActivityTypeMail])
        return [NSString stringWithFormat:@"I shared %@", self.product.title];

    return nil;
}

- (id)item {
    NSString *raw = [NSString stringWithFormat:@"<html><body>%@<br />%@<br /><b>%@</b></body></html>",
                     self.product.designer,
                     self.product.title,
                     self.product.price];

    // The mail controller will (a) autodetect HTML and (b) crash if it's given an attributed string. sigh.
    if ([self.activityType isEqualToString:UIActivityTypeMail])
        return raw;

    NSData *data = [raw dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *options = @{                                                                                  
        NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType,                                               
        NSCharacterEncodingDocumentAttribute : @(NSUTF8StringEncoding) 
    };

    NSAttributedString *text = [[NSAttributedString alloc] initWithData:data
                                                                options:options
                                                     documentAttributes:nil
                                                                  error:nil];
    return text;
}

@end
answered on Stack Overflow Dec 11, 2013 by deanWombourne
0

Got some fast solution here. Just implement a category:

@implementation NSMutableAttributedString (append)
-(void)appendString:(id)string{
    if([string isKindOfClass:[NSString class]]){
        [self appendAttributedString:[[NSAttributedString alloc] initWithString:string]];

    }else if ([string isKindOfClass:[NSAttributedString class]]){
        [self appendAttributedString:string];
    }
}
@end
answered on Stack Overflow Jun 21, 2015 by dotAramis

User contributions licensed under CC BY-SA 3.0