iPhone从桌面选取文件

发布于 2024-11-08 19:23:31 字数 998 浏览 0 评论 0原文

我是 Objective 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];

I am a beginner in objective c and iphone programming. I'm making a program for transfering files from iphone to server through gprs. I dont know that how can i pick a file from the desktop to load into the simulator and then sending the file to the server.please explain that how can i pick such file and i also dont know how to access the path of a file in a mac operating system. Can the following code be useful in picking up a file

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];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

错爱 2024-11-15 19:23:31

您将需要查看使用文档路径并将项目保存到手机/模拟器应用程序文档文件夹中(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;
}

You will need to look at using Documents path and saving items into the phone/simulators app documents folder (ios does not support user created subfolders so the hierarchies are pretty flat, exceptions being there are a few predefined places on the ios you can put files)

Examples below read/write to ios file system. You would need to transport that somehow client/server.

//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;
}
傲娇萝莉攻 2024-11-15 19:23:31

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

You can add your test file to the project as a resource.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文