Get A Small File Method This test will lock up if there is WiFi but no internet connection: if (![self.reachability isReachable]) This works much better as it tests the connection to an actual file on the server you want to connect to (assuming you can get a named file from it) and allows you to […]
Category: TCP/IP
Download File
Get String From Text File – (NSString *)GetTextFile { NSString *urlString = @"http://mydomain.com/my_file.txt"; NSURL *url = [NSURL URLWithString:urlString]; return [NSString stringWithContentsOfURL:url encoding:NSASCIIStringEncoding error:nil]; } The above works great, but if the iOS device has a WiFi connection but no internet connectivity or if the server is not responding it will lock up indefinately. Use […]
Download File In Background
A Quick & Simple Method Get file //Get file on a background thread to stop GUI locking up dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSData* data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@”http://mydomain.com/comefile.php”]]; [self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES]; }); File Received //*********************************** //*********************************** //********** FILE RECEIVED ********** //*********************************** //*********************************** – (void)fetchedData:(NSData *)responseData { } A Fuller Featured Method In your .h […]
Get Device IP Address
This code is based on Shivan Raptor’s answer at http://stackoverflow.com/questions/7072989/iphone-ipad-how-to-get-my-ip-address-programmatically //**************************************** //**************************************** //********** GET OUR IP ADDRESS ********** //**************************************** //**************************************** #import #import // Get IP Address – (NSString *)GetOurIpAddress { NSString *address = @”error”; struct ifaddrs *interfaces = NULL; struct ifaddrs *temp_addr = NULL; int success = 0; // retrieve the current interfaces – returns 0 […]
TCP Client
This guide is based on the excellent Ray Wenderlick guide Adding A TCP Client To A ViewController Class In your #ViewController.h #import @interface MyViewController : UIViewController <NSStreamDelegate> //-ADD THIS { NSInputStream *InputStream; NSOutputStream *OutputStream; NSMutableData *OutputData; } – (void)TcpClientInitialise; @end In your #ViewController.m //************************************** //************************************** //********** VIEW WILL APPEAR ********** //************************************** //************************************** //View about to […]
UDP Server
This is based on using the AsyncUdpSocket library. If you don’t use git you’ll have to download a git client to get the library files. Add Framework To Your Project Select the project at the top of the left navigator panel, select the target and then select the Build Phases tab. In the Link Binary with […]
Using URL’s
Basic URL NSURL *url = [NSURL URLWithString:@”http://www.mydomain.com/some_folder/movie.mov”]; //or NSString *urlAddress = @”http://www.google.com”; NSURL *url = [NSURL URLWithString:urlAddress]; Simple Downloading data from a URL Synchronous methods which block until they have the result: NSData *downloadData = [NSData dataWithContentsOfURL:url]; Same synchronous method but with Options and Error //—– DOWNLOAD FILE —– NSError *error = nil; NSURL *url […]