MSMessage url 在将其设置为文件的 url 后保持为零
MSMessage url stays nil after setting it to url of a file
我有一个带有 iOS 10 iMessage 应用程序的应用程序。当我将文件 URL 附加到 MSMessage
时 message.URL
是 (null)
。我真的不知道是什么原因造成的。当我检查日志时,我看到正确的 url: URL: file:///thisuser/...
等。但是,message.URL
日志 (null)
.
我已经构建了一个 Exporter
class,这会将文件保存到磁盘,然后 return 是它的路径。
+ (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;
}
这将 return 类似于:/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);
}];
==
编辑:我在代码中添加了一个检查以查看 Exporter return 是否是一个有效的文件路径,结果是有效的。
NSURL *fileURL = [NSURL fileURLWithPath:[Exporter saveRequestToDisk:[Converter databaseToRequest:history]]];
if ([fileURL isFileURL]) {
NSLog(@"is File URL!");
message.URL = fileURL;
}
查看 docs and skimming this article 后,我认为 url 属性 不应指向文件。相反,它应该
[...][encode] data to be transmitted with the message.
我想正确的做法是
Encode your application’s data in the URL. For example, you can encode data as key-value pairs in the URL’s query string, as shown below:
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...)
因此,与其将字典转换为 NSData 并将其写入文件,不如将其编码为 queryItems,可能像这样:
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];
我有一个带有 iOS 10 iMessage 应用程序的应用程序。当我将文件 URL 附加到 MSMessage
时 message.URL
是 (null)
。我真的不知道是什么原因造成的。当我检查日志时,我看到正确的 url: URL: file:///thisuser/...
等。但是,message.URL
日志 (null)
.
我已经构建了一个 Exporter
class,这会将文件保存到磁盘,然后 return 是它的路径。
+ (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;
}
这将 return 类似于:/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);
}];
== 编辑:我在代码中添加了一个检查以查看 Exporter return 是否是一个有效的文件路径,结果是有效的。
NSURL *fileURL = [NSURL fileURLWithPath:[Exporter saveRequestToDisk:[Converter databaseToRequest:history]]];
if ([fileURL isFileURL]) {
NSLog(@"is File URL!");
message.URL = fileURL;
}
查看 docs and skimming this article 后,我认为 url 属性 不应指向文件。相反,它应该
[...][encode] data to be transmitted with the message.
我想正确的做法是
Encode your application’s data in the URL. For example, you can encode data as key-value pairs in the URL’s query string, as shown below:
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...)
因此,与其将字典转换为 NSData 并将其写入文件,不如将其编码为 queryItems,可能像这样:
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];