gpt4 book ai didi

iOS:connectionDidFinishLoading

转载 作者:行者123 更新时间:2023-11-28 22:54:58 28 4
gpt4 key购买 nike

我需要一些帮助。我正在从另一个类调用登录函数,

// Login on server
- (BOOL) login:(NSString*) username password:(NSString*)password
{
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:subscribedAppsURL]];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connectionDict setObject:connection forKey:@"login"];
[connection release];
return true;
}

// delegate
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"Finished Loading");

if (connection == [connectionDict objectForKey:@"login"]) {
[connection release];
//@TODO Here I want to function login to return true.
}

}

在 connectionDidFinishLoading 结束时,我想在函数登录中返回 TRUE/FALSE 值。有人有什么建议吗?谢谢!

最佳答案

您可以像这样同步发送您的请求:

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:subscribedAppsURL]];
NSURLResponse *response = nil;
NSError *error = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

if (error != nil)
{
NSString *stringResponse = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"Reponse:%@", response);

//Handle the response, possible just return true here:
}
else
{
NSLog(@"Error:%@", error.localizedDescription);
}

为了使用委托(delegate):

//In Header
@protocol LoginCompletionDelegate
-(void) didCompleteAndIsLoggedIn:(BOOL) loggedIn;
@end

@property (nonatomic, assign) id<LoginCompletionDelegate> delegate;


//In implementation
- (BOOL) loginWithDelegate:(id<LoginCompletionDelegate>)target username:(NSString*) username password:(NSString*)password
{
delegate = target;
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:subscribedAppsURL]];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connectionDict setObject:connection forKey:@"login"];
[connection release];
return true;
}

// delegate
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"Finished Loading");

if (connection == [connectionDict objectForKey:@"login"]) {
[connection release];
//@TODO Here I want to function login to return true.
[delegate didCompleteAndIsLoggedIn:YES];
}

}

//There is another method that looks like this. I might have the signature a bit wrong
-(void) connection:(NSURLConnection*) connection didFailWithError:(NSError*) error
{
[delegate didCompleteAndIsLoggedIn:NO];
}

关于iOS:connectionDidFinishLoading,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11014528/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com