ios - MSMessage url 在将其设置为文件的 url 后保持为 nil

标签 ios objective-c msmessage msmessagetemplatelayout

我有一个带有 iOS 10 iMessage 应用程序的应用程序。当我将我的文件 URL 附加到 MSMessage 时,message.URL(null)。我真的不知道是什么原因造成的。当我检查日志时,我看到一个正确的 url:URL: file:///thisuser/... 等。但是,message.URL 日志 (空)

我构建了一个 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);
}];

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

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

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

最佳答案

查看 docs 后和略读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 - MSMessage url 在将其设置为文件的 url 后保持为 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40362668/

相关文章:

ios - UIPickerView,检测 "rolling wheel"起停?

ios - 不能在 Storyboard之间继续

ios - 为什么我不能在 Swift 中自己运行命令?

ios - CBCentralManager 可以扩展自定义协议(protocol)吗?我们可以覆盖 CBCentralManagerDelegate 吗?

ios - 谁实际上是本地参与者标识符?

ios - 为iPhone和iPad开发同一个应用程序:针对每种设备类型针对所有应用程序开发一个应用程序还是针对两个应用程序开发两个应用程序?

iphone - ScrollView 仅在 View 出现第二次时才开始滚动

ios - 如何在 UITableViewCell 运行时之上添加自定义 UIView?

ios - 使用 Swift 在 iMessage 中发送多条消息

ios - 为 MSMessage 设置标签或唯一标识符