iOS Question Disable automatic redirections with HttpUtils2

Star-Dust

Expert
Licensed User
I would like to disable automatic redirection with HttpUtils.

For Android there is this method, but I need it in B4i
 
Last edited:
Solution
Add this code to any of the modules:
B4X:
#if OBJC
@end
@interface B4IHttp (redirect)
@end
@implementation B4IHttp (redirect)

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
                     willPerformHTTPRedirection:(NSHTTPURLResponse *)response
                                     newRequest:(NSURLRequest *)request
                              completionHandler:(void (^)(NSURLRequest * _Nullable))completionHandler {
    NSLog(@"disabling redirect...");
    completionHandler(nil);
}
#End If

Semen Matusovskiy

Well-Known Member
Licensed User
As I understand, theoretically you need a method wiiPerformHTTPRedirection of NSURLSession delegate. But:
1) B4i sets own delegate and to expand it without source code is a problem.
2) As I understand, if server returns 200 instead of 307, wiiPerformHTTPRedirection does not work,

Why HttpUtils exactly ? For example, to control WKWebView is enough simple (navigationDelegate, decidePolicyForNavigationAction:...)
 
Upvote 0

Star-Dust

Expert
Licensed User
I use HttpUtils2 because I have created an alternative Multiplatform client to a webApp.

I need to read the server responses to populate the masks and respond to the server.
Sometimes the server responds with page 302 and redirects me. I need to know when it happens and manage it. At the moment httpUtils does it all by itself.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Add this code to any of the modules:
B4X:
#if OBJC
@end
@interface B4IHttp (redirect)
@end
@implementation B4IHttp (redirect)

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
                     willPerformHTTPRedirection:(NSHTTPURLResponse *)response
                                     newRequest:(NSURLRequest *)request
                              completionHandler:(void (^)(NSURLRequest * _Nullable))completionHandler {
    NSLog(@"disabling redirect...");
    completionHandler(nil);
}
#End If
 
Upvote 1
Solution

Star-Dust

Expert
Licensed User
Thank you very much Erel, it was very important to me
 
Upvote 0
Top