RSS
 

Archive for the ‘Cocoa’ Category

註冊 iPhone 多工背景播放音樂

14 Dec
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
 
    // Override point for customization after application launch.
    [self.window addSubview:tabBarController.view];
    [self.window makeKeyAndVisible];
 
	UIDevice *thisDevice = [UIDevice currentDevice];
 
	if([thisDevice respondsToSelector:@selector(isMultitaskingSupported)]
	   && thisDevice.multitaskingSupported) {
		UIBackgroundTaskIdentifier backgroundTask = [application beginBackgroundTaskWithExpirationHandler:^{
			/* just fail if this happens. */
			NSLog(@"BackgroundTask Expiration Handler is called");
			[application endBackgroundTask:backgroundTask];
		}];
	}
 
    return YES;
}
 
 

精省錢 將字串複製到記憶體筆記

02 Aug
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
if ([host isEqualToString:@"mympro_Copy"]) {
    NSString *command = request.URL.path.lastPathComponent;
    if ([command isEqualToString:@"mobileNumber"]) {
        NSString *mobileNumber = [[NSUserDefaults standardUserDefaults] stringForKey:@"mobileNumber"];
        if (mobileNumber.length != 0) {
            [UIPasteboard generalPasteboard].string = mobileNumber;
            [labelStatus setText:@"門號已複製到記憶體..."];
        } else {
            [labelStatus setText:@"尚未設定門號..."];
        }
    }
    if ([command isEqualToString:@"mobilePasswd"]) {
        NSString *mobilePasswd = [[NSUserDefaults standardUserDefaults] stringForKey:@"mobilePasswd"];
        if (mobilePasswd.length != 0) {
            [UIPasteboard generalPasteboard].string = mobilePasswd;
            [labelStatus setText:@"密碼已複製到記憶體..."];
        } else {
            [labelStatus setText:@"尚未設定密碼..."];
        }
    }
    return NO;
}
 
- (IBAction)btnNumberClicked:(id)sender {
	NSString *mobileNumber = [[NSUserDefaults standardUserDefaults] stringForKey:@"mobileNumber"];
 
	if (mobileNumber.length != 0) {
		[UIPasteboard generalPasteboard].string = mobileNumber;
		[labelStatus setText:@"門號已複製到記憶體..."];
	} else {
		[labelStatus setText:@"尚未設定門號..."];
	}
}
 
- (IBAction)btnPasswdClicked:(id)sender {
	NSString *mobilePasswd = [[NSUserDefaults standardUserDefaults] stringForKey:@"mobilePasswd"];
 
	if (mobilePasswd.length != 0) {
		[UIPasteboard generalPasteboard].string = mobilePasswd;
		[labelStatus setText:@"密碼已複製到記憶體..."];
	} else {
		[labelStatus setText:@"尚未設定密碼..."];
	}
}
 
 

原來NSLog();那麼好用

20 Jan
1
2
NSDictionary *playerInfo = [aNotification userInfo];
NSLog(@"%@", playerInfo);

我很好奇這個 NSDictionary 裡面裝了什麼膏藥? 就用NSLog幫忙揭開神祕的面紗 :

Album = “In the Enchanted Garden”;
“Album Rating” = 0;
“Album Rating Computed” = 1;
Artist = “Kevin Kern”;
“Artwork Count” = 1;
Genre = “New Age”;
“Library PersistentID” = “-8755280181446606464″;
Location = “file://localhost/Users/xxxxx/Music/iTunes/iTunes%20Music/Kevin%20Kern/In%20the%20Enchanted%20Garden/07%20Water%20Lilies.mp3″;
Name = “Water Lilies”;
PersistentID = 8747019994823533665;
“Play Count” = 0;
“Play Date” = “2040-02-06 06:28:16 +0800″;
“Player State” = Playing;
“Playlist PersistentID” = “-8755280181446606453″;
“Rating Computed” = 1;
“Skip Count” = 0;
“Skip Date” = “2040-02-06 06:28:16 +0800″;
“Store URL” = “itms://itunes.com/link?n=Water%20Lilies&an=Kevin%20Kern&pn=In%20the%20Enchanted%20Garden”;
“Total Time” = 257802;
“Track Count” = 10;
“Track Number” = 7;
Year = 1996;

另外, 還可以用下列方式

NSString *strOutput = [NSString stringWithFormat:@"%@", playerInfo];

NSString *strOutput = [playerInfo description];

 
 

NSThread sleep

13 Jan
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main(int argc, char *argv[])
{
    [NSThread detachNewThreadSelector:@selector(checkiTunesStatus) toTarget:self withObject:nil];
}
 
- (void) checkiTunesStatus
{	
	NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
	while (pTunesRun) {
		[NSThread sleepForTimeInterval:1.0];
	}
	[pool release];
	[NSThread release];
}

這個程式在一般的Application執行沒問題. 但是寫成 widget plugin就死翹翹了. 先自己記錄起來.之後再用這個 codes.

 
No Comments

Posted in Cocoa

 

SMS Timer for Mac OS X

28 Jun

SMS Timer

在使用Skype傳送簡訊時, 也許會期望Skype提供定時發送簡訊功能吧?! 我就有這個需求.所以索性寫了一個無敵陽春的 SMS Timer 程式.歡迎到這裡下載.

When you use the Skype to send the SMS, you may need a function which is able to let you set up the specific time to send the text message out. I need this function. So, I implemented it which is extremely basic one. If you would like to use, please download here.

 
No Comments

Posted in Cocoa

 

Windows close button quites App?

18 Dec

