gpt4 book ai didi

ios - 处理 NSURLConnection 错误的最佳方法 - iPhone 开发

转载 作者:行者123 更新时间:2023-11-28 19:53:21 25 4
gpt4 key购买 nike

嘿,我有一个正在处理的应用程序,我经常会收到以下消息:

 NSURLConnection error: Error Domain=NSURLErrorDomain Code=-1005 
"The network connection was lost."

我的日志将记录此内容,然后我的应用程序将卡住。我的希望是,我最好的选择是什么,只是发送一个互联网丢失的通知,请重试而不是关闭我的应用程序?

这是我正在做的:

用户登录 -> 主 Controller 加载并运行:

// EXECUTE SERVER CALL
-(void)makeRequests
{
/* GRAB USERNAME TO BE SENT OVER FOR POPULATING DATA */
NSArray *get = [[SSKeychain allAccounts] init];
NSString *username = [get[0] objectForKey:@"acct"];
NSDictionary *dictionary = @{@"function": @"populateHomePage", @"username" : username};
NSError *error = nil;
NSData *data = [NSJSONSerialization dataWithJSONObject:dictionary options:0 error:&error];
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if (error)
NSLog(@"%s: JSON encode error: %@", __FUNCTION__, error);

NSURL *url = [NSURL URLWithString:@"/IOS-Frame/grab-data.php"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

[request setHTTPMethod:@"POST"];
NSString *params = [NSString stringWithFormat:@"json=%@",
[string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSData *paramsData = [params dataUsingEncoding:NSUTF8StringEncoding];
[request addValue:@"8bit" forHTTPHeaderField:@"Content-Transfer-Encoding"];
[request addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request addValue:[NSString stringWithFormat:@"%lu", (unsigned long)[paramsData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:paramsData];


// issue the request
NSURLResponse *response = nil;
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if (error)
NSLog(@"%s: NSURLConnection error: %@", __FUNCTION__, error);

NSString *responseString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"responseString: %@",responseString);
// GRAB STATUS OBJECT
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:returnData //1

options:kNilOptions
error:&error];
self.dataGrabed_dictionary = [json objectForKey:@"retrieved_data"];
}

最佳答案

按照 internet connection test 的答案进行操作并在队列中排序方法。

- (void)testInternetConnection
{
internetReachableFoo = [Reachability reachabilityWithHostname:@"www.google.com"];

// Internet is reachable
internetReachableFoo.reachableBlock = ^(Reachability*reach)
{
// Update the UI on the main thread
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"Yayyy, we have the interwebs!");

//show activity indicator........block ui until data loads.

//queue methods

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Save in the background

[self makerequests];

dispatch_async(dispatch_get_main_queue(), ^{
// Perform UI functions on the main thread!
//dismiss activity indicator
});
});

});
};

// Internet is not reachable
internetReachableFoo.unreachableBlock = ^(Reachability*reach)
{
// Update the UI on the main thread
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"Someone broke the internet :(");
//show some alert....

});
};

[internetReachableFoo startNotifier];
}

附言这是提示,不是确切答案。

关于ios - 处理 NSURLConnection 错误的最佳方法 - iPhone 开发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27929175/

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