IOS 7 应用程序崩溃时 NSAttributedString + NSString 发送到 UIActivityVi



我想确认支持以下功能:

有一个简单的应用程序,我正在尝试将字符串数组发送到UIActivityViewController。 该数组包含一个属性字符串和一个常规字符串。 当我单击共享按钮并打开邮件应用程序时,我遇到崩溃和错误。 但是,当我只发送属性字符串本身时,它可以工作。

我已经在 IOS6 上尝试过这个,它工作正常。

以下是配置:

 NSAttributedString *appName=[[NSAttributedString alloc] initWithString:@"TasteBankn"     
       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:^{}];

这有什么问题吗?IOS7 是否支持此配置? 如果是这样,这可能是一个错误吗?

当我在装有IOS 4的iPhone 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)

在XCODE 7.0模拟器上运行时,我看到此错误:

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
)
我不知道

这是否有帮助,但我使用自定义活动提供程序解决了我的问题,该提供程序只是为电子邮件类型提供了 NSString,并将属性字符串提供给所有其他类型。电子邮件控制器自动检测到字符串是 HTML,并且无论如何都正确设置了格式。

我想如果你能从 HTML 中获得与属性字符串相同的效果,这可能会有所帮助?

@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

这里有一些快速的解决方案。只需实现一个类别:

@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

最新更新