Showing posts with label initWithURL example. Show all posts
Showing posts with label initWithURL example. Show all posts

Sunday, June 16, 2013

NSFileWrapper initWithURL example in Objective C (iOS).


NSFileWrapper initWithURL

Initializes a file wrapper instance whose kind is determined by the type of file-system node located by the URL.

- (id)initWithURL:(NSURL *)url options:(NSFileWrapperReadingOptions)options error:(NSError **)outError

Parameters of [NSFileWrapper initWithURL]
url
URL of the file-system node the file wrapper is to represent.
options
Option flags for reading the node located at url. See “File Wrapper Reading Options” for possible values.
outError
If an error occurs, upon return contains an NSError object that describes the problem. Pass NULL if you do not want error information.

Return Value of [NSFileWrapper initWithURL]
File wrapper for the file-system node at url. May be a directory, file, or symbolic link, depending on what is located at the URL. Returns NO (0) if reading is not successful.

Discussion of [NSFileWrapper initWithURL]
If url is a directory, this method recursively creates file wrappers for each node within that directory. Use the fileWrappers method to get the file wrappers of the nodes contained by the directory.

NSFileWrapper initWithURL example.
- (NSData *)exportToNSData {
    NSError *error;
    NSURL *url = [NSURL fileURLWithPath:_docPath];
    NSFileWrapper *dirWrapper = [[[NSFileWrapper alloc] initWithURL:url options:0 error:&error] autorelease];
    if (dirWrapper == nil) {
        NSLog(@"Error creating directory wrapper: %@", error.localizedDescription);
        return nil;
    }  

    NSData *dirData = [dirWrapper serializedRepresentation];
    NSData *gzData = [dirData gzipDeflate];   
    return gzData;
}

Example of [NSFileWrapper initWithURL].
NSError *error;
NSFileWrapper *fw = [[NSFileWrapper alloc] initWithURL:url options:0 error:&error];
// After the above method has been called, `error` is either `nil` (if all went well),
// or non-`nil` (if something went wrong).

NSFileWrapper initWithURL example.

- (BOOL)readFromURL:(NSURL *)url error:(NSError **)outError;
{
self.fileWrapper = [[NSFileWrapper alloc] initWithURL:url options:nil error:outError];
NSString *rtfFilename = [url lastPathComponent];
NSFileWrapper *rtfWrapper = [self.fileWrapper.fileWrappers objectForKey:rtfFilename];
NSString *rtfString = [[NSString alloc] initWithData:[rtfWrapper regularFileContents] encoding:NSUTF8StringEncoding];
NSAttributedString *attributedString = [OUIRTFReader parseRTFString:rtfString];

_text = [attributedString copy];

return YES;
}

End of NSFileWrapper initWithURL example article.

Sunday, May 12, 2013

NSInputStream initWithURL example ios


initWithURL:

Initializes and returns an NSInputStream object that reads data from the file at a given URL.
- (id)initWithURL:(NSURL *)url
Parameters
url
The URL to the file.
Return Value( NSInputStream initWithURL example )
An initialized NSInputStream object that reads data from the file at url. If the file specified by url doesn’t exist or is unreadable, returns nil.
( NSInputStream initWithURL example )
NSInputStream * stream = [[NSInputStream alloc] initWithURL:yamlURL];
( NSInputStream initWithURL example )
- (void)viewDidLoad
{

int actuallyRead=0;

NSString *path=[self dateFilePath];

NSURL *audiourl=[[NSURL alloc]initFileURLWithPath:path];

NSLog(@"%@",audiourl);

inStream=[[NSInputStream alloc] initWithURL: audiourl];
[inStream open];
actuallyRead=[inStream read:buffer maxLength:sizeof(buffer)];

NSLog(@"%d",actuallyRead);

[dataBuffer1 appendBytes:buffer length:actuallyRead];

NSLog(@"%d",actuallyRead);

[inStream release];

[super viewDidLoad];

}

Wednesday, May 1, 2013

NSURLResponse initWithURL example ios


initWithURL :MIMEType:expectedContentLength:textEncodingName:

Returns an initialized NSURLResponse object with the URL, MIME type, length, and text encoding set to given values.
- (id)initWithURL:(NSURL *)URL MIMEType:(NSString *)MIMETypeexpectedContentLength:(NSInteger)length textEncodingName:(NSString *)name
Parameters of [NSURLResponse initWithURL]
URL
The URL for the new object.
MIMEType
The MIME type.
length
The expected content length.This value should be –1 if the expected length is undetermined
name
The text encoding name. This value may be nil.
Return Value of [NSURLResponse initWithURL]
An initialized NSURLResponse object with the URL set to URL, the MIME type set toMIMEType, length set to length, and text encoding name set to name.
Discussion
This is the designated initializer for NSURLResponse.
Example of [NSURLResponse initWithURL]
NSURLRequest *request =  [NSURLRequest 
       requestWithURL:cachedURL 
       cachePolicy:NSURLRequestReloadIgnoringLocalCacheData 
       timeoutInterval:60.0] ;

 NSURLResponse *responseToCache = 
      [[NSURLResponse alloc]
       initWithURL:[request URL] 
       MIMEType:@"text/html" 
       expectedContentLength:[dataAtURL length] 
       textEncodingName:nil] ;

 NSCachedURLResponse *cachedResponse = 
      [[NSCachedURLResponse alloc]
       initWithResponse:responseToCache data:dataAtURL
       userInfo:nil storagePolicy:NSURLCacheStorageAllowedInMemoryOnly] ;

  // Store it
 [[NSURLCache sharedURLCache] storeCachedResponse:cachedResponse forRequest:request] ;

 [responseToCache release] ;
 [cachedResponse release] ;

 NSLog( @"*** request %@, cache=%@", request ,[[NSURLCache sharedURLCache] 
   cachedResponseForRequest:request] 

NSURLRequest initWithURL example ios


initWithURL:

Returns a URL request for a specified URL with default cache policy and timeout value.
- (id)initWithURL:(NSURL *)theURL
Parameters of [NSURLRequest initWithURL]
theURL
The URL for the request.
Return Value
The initialized URL request.
Discussion of [NSURLRequest initWithURL]
The default cache policy is NSURLRequestUseProtocolCachePolicy and the default timeout interval is 60 seconds.
Example of [NSURLRequest initWithURL]
// Create request
self.imageRequest = [[NSURLRequest alloc] initWithURL:imageURL 
                                          cachePolicy:(self.cachesImage) ? NSURLRequestReturnCacheDataElseLoad : NSURLRequestReloadIgnoringLocalCacheData 
                                      timeoutInterval:self.downloadTimeoutInterval];

// Begin download
self.imageData = nil;
self.imageConnection = [[NSURLConnection alloc] initWithRequest:self.imageRequest 
                                                       delegate:self 
                                               startImmediately:YES];
Example of [NSURLRequest initWithURL]
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:_url];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
self.connection = connection;
[connection release];
[request release];