Showing and hiding an NSWindow programatically
// Show
[myWindow makeKeyAndOrderFront:self];
[NSApp activateIgnoringOtherApps:YES];
// Hide
[myWindow orderOut:self];
// Show
[myWindow makeKeyAndOrderFront:self];
[NSApp activateIgnoringOtherApps:YES];
// Hide
[myWindow orderOut:self];
First, find the process id:
$ ps -ef|grep Grab
501 50335 319 0 0:02.11 ?? 0:04.40 /Applications/Utilities/Grab.app/Contents/MacOS/Grab -psn_0_7309048
The pid is 50335 so create a D program that captures the Objective-C calls for that process:
objc50335:::entry
{
printf("%s %s\n", probemod, probefunc);
}
Save the code in a file called grab.trace.d and start tracing by executing this command:
sudo dtrace -s grab.trace.d > trace.log
Output is sent to the trace.log file.
NSDate *stopDate = [NSDate dateWithTimeIntervalSinceNow:2];
[[NSRunLoop currentRunLoop] runUntilDate:stopDate];
int choice = NSRunAlertPanel(@"title", @"description", @"DEFAULT", @"ALTERNATE", "OTHER", 8);
if(choice == NSAlertDefaultReturn)
} else if(choice == NSAlertOtherReturn) {
} else if(choice == NSAlertAlternateReturn) {
}
Open Info.plist and add the following:
NSUIElement=1
First download RegexKitLite.
Double-click the project target under Targets in the Groups & Files sidebar.
Under the Linking header add -licucore to the Other Linker Flags setting.
In the Xcode menu, open Xcode->Preferences->Documentation. Then click the Add Publisher button.
Add this URL: feed://regexkit.sourceforge.net/RegexKitLiteDocSets.atom
This will extract the integer that is embedded in a td tag:
NSString *regex = @"<td id\"amount\">(\\d+)</td>";
NSString *body = [[[NSString alloc] initWithData:html encoding: NSASCIIStringEncoding] autorelease];
NSString *amount = [body stringByMatching:regex capture:1];
if ([amount isEqual:@""] == NO) {
NSLog(@"Amount is %@", amount);
} else {
NSLog(@"Amount was not found.");
}
In MainViewController#shouldAutorotateToInterfaceOrientation return YES:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return YES; //[super shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}
Example of how to make asynchronous HTTP requests with NSURLConnection:
//
// HTTP.h
//
#import <Cocoa/Cocoa.h>
#import "HTTPDelegate.h"
@interface HTTP : NSObject {
id delegate;
NSMutableData *receivedData;
NSURL *url;
}
@property (nonatomic,retain) NSMutableData *receivedData;
@property (retain) id delegate;
- (void)get: (NSString *)urlString;
- (void)post: (NSString *)urlString;
@end
//
// HTTP.m
//
#import "HTTP.h"
@implementation HTTP
@synthesize receivedData;
- init {
if ((self = [super init])) {
}
return self;
}
- (void)dealloc {
[super dealloc];
}
- (void)setDelegate:(id)val
{
delegate = val;
}
- (id)delegate
{
return delegate;
}
- (void)get: (NSString *)urlString {
NSLog ( @"GET: %@", urlString );
self.receivedData = [[NSMutableData alloc] init];
NSURLRequest *request = [[NSURLRequest alloc]
initWithURL: [NSURL URLWithString:urlString]
cachePolicy: NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval: 10
];
NSURLConnection *connection = [[NSURLConnection alloc]
initWithRequest:request
delegate:self
startImmediately:YES];
if(!connection) {
NSLog(@"connection failed :(");
} else {
NSLog(@"connection succeeded :)");
}
[connection release];
[request release];
[receivedData release];
}
- (void)post: (NSString *)urlString {
// POST
//[request setHTTPMethod:@"POST"];
// NSString *postString = @"Some post string";
//[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
}
// ====================
// Callbacks
// ====================
#pragma mark NSURLConnection delegate methods
- (NSURLRequest *)connection:(NSURLConnection *)connection
willSendRequest:(NSURLRequest *)request
redirectResponse:(NSURLResponse *)redirectResponse {
NSLog(@"Connection received data, retain count");
return request;
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSLog(@"Received response: %@", response);
[receivedData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSLog(@"Received %d bytes of data", [data length]);
[receivedData appendData:data];
NSLog(@"Received data is now %d bytes", [receivedData length]);
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"Error receiving response: %@", error);
[[NSAlert alertWithError:error] runModal];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
// Once this method is invoked, "responseData" contains the complete result
NSLog(@"Succeeded! Received %d bytes of data", [receivedData length]);
NSString *dataStr=[[NSString alloc] initWithData:receivedData encoding:NSASCIIStringEncoding];
NSLog(@"Succeeded! Received %@ bytes of data", dataStr);
if ([delegate respondsToSelector:@selector(didFinishDownload:)]) {
NSLog(@"Calling the delegate");
//NSString* dataAsString = [[[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding] autorelease];
[delegate performSelector:@selector(didFinishDownload:) withObject: dataStr];
}
[dataStr release];
}
@end
First you'll need to setup a UIWebView in for example Interface Builder. Then in the controller you add this incredible piece of code:
NSString *path = [[NSBundle mainBundle] pathForResource:@"about" ofType:@"html" inDirectory:@"html"];
NSLog(@"%@", path);
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
This beautiful piece of code will look for a file named about.html in a folder called html. Now go create that folder and the file with your favorite tools.
Next drag that folder under your application in Groups & Files in XCode. Now to make XCode happy you have to make sure you select Create Folder References for any added folders.
That's it. If you get this error it means the file couldn't be found, and that you didn't import the files correctly:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter'
-(void) confirmDelete {
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:@"Remove account"
message:@"Are you sure you want to remove this account?"
delegate:self
cancelButtonTitle:@"Remove"
otherButtonTitles:nil];
[alertView addButtonWithTitle:@"Don't remove"];
[alertView show];
[alertView release];
}
- (void)alertView:(UIAlertView *)alertView
clickedButtonAtIndex:(NSInteger)buttonIndex {
[appDelegate.managedObjectContext deleteObject:selectedAccount];
if (buttonIndex == 0) {
NSLog(@"Remove button clicked");
} else if (buttonIndex == 1) {
NSLog(@"Cancel button clicked");
}
}
-(void)confirmDelete {
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Remove account?" delegate:self cancelButtonTitle:@"Don't remove" destructiveButtonTitle:@"Remove account" otherButtonTitles:nil];
actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
[actionSheet showInView:[UIApplication sharedApplication].keyWindow];
[actionSheet release];
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
NSLog(@"Remove button clicked");
} else if (buttonIndex == 1) {
NSLog(@"Cancel button clicked");
}
}