Showing posts with label NSURL. Show all posts
Showing posts with label NSURL. Show all posts

Wednesday, May 1, 2013

NSURL URLByStandardizingPath example ios


URLByStandardizingPath

Returns a new URL that points to the same resource as the original URL and is an absolute path.
- (NSURL *)URLByStandardizingPath
Return Value of [NSURL URLByStandardizingPath]
A new URL that points to the same resource as the original URL and is an absolute path.
Discussion of [NSURL URLByStandardizingPath]
This method only works on URLs with the file: path scheme. This method will return an identical URL for all other URLs.
Example of [NSURL URLByStandardizingPath]
if([a isEqual:b]) {
  NSLog(@"Same"); 
}

if([[a absoluteURL] isEqual:[b absoluteURL]]) {
  NSLog(@"Same"); 
}

if([[a URLByStandardizingPath] isEqual:[b URLByStandardizingPath]]) {
  NSLog(@"Same"); 
}

NSURL URLByResolvingSymlinksInPath example ios


URLByResolvingSymlinksInPath

Returns a new URL that points to the same resource as the original URL and includes no symbolic links.
- (NSURL *)URLByResolvingSymlinksInPath
Return Value of [NSURL URLByResolvingSymlinksInPath]
A new URL that points to the same resource as the original URL and includes no symbolic links.
Discussion of [NSURL URLByResolvingSymlinksInPath]
If the original URL has no symbolic links, the returned URL is identical to the original URL.
This method only works on URLs with the file: path scheme. This method will return an identical URL for all other URLs.
Example of [NSURL URLByResolvingSymlinksInPath]
NSURL * myURLWithoutSymlinks = [myURLWithSymlinks URLByResolvingSymlinksInPath];
Example of [NSURL URLByResolvingSymlinksInPath]
+ (NSArray *)enumerateContentsOfFolderWithPath:(NSURL *)aFolderPath
{
    NSError *error = nil;
    NSArray *contentProperties = @[NSURLIsDirectoryKey,
                                   NSURLIsReadableKey,
                                   NSURLCreationDateKey,
                                   NSURLContentAccessDateKey,
                                   NSURLContentModificationDateKey];

    NSArray *contents = [[NSFileManager defaultManager] contentsOfDirectoryAtURL:aFolderPath
                                   includingPropertiesForKeys:contentProperties
                                                      options:NSDirectoryEnumerationSkipsHiddenFiles
                                                        error:&error];
    if (error != nil)
        DLog(@"Content enumeration error: %@", error);

    NSMutableArray *pdfURLs = [NSMutableArray array];

    for (NSURL *item in contents)
    {
        NSURL *fileURL = [NSURL fileURLWithPath: [item path]];
        NSURL *noSimlink = [fileURL URLByResolvingSymlinksInPath];
        [pdfURLs addObject: noSimlink];
    }
    return pdfURLs;
}

NSURL URLByDeletingPathExtension example ios


URLByDeletingPathExtension

Returns a new URL made by deleting the path extension, if any, from the original URL.
- (NSURL *)URLByDeletingPathExtension
Return Value of [NSURL URLByDeletingPathExtension]
A new URL with the path extension of the original URL removed.
Discussion of [NSURL URLByDeletingPathExtension]
If the original URL represents the root path, the returned URL is identical. If the URL has multiple path extensions, only the last one is removed.
Example of [NSURL URLByDeletingPathExtension]
NSString *path;
NSURL *filepath;
fileurl = [sheet URL];
fileurl = [fileurl URLByDeletingPathExtension];
fileurl = [fileurl URLByAppendingPathExtension:@"yyy"];
path = [fileurl path];
Example of [NSURL URLByDeletingPathExtension]
           NSUInteger count = 0;
            NSString *filePath = nil;
            do {
                NSString *extension = ( NSString *)UTTypeCopyPreferredTagWithClass(( CFStringRef)AVFileTypeQuickTimeMovie, kUTTagClassFilenameExtension);
                NSString *fileNameNoExtension = [[asset.defaultRepresentation.url URLByDeletingPathExtension] lastPathComponent];
                NSString *fileName = [NSString stringWithFormat:@"%@-%@-%u",fileNameNoExtension , AVAssetExportPresetLowQuality, count];
                filePath = NSTemporaryDirectory();
                filePath = [filePath stringByAppendingPathComponent:fileName];
                filePath = [filePath stringByAppendingPathExtension:extension];
                count++;

            } while ([[NSFileManager defaultManager] fileExistsAtPath:filePath]);

            NSURL *outputURL = [NSURL fileURLWithPath:filePath];

