Show SVProgressHUD on ViewController? - ios

I used the web Service in AppDelegate. But i want to show the SVProgressHUD on the initial view controller (View Controller), And when the Web Service completed then hide the SVProgressHUD in View Controller.

There is a static method on SVProgressHUD called
+ (void)setContainerView:(UIView*)containerView;
[SVProgressHUD setContainerView:yourViewController.view];

I understand your demand, my suggestion is to package two
class,HUD and Mysevers:
I have token example here:
HUD.m:
#import "HUD.h"
#import <UIKit/UIKit.h>
#import "MBProgressHUD.h"
#implementation HUD
+(void)addHUD
{
MBProgressHUD* HUD = [[MBProgressHUD alloc]initWithFrame:CGRectMake(0, 0, IPHONE_WIDTH, IPHONE_HEIGHT)];
HUD.tag = 5000;
HUD.labelText = #"正在加载";
[WINDOW addSubview:HUD];
[HUD show:YES];
}
+(void)removeHUD
{
MBProgressHUD* HUD = (MBProgressHUD*)[WINDOW viewWithTag:5000];
[HUD removeFromSuperview];
HUD = nil;
}
#end
Mysevers.m:
#import "Mysevers.h"
#import "AFNetworking.h"
#import "HUD.h"
#implementation Mysevers
+(void)AFPOSTAddressname:(NSString*)addressName parmas:(NSDictionary*)parmas RequestSuccess:(void(^)(id result))success failBlcok:(void(^)(void))failBlcok{
[HUD addHUD];
AFHTTPSessionManager *requestManager = [AFHTTPSessionManager manager];
NSString *urlStr = [NSString stringWithFormat:#"%#%#",BASE_URL,addressName];
NSLog(#"%#",urlStr);
/*[requestManager POST:urlStr parameters:parmas success:^(NSURLSessionTask *task, id responseObject) {
[HUD removeHUD];
success(responseObject);
} failure:^(NSURLSessionTask *operation, NSError *error) {
NSLog(#"error==%#",[error localizedDescription]);
[HUD removeHUD];
failBlcok();
}];*/
[requestManager POST:urlStr parameters:parmas progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
[HUD removeHUD];
success(responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (error != nil) {
NSLog(#"error==%#",[error localizedDescription]);
[HUD removeHUD];
failBlcok();
}
return ;
}];
}
+(void)AFGETAddressname:(NSString*)addressName parmas:(NSDictionary*)parmas RequestSuccess:(void(^)(id result))success failBlcok:(void(^)(void))failBlcok{
[HUD addHUD];
AFHTTPSessionManager *requestManager = [AFHTTPSessionManager manager];
NSString *urlStr = [NSString stringWithFormat:#"%#%#",BASE_URL,addressName];
NSLog(#"%#",urlStr);
/*[requestManager GET:urlStr parameters:parmas success:^(NSURLSessionTask *task, id responseObject) {
[HUD removeHUD];
success(responseObject);
} failure:^(NSURLSessionTask *operation, NSError *error) {
NSLog(#"error==%#",[error localizedDescription]);
[HUD removeHUD];
failBlcok();
}];*/
[requestManager GET:urlStr parameters:parmas progress:^(NSProgress * _Nonnull downloadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
[HUD removeHUD];
success(responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(#"error==%#",[error localizedDescription]);
[HUD removeHUD];
failBlcok();
}];
}
#end
USAGE
You can have a test in your project:
[Mysevers AFPOSTAddressname:url parmas:params RequestSuccess:^(id result) {
// code here
}
Hope this can help you, this is my experience.And if you want to use SVProgressHUD, you can also replace the MBProgressHUD.

Related

How to upload video using AFNetworking on Server

I,m trying to upload video using afnetworking and my code is below in response it return nil url please let me know if you want any other detail . i don,t know where is my mistake.
{
// add hud to show sending image
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
NSString *format;
format=#"video/mp4";
myDict1 = #{#"user_id”:””,
#"timezone”:””,
#"friend_id”:””,
#"message_type”:””,
#"message":"",
#"language_id”:””
};
AFHTTPSessionManager *manager=[AFHTTPSessionManager manager];
NSString* webService=[NSString stringWithFormat:#"%#/send_messages",WEB_SERVICE_URL_BETA];
[manager POST:webService parameters:myDict1 constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData)
{
[formData appendPartWithFileData:picturedata name:#"file" fileName:#"testvideo.mov" mimeType:#"video/quickTime"];
} progress:nil success:^(NSURLSessionDataTask * Nonnull task, id Nullable responseObject) {
if ([[NSString stringWithFormat:#"%#",[responseObject valueForKey: #"status"] ] isEqualToString:#"1"])
{
}
else
{
}
}
failure:^(NSURLSessionDataTask _Nullable task, NSError _Nonnull error) {
NSLog(#"Failure %#",error);
}];
}

How to pass Swift Bool value to Objective-C's method?

I have a Mysevers Class for network written by Objective-c, and now I use mixed Objective-c and Swift project.
In my Mysevers, I have a method to network:
+(void)AFPOSTWithHud:(BOOL)hud andAddressname:(NSString*)addressName parmas:(NSDictionary*)parmas RequestSuccess:(void(^)(id result))success failBlcok:(void(^)(void))failBlcok
{
if (hud) {
[HUD addHUD];
}
AFHTTPSessionManager *requestManager = [AFHTTPSessionManager manager];
NSString *urlStr = [NSString stringWithFormat:#"%#%#",BASE_URL,addressName];
NSLog(#"%#",urlStr);
[requestManager POST:urlStr parameters:parmas progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if (hud) {
[HUD removeHUD];
}
success(responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (error != nil) {
NSLog(#"error==%#",[error localizedDescription]);
if (hud) {
[HUD removeHUD];
}
}
failBlcok();
}];
}
In the Swift controller, invoke the method, pass a swift Bool value false.
Mysevers.afpost(withHud: false, andAddressname: Global.url_fillMoreInfo, parmas: uploadDict, requestSuccess:{ (result) in
But, in the Mysevers class, I give a breakpoint to check the passed value, it turn out to be YES:
(lldb) po hud
YES

Show alert message to the desired VC from method in another VC

I implemented this in my GatewayManager.m
+ (void)GetAPIName:(NSString *)APIName
withParameter:(NSMutableDictionary *)parameter
returnManager:(void(^)(AFHTTPSessionManager *manager))returnManager
progress:(void(^)(NSProgress *uploadProgress))progress
success:(void(^)(NSDictionary *result))success
error:(void(^)(NSError *error))failure {
if([ShareFunction isInternetConnecting] && [ShareFunction isVPNConnected]) {
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
NSString *urlString = [NSString stringWithFormat:#"%#%#", kServerDomain, APIName];
if (![APIName isEqualToString:kAccountLogin]) {
[parameter setObject:[UserManager shareInstance].token forKey:#"accessToken"];
}
[manager GET:urlString
parameters:parameter
progress:^(NSProgress * _Nonnull downloadProgress) {
progress(downloadProgress);
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(#"[API] %# Success : %#",APIName, responseObject);
success(responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(#"[API] %# Error : %#",APIName, error);
failure(error);
//NSLog(#"[API] %# Error : %#",APIName, error);
}];
} else {
[ShareFunction showSimpleAlertWithString:#"Internet/VPN no connect" inVC:[UIApplication sharedApplication].keyWindow.rootViewController];
failure(nil);
}
}
I have a problem with this line
[ShareFunction showSimpleAlertWithString:#"Internet/VPN no connect" inVC:[UIApplication sharedApplication].keyWindow.rootViewController];
If it's root VC who calls this method, there's no problem to show the alert message on that VC. But if it's not root VC, how can I show the alert message on that VC?
Just replace this
[ShareFunction showSimpleAlertWithString:#"Internet/VPN no connect" inVC:[UIApplication sharedApplication].keyWindow.rootViewController];
with
[ShareFunction showSimpleAlertWithString:#"Internet/VPN no connect"];

How can I write completion block with nullable?

When I call this method with nil, the app crashes, but I want to know how to write it with nullable.
CRASH
[KPTaxnoteApiSaveHandler saveEntryWithUuid:uuid completion:nil];
OK
[KPTaxnoteApiSaveHandler saveEntryWithUuid:uuid completion:^(NSError *error) {}];
This is the code.
+ (void)saveEntryWithUuid:(NSString *)uuid completion:(void (^ __nullable)(NSError * _Nullable error))completion {
NSLog(#"saveEntryWithUuid");
Entry *entry = [Entry MR_findFirstByAttribute:#"uuid" withValue:uuid];
NSDictionary *params = #{#"entry[uuid]":entry.uuid};
[KPTaxnoteApiSaveHandler postWithUrl:kApiUrlStringForEntry params:params completion:^(NSError *error) {
if (!error) {
[MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) {
Entry *entry = [Entry MR_findFirstByAttribute:#"uuid" withValue:uuid inContext:localContext];
entry.needSave = #NO;
}];
}
completion(error);
}];
+ (void)postWithUrl:(NSString *)urlStr params:(NSDictionary *)params completion:(nullable void (^)(NSError *_Nullable error))completion {
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:urlStr parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
completion(nil);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
completion(error);
}];
Where is the crash happening? My first guess is you need to do something like this:
if (completion) {
completion(nil); // Or completion(error);
}
This will handle the case where the completion is nil.

why return value is null , when the return type is null in iOS, objectiveC

I'm getting data from webservice.this is my code
- (NSArray *)setupConnection
{
airportArray = nil;
NSString *airportCode = [NSString stringWithFormat:#"somevalue"];
NSString *authenticationCode = [NSString stringWithFormat:#"somenumber"];
NSString *baseurl = [NSString stringWithFormat:#"someurl",authenticationCode,airportCode];
// NSString *mainurlString = [NSString stringWithFormat:#""];
// NSURL *mainurl = [NSURL URLWithString:mainurlString];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager GET:baseurl parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSArray *mainArray = (NSArray *)responseObject;
airportArray = [[NSMutableArray alloc] init];
for (NSDictionary *all in mainArray) {
airports = [all objectForKey:#"something"];
[airportArray addObject:airports];
NSLog(#"%#", airports);//**this prints the value**
}
//NSLog(#"%#", responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
UIAlertController *mainAlert = [UIAlertController alertControllerWithTitle:#"Something Wrong!" message:[error localizedDescription] preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:mainAlert animated:YES completion:nil];
}];
return airportArray;//**this returns null**
}
I checked with this, but it is null
- (void)printap
{
NSArray *chk = [self setupConnection];
NSLog(#"CHK :%#", chk);
}
when print it prints the value. but when I return it is null.why is that.help me with this
Hey you need to use callback block.
Below is a example for your provided code.
In header file
typedef void(^FailureBlock)(NSError *error);
typedef void (^SuccessBlock)(NSArray *responseArray);
In implementation file
#implementation BSANetworkController {
FailureBlock _failureBlock;
SuccessBlock _successBlock;
}
- (void)setupConnectionWithsuccess:(SuccessBlock)success failure:(FailureBlock)failure;
//your code...
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager GET:baseurl parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSArray *mainArray = (NSArray *)responseObject;
airportArray = [[NSMutableArray alloc] init];
for (NSDictionary *all in mainArray) {
airports = [all objectForKey:#"something"];
[airportArray addObject:airports];
NSLog(#"%#", airports);//**this prints the value**
}
if (_successBlock) {
_successBlock(airports); //Change the type as per your need
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (_failureBlock) {
_failureBlock(error);
}
}];
}
- (void)printap
{
[self setupConnectionWithsuccess:^(NSArray *array) {
NSLog(#"CHK :%#", array);
} failure:^(NSError *error) {
UIAlertController *mainAlert = [UIAlertController alertControllerWithTitle:#"Something Wrong!" message:[error localizedDescription] preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:mainAlert animated:YES completion:nil];
}
}];
}
Read about it in below links.
link1
link2
link3
Google search

Resources