2011년 3월 29일 화요일

아이폰개발:유용한 매크로...

<<성능측정>>
#define BM_START(name) NSDate *name##_start = [NSDate new]
#define BM_END(name) NSDate *name##_end = [NSDate new];\
NSLog(@"%s interval: %f", #name, [name##_end timeIntervalSinceDate:name##_start]);\
[name##_start release];[name##_end release]

#define GET_USER_NAME() [MSStringUtil stringByTrimmingSpaceWithString: [[[[KISDocument sharedKISFDocument] settings] loginData] objectAtIndex:2]]

#define GET_USER_ID() [MSStringUtil stringByTrimmingSpaceWithString: [[[KISDocument sharedKISFDocument] settings] loginID]]


#define GET_APP_DELEGATE() (ZZZZZAppDelegate *)[[UIApplication sharedApplication] delegate]

#define PUSH_VIEW_CONTROLLER(controller) controller *target = \

[[controller alloc] initWithNibName:@#controller bundle:nil]; \

[self.navigationController pushViewController:target animated:YES];\

[target release];


//내비게이션 컨트롤러 내에서 있다면 ...

#define JUMPTO(controller) NSArray *arr = self.navigationController.viewControllers;\

NSEnumerator *enumerator = [arr objectEnumerator];\

id anObject;\

NSInteger toIdx = 0;\

while (anObject = [enumerator nextObject]) {\

if ([anObject isKindOfClass:[controller class]]) {\

toIdx = [arr indexOfObject:anObject];\

break;\

}\

}\

[self.navigationController popToViewController:anObject animated:YES];


//로그인 컨트롤러, 로그인 성공시에 이동할 뷰컨트롤러

#define LOGIN_TITLE_TARGET(controller,target) controller *login = \

[[controller alloc] initWithNibName:@#controller bundle:nil]; \

login.doCertificateLogin = YES; \

target *tg = [[target alloc] initWithNibName:@#target bundle:nil]; \

[login setDestination:(target*)tg];\

[self.navigationController pushViewController:login animated:YES];\

[tg release];\

[login release];


//Singletone

#define SYNTHESIZE_SINGLETON_FOR_CLASS(classname) \

\

static classname *shared##classname = nil; \

\

+ (classname *)shared##classname \

{ \

@synchronized(self) \

{ \

if (shared##classname == nil) \

{ \

shared##classname = [[self alloc] init]; \

} \

} \

\

return shared##classname; \

} \

\

+ (id)allocWithZone:(NSZone *)zone \

{ \

@synchronized(self) \

{ \

if (shared##classname == nil) \

{ \

shared##classname = [super allocWithZone:zone]; \

return shared##classname; \

} \

} \

\

return nil; \

} \

\

- (id)copyWithZone:(NSZone *)zone \

{ \

return self; \

} \

\

- (id)retain \

{ \

return self; \

} \

\

- (NSUInteger)retainCount \

{ \

return NSUIntegerMax; \

} \

\

- (void)release \

{ \

} \

\

- (id)autorelease \

{ \

return self; \

}


아이폰개발: https로 요청하기....

요청하기....

NSURL *url;

NSMutableURLRequest *request;

NSURLConnection *connection;

//NSString *httpBody;

//URL reques 할당.

url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", AUTHENTICATION_URL, phoneNumberFld.text]];

request = [[NSMutableURLRequest alloc] initWithURL: url];

connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

[connection release];

[request release];


델리게이트에선....

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

{

//NSLog(@"did receivedata [%@]byte",[NSString stringWithUTF8String:(const char*)[data bytes]]);

//if([data length] > 0)

[responseData appendData:data];

}


- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response

{

NSLog(@"didReceiveResponse");

NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;

NSLog(@"Headers = [%@]", [httpResponse allHeaderFields]);

[responseData setLength:0];

}


- (void)connectionDidFinishLoading:(NSURLConnection *)connection

{

NSLog(@"connectionDidFinishLoading");

//JSON처리는 이쪽에서 행할것!

//NSString* recvString = [NSString stringWithUTF8String:(const char*)[responseData bytes]];

NSString *recvString = [[NSString alloc] initWithData:responseData

encoding:NSUTF8StringEncoding];


dispRecvData.text = [NSString stringWithFormat:@"", recvString];

[self checkJsonValue:dispRecvData.text];

}


- (void)connection:(NSURLConnection *)connection

didSendBodyData:(NSInteger)bytesWritten

totalBytesWritten:(NSInteger)totalBytesWritten

totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite

{

NSLog(@"didSendBodyData");

}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error

{

NSLog(@"didFailWithError");

return;

}

// 나중에 필요하면 주석을 풀어 사용하세요.

//- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {

// NSLog(@"canAuthenticateAgainstProtectionSpace");

// return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];

//}


//인증처리가 필요해요!!!!

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {

NSLog(@"didReceiveAuthenticationChallenge");

if ([challenge previousFailureCount] == 0) {

[[challenge sender] useCredential:[NSURLCredential credentialWithUser:KUSER password:KPASS persistence:NSURLCredentialPersistenceForSession] forAuthenticationChallenge:challenge];

} else {

[[challenge sender] cancelAuthenticationChallenge:challenge];

}

}

아이폰 개발: id와 SEL의 사용법

<호출하고자 하는 오브젝트 클래스>

interface에 다음과 같이 넣어두자.

id target;

SEL selector;


<호출하는 오브젝트 클래스>

- (void) example {

SampleObj* obj = [[SampleObj alloc] init];

obj.target = self;

obj.selector = @selector(callbackFunc);

//or

obj.selector = @selector(callbackFunc:);// 변수를 받을때 ":"를 반드시 붙여야함.!!!

...

}

- (void) callbackFunc {

}

//or

- (void) callbackFunc:(id)sender {


}


//sampleObject에서의 처리.

if([self.target respondsToSelector:self.selector]) {// 응답할수 있는지 체크하고.

[self.target performSelector: self.selector]; // 부른다.

//or

[self.target performSelector: self.selector withObject:@"아무거나..배열도되고 스트링도 되고."]; // 오브젝트를 변수로 넣어서 부른다.

}