iPhone SDK: verify XML before processing it? - objective-c

Is there a way to verify / validate a remote XML (or download it first and store it locally) before processing it and store the info into CoreData or DB with iPhone SDK 3?

In Cocoa you can just download it
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url
cachePolicy:NSURLRequestReturnCacheDataElseLoad
timeoutInterval:30];
NSData *urlData;
NSURLResponse *response;
NSError *error = nil;
urlData = [NSURLConnection sendSynchronousRequest:urlRequest
returningResponse:&response
error:&error];
and load it into a NSXMLDocument
NSXMLDocument *doc = [[NSXMLDocument alloc]
initWithData:urlData options:0 error:&error];
and get the nodes with
NSArray* tempArray = [doc nodesForXPath:#"something/anotherthing" error:&error];
Don't know if all of this works on the iPhone.

Related

IOS6 JSON.H import in main viewcontroller (Xcode5)

I am new to ios,so inorder to get accesstoken, i followed the link
http://technogerms.com/login-with-google-using-oauth-2-0-for-ios-xcode-objective-c/ .so in these link they used json.h files are they mandatory.if it is yes then explain me about json github in these link.
Here is the link what you want.
https://github.com/johnezang/JSONKit
Use native NSJSONSerialization class, you can find more info on below link,
iOS NSJSONSerialization
How to use NSJSONSerialization
There is no compulsory to use JSON.h in your project. Now You can use inbuilt apple JSON parser. IF you want to download JSon library then link is
Download JSon file from here
Otherwise you can use this Inbuilt apple JSON Parser by using following method.
NSDictionary *res = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves || NSJSONReadingMutableContainers error:&myError];
The full code is as below:
NSString *urlString=[NSString stringWithFormat:#"%#listcontact.php?uid=%#&page=0",LocalPath,appdel.strid]; //---- Add your URL for webservice-----
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
There are two NSURLConnection request methods AsynchronousRequest and SynchronousRequest.
(1) For AsynchronousRequest
[NSURLConnection sendAsynchronousRequest:urlRequest queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
NSError *error1;
NSDictionary *res=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error1];
}];
(2) For SynchronousRequest
NSData *GETReply = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:nil error:nil];
NSError *myError = nil;
NSDictionary *res = [NSJSONSerialization JSONObjectWithData:GETReply options:NSJSONReadingMutableLeaves error:&myError];

Obj-C NSURLConnection not working

So i am trying to send data to a webservice via url with a parameter. the code i have is below but it never hits the server. the request and responses are null. What am i doing wrong?
-(void) postData:(NSString *)data{
NSURLResponse* response;
NSError* error = nil;
NSString *urlString = [NSString stringWithFormat:#"http://someaddress.com/api?data=%#", data];
NSURL *lookupURL = [NSURL URLWithString:urlString];
//Create the request.
NSURLRequest *theRequest=[NSURLRequest requestWithURL:lookupURL];
NSData *request = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&error];
NSString *dataString = [[NSString alloc] initWithData:request encoding:NSUTF8StringEncoding];
NSLog(#"-----------------------------");
NSLog(#"Request: %#", theRequest);
NSLog(#"req response: %#", request);
NSLog(#"response: %#", dataString);}
you want to POST some binary data but you do a GET request and try to put the binary into the url. (without encoding it)
sample post:
NSURL *url = [NSURL URLWithString:#"http://server.com"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = #"POST";
request.HTTPBody = postData;
NSData *respData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
also, note that the synchronous get is bad as it blocks :) use async networking!

NSURLConnection sendSynchronousRequest - missing data

I'm trying to read a text file using a synchronous request. It doesn't work but I get no errors or warnings either.
Can anyone enlighten me on what I'm doing wrong, please?
NSString *url = #"http://pappons.com/test.txt" ;
NSLog(#"getHTTPData: %#" , url ) ;
NSURLResponse* response = nil;
NSURLRequest* urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
NSData* data = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:nil] ;
NSString *myString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog( #"data: %#" , myString ) ;
output:
2012-06-15 11:33:42.209 FrederikTest[1365:707] getHTTPData: http://pappons.com/test.txt
2012-06-15 11:33:42.306 FrederikTest[1365:707] data:
pass in NSError to check if error occurred
NSError *error = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error];

Simple http request in gnustep's objective c not working

#import <Foundation/Foundation.h>
main() {
NSURL *url = [NSURL URLWithString:#"http://www.google.com/"];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:60];
NSURLResponse *response;
NSError *error;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSLog(#"Data were download below:\n%#", [[NSString alloc] initWithData:data encoding: NSASCIIStringEncoding]);
}
run the executable ->
Data were download below:
[null]
Why?
Have you tried allocating the request and error?
NSURLResponse *response = [[NSURLResponce alloc] init];
NSError *error = [[NSError alloc] init];

Download video before view Iphone sdk

I am developing an application that requires to play some video.
However I do not want to pack the videos with application. Instead I would like to download videos in the NSDocumentDirectory and then play from there using MPMoviePlayerController.
Anybody any Idea how could I download the video from a url?
Thanx,
Gezim
Try this one:
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setHTTPMethod:#"GET"];
NSError *error;
NSURLResponse *response;
NSString *documentFolderPath = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents"];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *videosFolderPath = [documentFolderPath stringByAppendingPathComponent:#"videos"];
//Check if the videos folder already exists, if not, create it!!!
BOOL isDir;
if (([fileManager fileExistsAtPath:videosFolderPath isDirectory:&isDir] && isDir) == FALSE) {
[[NSFileManager defaultManager] createDirectoryAtPath:videosFolderPath attributes:nil];
}
NSData *urlData;
NSString *downloadPath = #"http://foo.com/videos/bar.mpeg";
[request setURL:[NSURL URLWithString:downloadPath]];
urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *filePath = [videosFolderPath stringByAppendingPathComponent:#"bar.mpeg"];
BOOL written = [urlData writeToFile:filePath atomically:NO];
if (written)
NSLog(#"Saved to file: %#", filePath);
Create a NSURLRequest, call [NSURLConnection connectionWithRequest:delegate:] and implement the NSURLConnection delegate methods to receive the data as it is downloaded.