0

我有一个NSMutableArray self.certificates

该数组由保存的字符串和核心数据组成。我想通过 AirDrop 发送这个。我已经检查过了serialization,我正在尝试用以下方式发送它

- (void)send{

NSData *jsonData2 = [NSJSONSerialization dataWithJSONObject:self.certificates options:NSJSONWritingPrettyPrinted error:nil];

NSString *jsonString = [[NSString alloc] initWithData:jsonData2 encoding:NSUTF8StringEncoding];
NSLog(@"Electrical Certificates List:\n%@", jsonString);


UIActivityViewController *activityCtr = [[UIActivityViewController alloc] initWithActivityItems:@[jsonString]
                                                                          applicationActivities:nil];

NSMutableArray *excludedActivities = [self iOSActivities].mutableCopy;
[excludedActivities addObject:UIActivityTypeAddToReadingList];
[excludedActivities addObject:UIActivityTypePostToFlickr];
[excludedActivities addObject:UIActivityTypePostToTencentWeibo];
[excludedActivities addObject:UIActivityTypePostToVimeo];
[activityCtr setExcludedActivityTypes:excludedActivities];

[self presentViewController:activityCtr
                   animated:YES
                 completion:nil];
}

这给了我以下错误
'NSInvalidArgumentException', reason: 'Invalid type in JSON write (Certificate)'

我已将数据转换为字符串,所以不确定我在这里缺少什么

我研究了 NSInvalidArgumentException,原因:“JSON 写入 (__NSDate) 中的类型无效”以及如何将 NSArray 发送到 Web 服务

4

1 回答 1

4

JSONSerialization在将数组转换为数据之前,由 , 引发异常。

要共享自定义数据类型,您需要在模型对象上实现NSCoding和:UIActivityItemSource

@interface CertificateGroup : NSObject <NSCoding, UIActivityItemSource>

@property(copy, nonatomic) NSArray *certificates;

@end

@implementation CertificateGroup

- (void)encodeWithCoder:(NSCoder *)aCoder {
    // Save all your custom properties
    [aCoder encodeObject:self.certificates forKey:@"certificates"]l
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super init];
    if (self) {
        // Read back properties
        self.certificates = [aDecoder decodeObjectForKey:@"certificates"];
    }
    return self;
}

- (id)activityViewControllerPlaceholderItem:(UIActivityViewController *)activityViewController
{
    //Let the activity view controller know NSData is being sent by passing this placeholder.
    return [NSData data];
}

- (id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(NSString *)activityType
{
    //Serialize this object for sending. NSCoding protocol must be implemented for the serialization to occur.
    return [NSKeyedArchiver archivedDataWithRootObject:self];
}

- (NSString *)activityViewController:(UIActivityViewController *)activityViewController dataTypeIdentifierForActivityType:(NSString *)activityType {
    return @"com.mycompany.myapp.certificates";
}

@end

然后,当您创建活动视图控制器时:

CertificatesGroup *group = [CertificatesGroup new];
group.certificates = self.certificates;
UIActivityViewController *activityCtr = [[UIActivityViewController alloc] initWithActivityItems:@[group]
                                                                          applicationActivities:nil];
...

您的应用程序委托应实施-application:openURL:sourceApplication:annotation:和解码传入的证书。

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
     NSData *groupData = [NSData dataWithContentsOfURL:url];
     CertificatesGroup *group = [NSKeyedUnarchiver unarchiveObjectWithData:groupData];
     NSLog(@"%@", group.certificates);
     return YES;
}

有关详细信息,请参阅 Apple 的AirDropSample项目,尤其是APLProfile.h/ .mAPLProfileViewController.h/.mAppDelegate.m.

于 2014-03-31T19:38:35.960 回答