How to get mac address using GCDAsynUdpSocket - ios

I am using GCDAyncSocket for UDP broadcast. I receive the data in the form of NSData. How can I get the mac address from that received data? I tried with
NSString *msg = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
But it shows the msg as nil.
How can I get the MAC address from this received data?
I would appreciate your help.

Related

How to send Numbers and Characters in one textField to Server?

I have one text field, in that text field I want to add numbers, characters like this (£ 12 /hr) and need to post to server but its taking either number or charecter I want to send both. if any suggestion it would be Appreciate.
Get the textfield's text and encode it before sending to server.
NSString *post = textfield.text
NSData *postData = [post dataUsingEncoding: NSUTF8StringEncoding allowLossyConversion:YES];
NSUTF8StringEncoding encoding will encode into proper data. After that send it to server using NSMutableURLRequest

HTML-encode special characters for JSON serialization

I have a web service that sends and accepts JSON, and handles all special characters by HTML-encoding them. I can parse and read the web service data perfectly fine using the following (error checking removed for brevity):
cleanJsonString = [self stringByDecodingXMLEntities:jsonString];
NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData: [cleanJsonString dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]
options: NSJSONReadingMutableContainers
error: &parseError];
The problem comes into play when I need to serialize user input to send data back to the server. I'm currently using the following:
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:myDictionaryObject
options:NSJSONWritingPrettyPrinted
error:&parseError];
NSString *myString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
The problem is that this does not account whatsoever for special characters. No errors occur on the client side, but the web service is receiving bad data. I know I could brute force it by recursing the dictionary object and encoding every string before serializing. But would it be safe to just do one single encode operation on the entire JSON string without corrupting it? Or, even more ideally, is there such a parameter that would handle encoding during the serialization process. Any advice or suggestions greatly appreciated.

Creating sockaddr_in for CocoaAsyncSocket Getting Invalid IPv4 address

