【问题标题】:Write .kml files in iOS在 iOS 中编写 .kml 文件
【发布时间】:2012-04-02 13:02:56
【问题描述】:

我想知道如何在应用程序中写入文件。我在这里尝试做的是,我正在生成一个 KML(使用来自https://github.com/skjolber/xswi 的类 XMLWriter)。我可以记录 KML,一切都很好,现在我要做的是,将此 KML 与 .kml 文件相同,然后通过电子邮件将其发送到某个地址。

发送电子邮件没问题,我想知道如何将此 KML 写入 .kml 文件,然后检索它以发送电子邮件。有人可以帮我写一个代码 sn-p。

【问题讨论】:

    标签: iphone ios ios4 xcode4.2


    【解决方案1】:

    这个tutorial 做到了,更多的是帮助.. 生成 .kml 文件的代码:

    NSFileManager *filemgr;
    NSData *databuffer;
    NSString *dataFile;
    NSString *docsDir;
    NSArray *dirPaths;
    
    filemgr = [NSFileManager defaultManager];
    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    docsDir = [dirPaths objectAtIndex:0];
    dataFile = [docsDir stringByAppendingPathComponent: @"datafile.kml"];
    databuffer = [[xmlWriter toString] dataUsingEncoding: NSASCIIStringEncoding];
    [filemgr createFileAtPath: dataFile contents: databuffer attributes:nil];
    

    然后我用它来检索它:

    NSFileManager *filemgr;
    NSString *dataFile;
    NSData *databuffer;
    NSString *docsDir;
    NSArray *dirPaths;
    
    filemgr = [NSFileManager defaultManager];
    
    // Identify the documents directory
    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    docsDir = [dirPaths objectAtIndex:0];
    
    // Build the path to the data file
    dataFile = [docsDir stringByAppendingPathComponent: @"datafile.kml"];
    databuffer = [filemgr contentsAtPath: dataFile];
    

    【讨论】:

      【解决方案2】:

      我相信您已经将所有内容都放入了一个字符串中,如果是这样,您可以继续将其保存到 .kml 文件中,如下所示:

       NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
                                                            NSUserDomainMask, YES);   
      NSString* documentsDirectory = [paths objectAtIndex:0];     
      NSString* fname = [tmp_FileName stringByAppendingFormat: @".kml" ]; 
      NSString* filenameStr = [documentsDirectory
                                stringByAppendingPathComponent:fname];
      [yourKMLString writeToFile: filenameStr atomically: YES];
      

      【讨论】:

      • 我在我的手机上,所以我没有办法测试代码,如果你有任何问题,请告诉我
      • 是的,我已经获得了字符串中的所有内容。顺便说一句,我将如何获取文件以将其设置为附件?非常感谢:)
      • 嗯,这取决于您使用的是什么作曲家,我只做过一次,所以我没有那么多经验,但是您可以看看这个线程stackoverflow.com/questions/5509618/…。他们在这里发送一个 .doc 文件,但是如果您更改 mime 类型,您也可以为 kml 做这个技巧。希望对您有所帮助!
      • 这个tutorial 做到了,然后帮助......谢谢:)
      猜你喜欢
      • 1970-01-01
      • 2011-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多