I have been trying to figure out a way to access my windows shared folder using iPhone. The desired functionality is part of bigger enterprise app I am building. Here is someone who has already asked a similar question but no luck - Does iOS support file operations via SMB?
Now, I have found apple developer tutorial called "SimpleNetworkStreams" which employs NSNetService to use x-SNSUpload protocol over tcp by setting type of NSNetService instance to protocol x-SNSUpload._tcp
Here is how they have done it -
self.netService = [[[NSNetService alloc] initWithDomain:@"local." type:@"_x-SNSUpload._tcp." name:@"Test"] autorelease];
So if I just replace _x-SNSUpload._tcp
with _smb._tcp
and run SMB server on my macbook. I run following set of commands to start SMB on my macbook
dns-sd -R Test _smb._tcp. "" 12345
nc -l 12345 > tmp.png
Then I am able to transfer a picture in my iPhone to my macbook's root directory. I was hoping to do the same with shared folder on windows machine.
The name of the share folder is "test sharing". I have explicitly shared my 'test sharing' folder in my windows machine with full control to everyone. The complete details of code is put below (after Update)
If I directly type in "smb:\\10.212.19.121" on my browser I am able to access my shared folder. It opens the finder application and gives me an option to mount the "temp sharing" folder.
Update - lot of redundant text taken out from above and better details on how SimpleNetworkStreams work and what I have tweaked is put below.
The code is taken from - SimpleNetworkStreams -
- Open the stream of type NSInputStream for the file we want to send
//Open a stream for the file we're going to send
//filepath is a NSString with path to the file on iPhone
self.fileStream = [NSInputStream inputStreamWithFileAtPath:filepath];
assert(self.fileStream != nil);
[self.fileStream open];
- As how apple documentation says
"you can either create an NSNetService object directly (if you know the exact host and port information) or you can use an NSNetServiceBrowser object to browse for services. "
An object of NSNetService is instantiated for the server which hosts SMB server
// Open a stream to the server, finding the server via Bonjour. Then configure
// the stream for async operation.
//here's the tweak.
//original code looked like -
//self.netService = [[[NSNetService alloc] initWithDomain:@"local." type:@"_x-SNSUpload._tcp." name:@"Test"] autorelease];
self.netService = [[[NSNetService alloc] initWithDomain:@"10.212.19.121" type:@"_smb._tcp." name:@"lanmanserver"] autorelease];
assert(self.netService != nil);
NSDictionary *newDict = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:@"domain\\username",@"password",@"C:\\Documents and Settings\\username\\Desktop\\test%20sharing",nil] forKeys:[NSArray arrayWithObjects:@"u",@"p",@"path",nil]];
[self.netService setTXTRecordData:[NSNetService dataFromTXTRecordDictionary:newDict]];
Get the output stream object of type NSOutputStream into self.networkStream.
success = [self.netService getInputStream:NULL outputStream:&output];
assert(success);
self.networkStream = output;
[output release];
self.networkStream.delegate = self;
[self.networkStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[self.networkStream open];
and then the NSOutputStream delegate catches NSStreamEventHasSpaceAvailable where we buffer in the input file and then write that buffer to our NSOutputStream object i.e. networkStream
bytesWritten = [self.networkStream write:&self.buffer[self.bufferOffset] maxLength:self.bufferLimit - self.bufferOffset];
self.netService = [[[NSNetService alloc] initWithDomain:@"10.212.19.121" type:@"_smb._tcp." name:@"lanmanserver"] autorelease];
.I am setting TXTRecords in a local client NSNetService object.. – Lisbethlisbon