Showing posts with label nsdata. Show all posts
Showing posts with label nsdata. Show all posts

Monday, 24 June 2013

UIDocument does not allow you to write strings directly to disk

If you are using UIDocument in your app, always remember that UIDocument doesn't allow you to write strings directly to disk.

You need to package the string value in NSData then you can write it to disk. You can do it using the contentsForType:error method.

Below is a sample code, first check that the string is not NULL :

- (id)contentsForType:(NSString *)typeName error:(NSError **) myError{
    if (!self.stringDocument)
        self.stringDocument = @"";

    NSData *nsDataDocument = [self.stringDocument
                           dataUsingEncoding:NSUTF8StringEncoding];
    return nsDataDocument;
}

written by: infinitywebseo 

Tuesday, 30 April 2013

(id) loadXMLByURL Method (local xml, xml on server)

If you are parsing xml data on your iOS program, please keep in mind that the code is slightly different when read xml file local from when you read it from a server.


Parsing Local XML file: ( xmlparser.m )


-(id) loadXMLByURL:(NSString *)urlString
{
   
   
    NSData *xmlData = [NSData dataWithContentsOfFile:urlString];
    NSXMLParser  *parser = [[NSXMLParser alloc] initWithData:xmlData];


    parser.delegate = self;
    [parser parse];
    return self;
   
}


Parsing  XML file from Server: ( xmlparser.m )


-(id) loadXMLByURL:(NSString *)urlString
{    
   
    NSData *xmlData = [NSData dataWithContentsOfFile:urlString];
    NSXMLParser  *parser = [[NSXMLParser alloc] initWithData:xmlData];


   NSURL *url = [NSURL URLWithString:urlString];
    NSXMLParser parser = [[NSXMLParser alloc]initWithContentsOfURL: url];


    parser.delegate = self;
    [parser parse];
    return self;
   
}


If you have any Questions please comment.


written by: infinitywebseo