tags:

views:

318

answers:

3

Hi,

I want to use a protocol, how can we implement it in iPhone.

///In POCViewController.h

#import

@protocol BasicAPI -(NSString*)hello; @end @interface HessianPOCViewController : UIViewController { idbasicAPI;

}

@end

///

// In POCViewController.m // In Some method

NSURL* url = [NSURL URLWithString@"http://www.caucho.com/hessian/test/basic"];

id proxy = (id)[CWHessianConnection proxyWithURL:url protocol:@protocol(basicAPI)];

NSLog(@"hello: %@", [proxy hello]);

////

Please help me how I can implement above code?

A: 

A good explanation of protocols and delegates, including sample code, can be found here: http://iphonedevelopertips.com/objective-c/the-basics-of-protocols-and-delegates.html

alku83
A: 

In the above code snippet - the @protocol block goes in your header file, underneath the @end declaration that's already there. Common use case is something like:

@interface MyClass
// properties, method definitions, etc
@end

@protocol BasicAPI

-(NSString*)hello;

@end

Then in some method body in your implementation file, MyClass.m

-(void)myMethod { 
   NSURL* url = [NSURL URLWithString@"http://www.caucho.com/hessian/test/basic"];
   id proxy = (id)[CWHessianConnection proxyWithURL:url protocol:@protocol(basicAPI)];
   NSLog(@"hello: %@", [proxy hello]);
}
bpapa
Its not working geeting error: "Can not find protocol declaration for BasicApi"
iPhoneDev
@iPhoneDev check your capitalization (BasicApi vs. BasicAPI) (and make sure you're including the .h file)
David Gelhar
opsss sorry my bad... the change is "BasicApi" to "BasicAPI" Thanks a lot :)
iPhoneDev
A: 

I see that the example you give is taken from the documentation for the Hessian Objective-C implementation. It's showing you how to interact with a Hessian web service from an Objective-C client.

Do you have an existing Hessian web service that you're trying to talk to? If so, you need to declare in your @protocol block the interface to that service. The answers to this question give some good examples of how this works on both the client & server side.

David Gelhar