ios read content from private network address - objective-c

I need to read a file that come for example:
\\192.168.0.1\Folder\Readme.txt
how can I read this file from my app into the iPhone

NSString *pathToTextFile;
NSError *readError;
NSString *fileData = [NSString stringWithContentsOfFile:pathToTextFile
encoding:NSUTF8StringEncoding
error:*readError]
NSLog(#"here is your file as string = %#",fileData);

I think in this case you can use a library like ASIHTTP. Link
It should be possible to download the file into a NSString object, and then store this object into a file.
[nsStringObject writeToFile:pathToFile atomically:YES encoding:stringEncoding error:errorHandler];

As you suggest in your question, you need to access your file over SMB protocol (samba or windows share). I don't think iOS supports smb out of the box, however, i stumbled across tango library on github some time ago. The library claims to be a SMB/CIFS implementation for iOS, so i guess you might give it a try.

Related

Standard temporary directory in OSX

I'm trying to figure what is the best place where to store temporary files in the OSX version of my application.
The obvious answer /tmp is not good since it is cleaned up at boot and my application may need to continue an interrupted job also after a restart.
I tried also to use the path pointed by the environment variable TMPDIR, that is the same returned by NSTemporaryDirectory(), that changes every boot and is something like:
/var/folders/wx/p4rqqs8d1ws0wlpx9dkwsh_80000gn/T/
.. but also the contents of this path are removed at boot.
There is a standard path where I can place some temporary files, resilient to restarts, or I have to invent my own solution (ie ~/Library/myapplication/temp)?
In Windows I'm using GetTempPath() and it works the way it should.
I've found my answer googling harder that I did before asking here, in this excellent article:
https://www.cocoawithlove.com/2009/07/temporary-files-and-folders-in-cocoa.html
Reading the articles and the various options I found that the Caches directory (NSCachesDirectory) is the correct place where to store my files. Placing them in "Application Support" will cause them to be backed up by time machine.
So here is what I did:
const char *get_temporary_dir()
{
NSString *path = nil;
NSString *bundleName = [[[NSBundle mainBundle] infoDictionary] objectForKey:#"CFBundleIdentifier"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
if ([paths count]) {
path = [[paths objectAtIndex:0] stringByAppendingPathComponent:bundleName];
} else {
path = NSTemporaryDirectory();
path = [path stringByAppendingPathComponent:bundleName];
}
return [path UTF8String];
}
... I'm not sure if the fallback to the standard, deletable, directory is needed but it doesn't hurt!
You should use the "application support directory" - this is typically ~/Library/Application Support or for a sandboxed application and equivalent within its container.
To obtain the URL for this directory you use URLForDirectory:inDomain:appropriateForURL:create:error: passing as first argument NSApplicationSupportDirectory.
Within this directory you need to create a directory just for your application, using your app's bundle ID is a common strategy for naming this directory.
This directory is intended to store files needed by your application, but not your user's files.

Create temp file for a sandboxed Cocoa application

My app is sandboxed (as per the latest App Store guidelines), and I want to create some temporary files.
Am I allowed to do so? If "yes", WHERE am I allowed to do it? Is there any prespecified path? (And a command to access that path?)
You should use the NSTemporaryDirectory() function, which will find and return the appropriate temporary folder for your application (regardless of sandbox status, OS version, and a host of other things). Take a look at this Cocoa With Love post for much more detail about NSTemporaryDirectory() and other temporary directory-related details.
There is a good article about temporary directories on NSHipster:
http://nshipster.com/nstemporarydirectory/
The author suggests this code which is working perfectly with sandboxed apps as well:
NSError *error;
NSString *globallyUniqueString = [[NSProcessInfo processInfo] globallyUniqueString];
NSString *tempDirectoryPath = [NSTemporaryDirectory() stringByAppendingPathComponent:globallyUniqueString];
NSURL *tempDirectoryURL = [NSURL fileURLWithPath:tempDirectoryPath isDirectory:YES];
[[NSFileManager defaultManager] createDirectoryAtURL:tempDirectoryURL withIntermediateDirectories:YES attributes:nil error:&error];

Returning two strings from php to ios

I'm creating an app that gets some values from a mysql database via php.
I've gone as far as returning a string that's echoed via php and using it in objective C.
Here's what I have so far:
NSString * strURL = [NSString stringWithFormat:#"http://localhost/search.php?name=%#",name];
NSData * dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];
NSString * result = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];
NSLog(#"%#", result);
Is it possible to return 2 different strings from php and using them separately in xcode or do I have to make 2 different calls to the php file?
Thank you very much for your help!
Great start!
Consider using some structured way to return data from PHP. One easy format that you can learn, which will help with other API integration later, would be JSON.
Apple ships some simple code to to the conversion in iOS5 with NSJSONSerialization
On the PHP side, play around with json_encode. You can pass in an indexed array, for example, which will give you an NSArray on the iOS side.
Some more examples for the iOS side:
How to use NSJSONSerialization

Retrieve info.plist file from command-line tool

Using the following steps, I'm able to embed an info.plist into a command-line tool.
http://developer.apple.com/library/mac/#documentation/Security/Conceptual/CodeSigningGuide/Procedures/Procedures.html
I know how to retrieve the plist file from a .bundle, but I'm not sure how to do the same in a single-file tool like I've got.
I've embedded the info.plist into the command-line tool so that I can store the version in it. Does anyone know how I can retrieve it at run-time so I can determine what version is running?
Thanks
__info_plist is a "magic" section name that makes the following Just Work:
NSBundle *bundle = [NSBundle mainBundle];
id version = [bundle objectForInfoDictionaryKey: (NSString*) kCFBundleVersionKey];
NSLog(#"mainBundle.version = %#", version);
If you need to read a bundle embedded in a different executable than the one currently running, this answer by Bavarious from the comments has a more comprehensive list of approaches.

Send data from an iPhone to a Web service

I'm developing an iPad application in which a user fills in their details and presses a submit button, which sends the information to a specific Web server (which will later be viewed by a person)
As far as protocols for Web services are concerned, I know JSON and XML. Are there any other protocols that I should be looking into? (or perhaps by a different method completely)
I'd be very grateful for any help.
If you just want to send text info to server you can try this code:
NSString *textdata = [YourTextField text];
NSString *anotherTextdata = [YourAnotherTextField text];
NSString *urlpath;
urlpath = [#"http://yoursiteapiurl.com/" stringByAppendingString:#"yourserverfile.php?textdata="];
urlpath = [urlpath stringByAppendingString:textdata];
urlpath = [urlpath stringByAppendingString:#"&anotherTextData="];
urlpath = [urlpath stringByAppendingString:anotherTextdata];
NSURL *url=[[NSURL alloc] initWithString:[urlpath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSString *a = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
The variable a will have the response of this URL. The server could send XML and then you can parse that XML using any XML parsing technique.
you can use tbxml for it, its very easy to implement. Follow the link
http://www.tbxml.co.uk/TBXML/TBXML_Free.html
If sending the data over HTTP is an option, I would recommend you look into the excellent ASIHTTPRequest library. As for encoding, I've found the json-framework library to be good.
Use AFNetworking for this.
AFNetworking is smart enough to load and process structured data over the network, as well as plain old HTTP requests. In particular, it supports JSON, XML and Property Lists (plists).