I would like to call a webService from my MainViewController.m which is located in my DataManagement.m class. What I do is create a singleton and launch it with this line:
[[DataManagement sharedManager] callWebService:url withBytes:bytes];
It works properly. The connectionDidFinishLoading is called in that case.
But if I want to check Internet with another Reachability.m class (the callback block returns the error as nil. So it means that there is an Internet connection). When I then launch the same code, the connectionDidFinishLoading method is never called. Do I have to use a delegate, or something else?
DataManagement.m
+ (instancetype) sharedManager
{
static DataManagement *sharedManager = nil;
if (sharedManager == nil)
{
sharedManager = [[self alloc] init];
}
return sharedManager;
}
- (void)callWebService:(NSString *)url withBytes:(NSString *) bytes
{
NSMutableURLRequest* request = [[NSMutableURLRequest alloc] init];
NSString *url_string = [bytes stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
[request setURL:[NSURL URLWithString:[url stringByAppendingString: url_string]]];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setTimeoutInterval:timeOut];
[NSURLConnection connectionWithRequest:request delegate:self];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
...
}
MainViewController.m
Doesn't work:
-(void)viewDidLoad
{
...
[Reachability checkInternetConnectivityWithUrl: url_server WithSuccessCompletion:^(NSError *error) {
if (error == nil)
{
[[DataManagement sharedManager] callWebService:url withBytes:bytes];
}
else
{
NSLog(@"No internet connection");
}
}];
...
}
Works:
-(void)viewDidLoad
{
...
[[DataManagement sharedManager] callWebService:url withBytes:bytes];
...
}
your using correct only for checking internet connection, but its connected or not check using this function. It will work fine.