iphone从桌面挑选文件

时间:2011-05-20 10:33:58

标签: iphone objective-c

我是目标c和iphone编程的初学者。我正在制作一个程序,通过gprs将文件从iphone传输到服务器。我不知道如何从桌面上选择一个文件加载到模拟器然后将文件发送到服务器。请解释我如何选择这样的文件,我也不知道如何访问文件的路径一个mac操作系统。以下代码在拾取文件时是否有用

NSString *urlStr = @"192.168.178.26";
    if (![urlStr isEqualToString:@""]) {
        NSURL *website = [NSURL URLWithString:urlStr];
        if (!website) {
            NSLog(@"%@ is not a valid URL");
            return;
        }
    NSHost *host = [NSHost hostWithName:[website host]];
        [NSStream getStreamsToHost:host port:3258 inputStream:&iStream  outputStream:&oStream];
        [iStream retain];
        [oStream retain];
        [iStream setDelegate:self];
        [oStream setDelegate:self];
        [iStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
                           forMode:NSDefaultRunLoopMode];
        [oStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
                           forMode:NSDefaultRunLoopMode];
        [iStream open];
        [oStream open];

2 个答案:

答案 0 :(得分:1)

您需要查看使用Documents路径并将项目保存到phone / simulators应用程序文档文件夹中(ios不支持用户创建的子文件夹,因此层次结构非常扁平,例外情况是ios上有一些预定义的位置可以放文件)

以下示例读/写ios文件系统。你需要以某种方式运输客户端/服务器。

//ensure whatever object you pass in has the writeToFile method or this will crash. UIImage need to use UIImagePNGRepresentation or similar.
+ (void)writeToFile:(id)object withFileName:(NSString*)filename
{
    [object writeToFile:[[MyAppDelegate DocumentsPath] stringByAppendingPathComponent:filename] atomically:TRUE];
}

+ (NSData*)readFromFile:(NSString*)filename
{
    NSString* path = [[MyAppDelegate DocumentsPath] stringByAppendingPathComponent:filename];

    if ([[NSFileManager defaultManager] fileExistsAtPath:path])
    {
        return [NSData dataWithContentsOfFile:path];
    }

    return nil;
}

答案 1 :(得分:0)

您可以将测试文件作为资源添加到项目中。