I'm having trouble connecting to a Java socket server. I know the server is working because I connect via a Java socket. I'm using the CocoaAsyncSocket library to make a client connection from an iOS device. I've tried the following,
[socket connectToHost:#"XXX.XXX.X.XXX" onPort:9090 error:&err]
method but the server never sees the client connect and the client (CocoaAsyncSocket) thinks its connected. So thats no good, then I realized there was another connection method available.
So I'm thinking I should be using the connectToAddress method instead. I've used this post as a reference for my current code but I'm still getting an error and I'm not sure why. The only difference from my version and the suggested version is for the length they use sa_len and I was getting a error and xCode wanted to switch it to sin_len, so I did. I'm really new to direct socket connections so bear with me.
GCDAsyncSocket *socket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
struct sockaddr_in ip4addr;
ip4addr.sin_family = AF_INET;
ip4addr.sin_port = htons(9090);
inet_pton(AF_INET, "XXX.XXX.X.XXX", &ip4addr.sin_addr);
NSData *discoveryHost = [NSData dataWithBytes:&ip4addr length:ip4addr.sin_len];
NSError *err = nil;
if (![socket connectToAddress:discoveryHost error:&err])
{
NSLog(#"I CANNOT CONNECT!");
}
else
{
NSLog(#"IM CONNECTED!");
}
The connection fails and the error is,
Error Domain=GCDAsyncSocketErrorDomain Code=2 "A valid IPv4 or IPv6 address was not given" UserInfo=0x8bac880 {NSLocalizedDescription=A valid IPv4 or IPv6 address was not given}
I changed
NSData *discoveryHost = [NSData dataWithBytes:&ip4addr length:ip4addr.sin_len];
to
NSData *discoveryHost = [NSData dataWithBytes:&ip4addr length:sizeof(ip4addr)];
and it fixed that error I was getting. However, the reason I wasn't able to connect via the connectToHost method was due to my server socket code. I have two server sockets accepting connections. I commented out the second and it worked just fine. I'm guessing it was due to the thread being locked by the second socket or something.

How to decode Norwegian or any special character in a xml to display it in TextView

I am dealing with strings in my project where in i send some data (string) to a service and the service responses me XML with some data (String).Earlier i used to face probs while sending special charcters like &,",',<,>,$,%,(,) etc in the string.
Then i encoded the string before sending as below:
NSString *str=#"£&#)(;:/-.,?!'"[]{}#%^*+=Â¥$â¬><~||_~<.,?!'m"";
NSString *stringToSend=[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
then data sent is as below:
"%C2%A3%26%40%29%28%3B%3A%2F-.%2C%3F%21%27%22%5B%5D%7B%7D%23%25%5E%2A%2B%3D%C2%A5%24%E2%82%AC%3E%3C~%7C%7C_~%3C.%2C%3F%21%27m%22"
then the server handles the data as required.
While receving the data from server i do decoding as below:
NSString *strReceived=#"%C2%A3%26%40%29%28%3B%3A%2F-.%2C%3F%21%27%22%5B%5D%7B%7D%23%25%5E%2A%2B%3D%C2%A5%24%E2%82%AC%3E%3C~%7C%7C_~%3C.%2C%3F%21%27m%22";
NSString *str=[str stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
then data received is :£&#)(;:/-.,?!'"[]{}#%^*+=Â¥$â¬><~||_~<.,?!'m" as expected.
PROBLEM/ISSUE
Now if my string contains norwegian characters like å,ø,Ø,æ,etc.
the string in response contains (%E5 for å) (%E6 for æ)and (%D8 for Ø)and (%F8 for ø).
I get the string as invalid after ececution of "stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding".
Can some one suggest how to handle all the charcters received in the xml and parse them perfectly.Plz help with ur inputs...
Tnx in advance.

Connecting my iPad app to a SQL Server

I have an iPad application that uses table view controllers to present lists of data. I want to connect these tables to data in a SQL Server. I have very little experience in this realm of programming and I am not sure where to begin. In the long run I'd like adding/editing/deleting data on the app to sync with the server as well. I understand that this is a very broad question, so I am mainly looking for suggestions to help me get started. I, for example, do not want to start researching and learning Core Data if it is not the framework that can accomplish my goal.
In short, how can I connect my application to a SQL Server so that I can access its data and sync it to a device? Any example code/walkthroughs would be much appreciated.
I am currently working in an iOS application that requires this same functionality. For mySQL database queries on the server, I am using server-side PHP scripts that can accept variables, such as the table name or database search term.
What I do is I make an HTTP GET request using objective-C's NSMutableURLRequest, then have the server process the request (in PHP), and then return the database query results to my application in JSON format. I use SBJsonParser to parse the returned data into an NSData, and then an NSArray object.
An example of making an HTTP request in Objective-C:
NSString *urlString = [NSString stringWithFormat:#"http://website.com/yourPHPScript.php?yourVariable=something"];
NSURL *url = [NSURL URLWithString: urlString];
NSMutableURLRequest *request1 = [[NSMutableURLRequest alloc] initWithURL:url];
/* set the data and http method */
[request1 setHTTPMethod:#"GET"];
[request1 setHTTPBody:nil];
/* Make the connection to the server with the http request */
[[NSURLConnection alloc] initWithRequest:request1
delegate:self];
There is more code that you need to add to actually respond to the request when it returns, and I can post an example of that if you would like.
I actually dont know if this is the best way to do this, but It has worked for me so far. It does require that you know PHP though, and I don't you if you have any experience with it.
UPDATE:
Here is some sample code showing how to respond to the request. In my case, since I am getting a JSON encoded response, I use the SBJsonParser to parse the response.
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
/* This string contains the response data.
* At this point you can do whatever you want with it
*/
NSString *responseString = [[NSString alloc] initWithData:receivedData
encoding:NSUTF8StringEncoding];
/* Here I parse the response JSON string into a native NSDictionary using an SBJsonParser */
SBJsonParser *parser = [[[SBJsonParser alloc] init] autorelease];
/* Parse the JSON into an NSDictionary */
NSDictionary *responseArr = [parser objectWithString:responseString];
/* Do whatever you want to do with the response */
/* Relsease the connection unless you want to re-use it */
[connection release];
}
Also add these methods, assuming you have an NSMUtableData instance variable titled receivedData.
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[receivedData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[receivedData appendData:data];
}
I would not recommend connecting directly to a SQL server. You could, in theory, try to compile FreeTDS or unixODBC for iOS. I did not try it, but from my experience, at least FreeTDS should be fairly portable.
However, there are many other (and for most purposes better and easier) way of synchronizing data with a SQL Server. For example, you could use RestKit on the iPad side and a simple REST service on the SQL Server side. Or you could use OData which has a iOS library (which I just recently learned about, I have to admit).

Resources