将MSMessage url设置为文件的url后,它将保持为nil

时间:2016-11-01 14:57:20

标签: ios objective-c msmessage msmessagetemplatelayout

我有一个iOS 10 iMessage应用程序的应用程序。当我将文件网址附加到MSMessage message.URL(null)。我真的不知道是什么导致了这一点。当我查看日志时,我会看到一个正确的网址:URL: file:///thisuser/...等。但是,message.URL会记录(null)

我已经构建了一个Exporter类,这会将文件保存到磁盘,然后返回它的路径。

+ (NSString *) saveToDisk:(NSDictionary *)dictionary {
    // Figure out destination name (in public docs dir)
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *zippedName = [self getExportFileName:dictionary withExtension:YES];
    NSString *zippedPath = [documentsDirectory stringByAppendingPathComponent:zippedName];

    // Export to data buffer
    NSData *gzData = [NSKeyedArchiver archivedDataWithRootObject:dictionary];

    if (gzData == nil) return FALSE;

    // Write to disk
    [gzData writeToFile:zippedPath atomically:YES];

    return zippedPath;
}

这将返回类似于:/Users/thisuses/Library/Developer/CoreSimulator/Devices/.../Documents/new-save.rst的内容,其中.rst是我的应用的自定义文件扩展名。反过来,这会添加到MSMessage

MSConversation *conversation = [self activeConversation];

MSMessageTemplateLayout *layout = [[MSMessageTemplateLayout alloc] init];
layout.image = [UIImage imageNamed:@"test"];
layout.caption = url.host;

MSMessage *message = [[MSMessage alloc] init];
message.layout = layout;

NSLog(@"Converter: %@", [Converter toDictionary:array]);
NSLog(@"Exporter: %@", [Exporter saveToDisk:[Converter toDictionary:array]]);
NSLog(@"URL: %@", [NSURL fileURLWithPath:[Exporter saveToDisk:[Converter toDictionary:array]]]);

message.URL = [NSURL fileURLWithPath:[Exporter saveToDisk:[Converter toDictionary:array]]];

NSLog(@"Message URL 1: %@", message.URL);

[conversation insertMessage:message completionHandler:^(NSError * error) {
    NSLog(@"MSConvo error: %@",error);
}];

== 编辑:我在代码中添加了一个检查,以查看导出器是否返回有效的文件路径,结果是,确实如此。

NSURL *fileURL = [NSURL fileURLWithPath:[Exporter saveRequestToDisk:[Converter databaseToRequest:history]]];

if ([fileURL isFileURL]) {
    NSLog(@"is File URL!");
    message.URL = fileURL;
}

1 个答案:

答案 0 :(得分:3)

在查看docs并略读this article之后,我认为url属性不应该指向文件。相反它应该

  

[...] [编码]与消息一起传输的数据。

我想正确的方法是

  

在URL中对您的应用程序数据进行编码。例如,您可以将数据编码为URL查询字符串中的键值对,如下所示:

guard let components = NSURLComponents(string: myBaseURL) else {
    fatalError("Invalid base url")
}

let size = NSURLQueryItem(name: "Size", value: "Large")
let count = NSURLQueryItem(name: "Topping_Count", value: "2")
let cheese = NSURLQueryItem(name: "Topping_0", value: "Cheese")
let pepperoni = NSURLQueryItem(name: "Topping_1", value: "Pepperoni")
components.queryItems = [size, count, cheese, pepperoni]

guard let url = components.url  else {
    fatalError("Invalid URL components.")
}

message.url = url

(从文档中获取的代码,您可能希望将其转换为ObjC ...)

因此,您可能希望将其编码为queryItems,而不是将字典转换为NSData并将其写入文件,可能是这样的:

NSMutableArray *queryItems = [[NSMutableArray alloc] init]; // Or initWithCapacity for the sake of performance...
NSDictionary *dict = [Converter toDictionary:array];

for (id key in dict) {
    id value = queryDictionary[key];
    NSURLQueryItem *queryItem = [NSURLQueryItem queryItemWithName:key value:value];
    [queryItems addObject:queryItem];
}

[url setQueryItems:queryItems];