代码之家  ›  专栏  ›  技术社区  ›  user4992124

MSMessage url在设置为文件的url后保持为零

  •  3
  • user4992124  · 技术社区  · 8 年前

    我有一个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 消息 .

    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 回复  |  直到 8 年前
        1
  •  3
  •   Jan Nash    8 年前

    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…)

    因此,与其将字典转换为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];
    
    推荐文章