Delegate Methods in TwitPic API - twitter

Can anyone explain about delegate methods in TwitPic API and tell me the architecture of those delegate methods, I mean which method calls first,second like that.
In my code I have added TwitterRequest external classes and implemented. When I build my app I am getting message "No response from Delegate". Anyone help me regarding this.
Thanks in advance.

From the code sample in the tutorial you followed, it looks like this message is logged when delegate you passed in doesn't implement the callback selector you also passed in when you made the call to the TwitterRequest class.
For example, suppose we were making a request to update status:
TwitterRequest *request = [[TwitterRequest alloc] init];
[request statuses_update:#"My status" delegate:self requestSelector:#selector(didUpdateStatus)];
This code would log the message you're seeing unless self implemented the callback didUpdateStatus. (Where self might be a view controller or similar.)
I would need to have implemented a method in my view controller something like this:
- (void)didUpdateStatus {
NSLog(#"Updated status successfully");
}
Check that you've assigned and implemented such a method. Note that this looks to be optional - if you don't want to know if your request succeeded you can either ignore this message, or implement an empty callback such as I have in this example.

Related

Passing data to delegate in swift

I have this use case where a model object (e.g. class User) has few methods.
Some of the methods in the class require authentication (e.g. getProfile, getFriends,...).
class User{
var loginDelegate:LoginDelegate
func getProfile{
HTTPAsync.getProfile(payload){response in
if response.status == 401 {
login(delegate)
}
}
func getFriends{
//similar code as above
login(delegate)
}
Once, user is successfully logged in, I want to call respective functions (getFriends, getProfile, whichever invoked login).
I have been thinking to use delegate pattern. But since my class (user) has multiple methods that require login, I need to pass some data to delegate, which must be read after user is logged in to call the appropriate method.
I am new to Swift, and was wondering if I am going in the right path. Is there any other obvious way to achieve this pretty common problem.
In my app, use a Url whiteList to solve this problem,
For example, the Url inside the user authentication interface which contains "/users/" this string (or other strings), when the user is not logged in and used a request for such a Url to send out a notification, by a unified class to receive this notification,then Pop up Login box
I am new to Swift, and was wondering if I am going in the right path. Is there any other obvious way to achieve this pretty common problem.
Yes there are a couple of ways you might choose to solve this. e
Define getter methods on your delegate protocol, if it is not your own delegate protocol you can use an extension to extend it's functionality.
Create an Enumeration as an instance variable so you can set an enumeration value with in the login method that your other methods can access after the login method finishes.
Change the login method to accept more parameters and returns a value\object.
For example:
login(delegate: LoginDelegate, dictionaryOfOtherStuff: [String :AnyObject]?) -> (value_1: String, value_2 : [int])
I can only give an example since you have not stated exactly what needs to be available after the login method is called.

How to check if my return value is used or was assigned at all to some variable?

Why do I need this?
For instance I have a method which returns an object (VKRequest) and looks like this:
VKRequest *request = [[VKUser currentUser] info];
A programmer can then initiate request as follows:
[request start];
If request should start immediately after method call, programmer should write following code:
[VKUser currentUser].startAllRequestsImmediately = YES;
[[VKUser currentUser] info];
What I really want is to eliminate startAllRequestsImmediately property from VKUser class and perform requests immediately if returned value isn't used.
Is it possible at all?
Thx.
You can't do what you're wanting to do, at least. There is no idea at runtime of "Am I assigned to a variable?" Your two options are to either have a parameter that specifies whether to start the request immediately, or have different methods for "get this thing immediately" and "get this thing after I say start".
A method/object should not care about what happens to a return value after it exits. It seems like your wanting to force poor coupling.
Another thought is that this sounds a bit like Apples NSURLConnection. See:
- (id)initWithRequest:(NSURLRequest *)request delegate:(id < NSURLConnectionDelegate >)delegate startImmediately:(BOOL)startImmediately

How to read the response headers every time using AFNetworking?

While using a 3rd party API, I have the requirement to cancel all traffic when a custom response header is set to a certain value. I am trying to find a nice place to do this check only once in my code (and not in every success/failure block, where it works fine). From what I understand, this could be done by overriding -(void)enqueueHTTPRequestOperation:(AFHTTPRequestOperation *)operation in my custom AFHTTPClient subclass, but when I implement it like that:
-(void)enqueueHTTPRequestOperation:(AFHTTPRequestOperation *)operation
{
NSLog(#"[REQUEST URL]\n%#\n", [operation.request.URL description]);
NSLog(#"[RESPONSE HEADERS]\n%#\n", [[operation.response allHeaderFields] descriptionInStringsFileFormat]);
[super enqueueHTTPRequestOperation:operation];
}
the response headers are nil. Can anybody help me with that?
At the moment when operations are being created and enqueued in AFHTTPClient, they will not have the response from the server--that will be assigned when the request operation is actually executed.
Although the requirement to cancel all traffic seems unorthodox (at least if outside of the conventions of HTTP), this is easy to accomplish:
In your AFHTTPClient subclass, add a BOOL property that stores if requests should be prevented, and then used in enqueueHTTPRequestOperation. Then, override HTTPRequestOperationWithRequest:success:failure: to execute the specified success block along with some logic to set the aforementioned property if the salient response is present.

Facebook iOS SDK - Custom Delegate

I am working on a new project with facebook integration. The app has to make different requests to facebook. The SDK provides one delegate that handles every request the same. Now i need to have a custom delegate to handle certain request different. Could somebody give me a hint on how to accomplish this?
The Facebook class exposes numerous method which return FBRequest* objects, and each of those methods have an argument for a delegate, which can be any object you like, so long as it conforms to the FBRequestDelegate protocol.
Therefore either, just have a custom class which implements the protocol for each type of request. Or, have one class, which implements the protocol, and inside those methods, you'll need to inspect the FBRequest that you receive to determine what to do. So, for example, if you're calling:
Facebook *fb = [[Facebook alloc] initWithAppId:kYourFacebookAppId];
[params setObject:[NSNumber numberWithBool:YES] forKey:#"PostComment"];
[fb requestWithParams:params andDelegate:delegate];
[fb release];
then in the delegate, you could do something like:
- (void)request:(FBRequest *)request didLoad:(id)result {
// Make a decision based on the request
if ([[request.params objectForKey:#"PostComment"] boolValue]) {
// Here we should post a comment.
}
}
However, I personally wouldn't do that, as it'll be much harder to maintain. Much better would be to write separate classes for each Facebook related task. These classes could inherit from a base FBDelegate class which does common things like error handling.

setDelegate explanation

Hi i am new to iphone developement, can any one explain me why setDelegate is used, where we should use it.
[request setDelegate:sender];
thanks in advance.
Delegates are simply a design pattern; there is no special syntax or language support.
A delegate is just an object that another object sends messages to when certain things happen, so that the delegate can handle application-specific details the original object wasn't designed for. It's a way of customizing behavior without subclassing.
Some classes, for example NSSpeechSynthesizer, include delegate support. Unlike a protocol, failure to provide a delegate method does not provoke an error: the class always provides a method, but calls yours instead, if it exists.
For example, NSSpeechSynthesizer has a method
-(void) speechSynthesizer:(NSSpeechSynthesizer*)sender
didFinishSpeaking:(BOOL)complete;
If you provide an identically declared method, in class Fred, it will be called instead of the synthesiser's own method, provided you have earlier done, in that class,
speech = [[NSSpeechSynthesizer alloc] initWithVoice:#"com.apple.speech.synthesis.voice.Albert"];
[speech setDelegate:self];
This will work, though the compiler will warn if you did not announce yourself as a delegate by
#interface Fred : NSObject <NSSpeechSynthesizerDelegate>, in that
{
. . .
(This example is adapted from Cocoa Programming... by Hillegass).

Resources