Saturday, November 8, 2014

MAC OS Application few common codes

As part of a test project, had to create a sample app that can do the following

- Open a file, and for this show a file explorer window
- Read contents from the file
- Create a webview and load content into the webview

The code is something like below for all these

1. Showing a file opener window
 NSOpenPanel* openDlg = [NSOpenPanel openPanel];
    [openDlg setPrompt:@"Select"];
    [openDlg beginWithCompletionHandler:^(NSInteger result){
        NSArray* files = [openDlg URLs];
        if(files && [files count] > 0)
        {
            NSURL *fileUrl = [files objectAtIndex:0];
            NSString *absString = [fileUrl absoluteString];
            absString = [absString stringByReplacingOccurrencesOfString:@"file://" withString:@""];
            NSLog(@"File URL is :%@ : abs url:%@",fileUrl, absString);
            [self.textLabel setStringValue:@"Computing..."];
            self.locationChangeReports = [[NSMutableArray alloc]init];
            [NSThread detachNewThreadSelector:@selector(scanAndFindLocationChangeReports:) toTarget:self withObject:absString];
        }

    }];


- Showing a web view

//    NSURL *myURL = [NSURL URLWithString:@"http://www.google.com"];
//    NSURLRequest *myRequest = [NSURLRequest requestWithURL:myURL];
    [self.webView.mainFrame loadHTMLString:[self getMapDataString] baseURL:nil];

Difference from iOS webview is that there is a mainFrame in which loadHTMLString is provided. 

No comments:

Post a Comment