使用cocoa连接到本地网络上的设备

时间:2012-02-01 08:56:22

标签: objective-c cocoa

我一直在尝试为我的ipad开发一个应用程序,我可以使用该应用程序连接到我的本地网络设备以获取文件等等而没有任何进展。 我想要做的是首先检测网络上的设备,然后通过连接来选择我想要浏览的文件。

到目前为止我得到的是

    #import "BrowseForNetworkDevices.h"

@implementation BrowseForNetworkDevices

- (id)init
{    
    self = [super init];

    if (self) 
    {        
        services = [[NSMutableArray alloc] init]; 
        serviceBrowser = [[NSNetServiceBrowser alloc] init];
        [serviceBrowser setDelegate: delegateObject];        

        searching = NO;        
    }

    return self;    
}

- (void)dealloc
{    
    [services release];    
    [serviceBrowser release];
    [super dealloc];    
}



// Sent when browsing begins
- (void)netServiceBrowserWillSearch:(NSNetServiceBrowser *)browser
{    
    searching = YES;    
    [serviceBrowser searchForServicesOfType:@" " inDomain:@"local."];
    [self updateUI];    
}

// Sent when browsing stops
- (void)netServiceBrowserDidStopSearch:(NSNetServiceBrowser *)browser
{    
    searching = NO;    
    [self updateUI];    
}

// Sent if browsing fails
- (void)netServiceBrowser:(NSNetServiceBrowser *)browser
             didNotSearch:(NSDictionary *)errorDict
{    
    searching = NO;    
    [self handleError:[errorDict objectForKey:NSNetServicesErrorCode]];    
}

// Sent when a service appears
- (void)netServiceBrowser:(NSNetServiceBrowser *)browser
           didFindService:(NSNetService *)aNetService
               moreComing:(BOOL)moreComing
{    
    //[services addObject:aNetService];
    if (![services containsObject:aNetService]) {
        [self willChangeValueForKey:@"services"];
        [services addObject:aNetService];
        [self didChangeValueForKey:@"service"];
    }
    if(!moreComing)        
    {       
        [self updateUI];        
    }    
}

// Sent when a service disappears
- (void)netServiceBrowser:(NSNetServiceBrowser *)browser
         didRemoveService:(NSNetService *)aNetService
               moreComing:(BOOL)moreComing
{
    //[services removeObject:aNetService];
    if ([services containsObject:aNetService]) {
        [self willChangeValueForKey:@"service"];
        [services removeObject:aNetService];
        [self didChangeValueForKey:@"service"];
    }
    if(!moreComing)        
    {       
        [self updateUI];       
    }  
}

// Error handling code
- (void)handleError:(NSNumber *)error
{    
    NSLog(@"An error occurred. Error code = %d", [error intValue]);

   // Handle error here    
}



// UI update code
- (void)updateUI
{

    if(searching)        
    {        
        // Update the user interface to indicate searching
        for(Class obj in services){
            NSLog(@"service found %@", obj);
        }
        // Also update any UI that lists available services

    }    
    else        
    {        
        // Update the user interface to indicate not searching        
    }

}


@end

关于如何执行此类任务的任何好教程?

提前致谢所有帮助

1 个答案:

答案 0 :(得分:0)

您需要网络设备和iPad使用相同的协议。您需要选择合适的协议。听起来你想要做的就是文件传输 - 有许多协议可以处理这个在iOS库中实现的协议:
- WebDAV
- HTTP
- FTP
- SFTP
- 法新社
- SAMBA
为了使设备能够相互检测,您需要使用Apple的Bonjour

之类的东西