通过AirDrop发送NSArray

时间:2014-03-31 18:57:48

标签: ios objective-c cocoa-touch serialization airdrop

我有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, reason: 'Invalid type in JSON write (__NSDate)'How to send NSArray to web service

1 个答案:

答案 0 :(得分:4)

在将数组转换为数据之前,JSONSerialization抛出异常。

要共享自定义数据类型,您需要在模型对象上实施NSCodingUIActivityItemSource

@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