gpt4 book ai didi

ios - 通过 AirDrop 发送 NSArray

转载 作者:行者123 更新时间:2023-11-28 19:03:24 24 4
gpt4 key购买 nike

我有一个 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',原因:'JSON 写入(证书)中的类型无效'

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

我研究过 NSInvalidArgumentException, reason: 'Invalid type in JSON write (__NSDate)'How to send NSArray to web service

最佳答案

在数组转换为数据之前,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

然后,当您创建事件 View Controller 时:

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

您是应用程序委托(delegate)人,应该实现 -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。米

关于ios - 通过 AirDrop 发送 NSArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22769911/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com