So what you need to do first is have the window you want to close be connected to an IBOutlet in the nib. For this example i connected the window to an outlet named “mainWindow”.

The code to add is:
Code:

- (void)awakeFromNib {
        [mainWindow setDelegate:self];
}

- (void)windowWillClose:(NSNotification *)aNotification {
	[NSApp terminate:self];
}


from : http://forums.macrumors.com/showthread.php?t=105229

 
No Comments

Posted in Cocoa

 

Customizing XCode File Templates

18 Dec

Want to customize/organize your XCode 2.1 templates??

Here’s a few things I do to help myself.

First off let’s get an understanding how this stuff works.

The location of file templates used for XCode are stored in /Library/Application Support/Apple/Developer Tools/File Templates/

You’ll find several folders in the file templates folder. Notice that the names of the folders coincide with the names of the headings in the sheet that appears when you add a new file to your project.

Also, within each of those folders are folders with names that end in .pbfiletemplate. (The ‘pb’ is a left over from the previously used Project Builder application.) Within each .pbfiletemplate folder there is a class.h, class.m template file, and a TemplateInfo.plist. Just as an example open the /Library/Application Support/Apple/Developer Tools/File Templates/Cocoa/Objective-C class.pbfiletemplate folder. In that folder you’ll find a class.m, class.h, and a TemplateInfo.plist file.

Open the TemplateInfo.plist file, which should automatically open in the Property List Editor application. In the Root node you’ll find the Description of the template you see when you select this file template in the New File dialog. You’ll also see the MainTemplateFile and its CounterpartTemplateFile which are set to class.m, and class.h respectively. Close the file without saving any changes you may have made.

Now open the class.m file. You’ll see the template of the standard .m file. Within this file you’ll see several elements using a particular pattern like the one for the file name ¬’FILENAME¬”. These elements are replaced when you create an instance of the file in your project. Most of these elements are one’s you probably don’t want to mess with. However, one such element is listed as ¬’ORGANIZATIONNAME¬” which normally defaults to “__MyCompanyName__”. This particular listing can be set up within the defaults of your XCode application. I’ll explain that later, though.

So let’s set up you own custom listing for files…

First Quit XCode and the Property List Editor applications, without saving any changes.

Now go to the /Library/Application Support/Apple/Developer Tools/File Templates/ in the Finder and create a new sub folder named “- My Classes”. Now go to the Cocoa folder and copy the ‘Objective-C class.pbfiletemplate’ to the “- My Classes” folder. Now open XCode and create a new Cocoa Application project. Now add a new file to the project. Notice you’ll see a heading of “- My Classes” with an “Objective-C class” listing. Just cancel for now, as we’ll go ahead an make some further changes.

Rename the ‘Objective-C class.pbfiletemplate’ to ‘NSObject subclass.pbfiletemplate’. Then open the TemplateInfo.plist with that folder.

Edit the Description listing under the Root element to say ‘An Objective-C class file which subclasses NSObject, with an optional header which includes the header.’

And finally, edit the .m file to use /* and */ to hold the comments rather than the normal // for each line.

Now close and save both the .h file and the TemplateInfo.plist file.

Now attempt to add a new file to your XCode project. You’ll see the “- My Classes” heading with the “NSObject subclass” listing. Select the listing and you’ll see the new description show up. Now click next, give the file and name and click finish. Notice the new commenting style on the .m file’s comments.

The quick and easy way to take care of that “__MyCompanyName__” information is to use the Terminal.

Enter the following line in the Terminal, replacing the Company Name with whatever you prefer.
defaults write com.apple.Xcode PBXCustomTemplateMacroDefinitions ‘{“ORGANIZATIONNAME” = “Company Name”;}’


from : http://cocoatravels.blogspot.com/2005/09/customizing-xcode-file-templates.html

 
 

NSTimer:timerWithTimeInterval:target:selector:userInfo:repeats:

14 Dec
+(NSTimer *) timerWithTimeInterval:(NSTimeInterval)seconds
   target:(id)target
   selector:(SEL)aSelector
   userInfo:(id)userInfo
   repeats:(BOOL)repeats

## example

#import "MyObject.h"
@implementation MyObject

NSTimer *timer;

- (IBAction)myAction:(id)sender
{
    timer = [NSTimer timerWithTimeInterval:1
                             target:self
                             selector:@selector(timerControl)
                             userInfo:nil
                             repeats:YES];

    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
}
- (IBAction)fire:(id)sender
{

}
- (IBAction)stop:(id)sender
{

}

-(void) timerControl{
    NSLog(@"...");
}
@end
 
No Comments

Posted in Cocoa

 

ls in Cocoa

08 Dec

//Launch “ls -l -a -t” in the current directory, and then read the result into an NSString:

NSTask *task;
task = [[NSTask alloc] init];
[task setLaunchPath: @"/bin/ls"];

NSArray *arguments;
arguments = [NSArray arrayWithObjects: @"-l", @"-a", @"-t", nil];
[task setArguments: arguments];

NSPipe *pipe;
pipe = [NSPipe pipe];
[task setStandardOutput: pipe];

NSFileHandle *file;
file = [pipe fileHandleForReading];

[task launch];

NSData *data;
data = [file readDataToEndOfFile];

NSString *string;
string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSLog (@”got\n%@”, string);

 
No Comments

Posted in Cocoa

 

Developing Cocoa Java Applications: A Tutorial

23 Apr

Chapter 1 Introduction to Developing Cocoa Java Applications
Chapter 2 Creating the Project and Interface
Chapter 3 Implementing Currency Converter
Chapter 4 Building and Debugging
Chapter 5 Expanding on the Basics

Click here to read