NSURL URLByDeletingLastPathComponent example ios


URLByDeletingLastPathComponent

Returns a new URL made by deleting the last path component from the original URL.
- (NSURL *)URLByDeletingLastPathComponent
Return Value
A new URL with the last path component of the original URL removed.
Discussion of [NSURL URLByDeletingLastPathComponent]
If the original URL represents the root path, the returned URL is identical. Otherwise, if the original URL has only one path component, the new URL is the empty string.
Example of [NSURL URLByDeletingLastPathComponent]
NSString *myURLString = @"http://www.test.com/folder/testfolder";
NSURL *myURL = [NSURL URLWithString:myURLString];
myURL = [myURL URLByDeletingLastPathComponent];
myURLString = [myURL absoluteString];
Example of [NSURL URLByDeletingLastPathComponent]
NSURL *documentDir = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] objectAtIndex:0];
NSURL *tmpDir = [[documentDir URLByDeletingLastPathComponent] URLByAppendingPathComponent:@"tmp" isDirectory:YES];
NSLog(@"tmpDir: %@", [tmpDir path]);

NSURL URLByAppendingPathExtension example ios


URLByAppendingPathExtension:

Returns a new URL made by appending a path extension to the original URL.
- (NSURL *)URLByAppendingPathExtension:(NSString *)pathExtension
Parameters of [NSURL URLByAppendingPathExtension]
pathExtension
The path extension to add to the URL.
Return Value
A new URL with pathExtension appended.
Discussion of [NSURL URLByAppendingPathExtension]
If the original URL ends with one or more forward slashes, these are removed from the returned URL. A period is inserted between the two parts of the new URL.
Example of [NSURL URLByAppendingPathExtension]
NSURL *baseURL = [NSURL URLWithString:@"http://foo.com/bar/baz"];
NSURL *appendExtension = [baseURL URLByAppendingPathExtension:@"qux"];
NSURL *appendComponent = [baseURL URLByAppendingPathComponent:@"qux"];

STAssertEqualObjects([appendExtension absoluteString], @"http://foo.com/bar/baz.qux", nil);
STAssertEqualObjects([appendComponent absoluteString], @"http://foo.com/bar/baz/qux", nil);
Example of [NSURL URLByAppendingPathExtension]
NSURL *originalURL = [NSURL URLWithString:@"http://hello.com/news"];
NSLog(@"%@", [originalURL URLByAppendingPathComponent:@"local"]);
NSLog(@"%@", [originalURL URLByAppendingPathExtension:@"local"]);

NSURL URLByAppendingPathComponent example ios


URLByAppendingPathComponent:

Returns a new URL made by appending a path component to the original URL.
- (NSURL *)URLByAppendingPathComponent:(NSString *)pathComponent
Parameters of [NSURL URLByAppendingPathComponent]
pathComponent
The path component to add to the URL.
Return Value
A new URL with pathComponent appended.
Discussion of [NSURL URLByAppendingPathComponent]
If the original URL does not end with a forward slash and pathComponent does not begin with a forward slash, a forward slash is inserted between the two parts of the returned URL, unless the original URL is the empty string.
Example of [NSURL URLByAppendingPathComponent]
@interface NSURL ( Extensions )
- (NSURL*) urlByAppendingPathComponent: (NSString*) component;
@end

@implementation NSURL ( Extensions )
- (NSURL*) urlByAppendingPathComponent: (NSString*) component;
{
    CFURLRef newURL = CFURLCreateCopyAppendingPathComponent( kCFAllocatorDefault, (CFURLRef)[self absoluteURL], (CFStringRef)component, [component hasSuffix:@"/"] );
    return [NSMakeCollectable(newURL) autorelease];
}
@end
and then:
*base = [NSURL URLWithString:@"http://my.url/path"];
*suffix = @"sub/path";
NSURL *final = [base urlByAppendingPathComponent:suffix];
NSLog( @"%@", final );
// displays http://my.url/path/sub/path
Example of [NSURL URLByAppendingPathComponent]
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Database.sqlite"];

// If the database doesn't exist copy in the default one
if (![storeURL checkResourceIsReachableAndReturnError:NULL])
{
    NSURL *defaultStoreURL = [[NSBundle mainBundle] URLForResource:@"Database" withExtension:@"sqlite"];

    if ([defaultStoreURL checkResourceIsReachableAndReturnError:NULL])
    {
        NSFileManager *fileManager = [NSFileManager defaultManager];
        [fileManager copyItemAtURL:defaultStoreURL toURL:storeURL error:NULL];
    }
}