본문으로 바로가기


/*===================================

 접속포트 조회 START

 ===================================*/

-(void) getPort:(NSString *)shopCode

{

    

    //전송방식 선택

    NSString *method = @"POST";

    

    

    //값이 존재하면

    if(shopCode){

        

        if([method isEqualToString:@"GET"]){

            

            //get 방식일때

            //[NSURL URLWithString:[NSString stringWithFormat:@"%@/proc/gameport.asp?param1=%@",SERVER_URL,shopCode]];

            NSString *getURL = [NSString stringWithFormat:@"%@/proc/gameport.asp?param1=%@",SERVER_URL,shopCode];

            

            NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

            [request setURL:[NSURL URLWithString:getURL]];

            [request setHTTPMethod:@"GET"];

            

            NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];

            

            [[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

                

                if (data!=nil){

      

                    rtnPort = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];

                    NSLog(@"portStr : %@", rtnPort);

                    

                    /*

                     포트 체크 후 서버에 접속 실행 START

                     */

                    if(rtnPort){

                        if([rtnPort isEqualToString:@"0"]){

                            [self alertViewPlay:@"QR 로그인" Msg:@"유효한 매장이 아닙니다." noBtn:NO Close:YES];

                            

                        }else if([rtnPort isEqualToString:@"1"]){

                            [self alertViewPlay:@"QR 로그인" Msg:@"등록된 포트 정보가 없습니다." noBtn:NO Close:YES];

                            

                        }else{

                            //서버연결 시도 (스레드)

                            NSLog(@"서버연결 시도 포트 : %@", rtnPort);

                            dispatch_async(dispatch_get_main_queue(), ^{

                                [self connectToServer:rtnPort];

                            });

                            

                        }

                    }else{

                        [self alertViewPlay:@"QR 로그인" Msg:@"포트 조회가 정상적이지 않습니다." noBtn:NO Close:YES];

                        

                    }

                    /*

                     포트 체크 후 서버에 접속 실행 END

                     */

                    

                    /*

                    NSDictionary* json = [NSJSONSerialization

                                          JSONObjectWithData:data

                                          options:kNilOptions

                                          error:&error];

                    

                    //                    for (NSDictionary *project in json[@"projects"]) {

                    //                        NSLog(@"Project name: %@", project[@"name"]);

                    //                    }

                    

                    NSLog(@"username %@",  [json objectForKey:@"username"]);

                    NSLog(@"password %@", [json objectForKey:@"password"]);

                    */

                    

                }else{

                    NSLog(@"error");

                }

                

            }] resume];

            

        }else{

            //전송방식이 post일때~!

            

            // 통신을 Request(요청)하는 객체를 만든다

            NSMutableURLRequest* urlRequest = [[NSMutableURLRequest alloc] init];

            

            // 기본 구성에 URLSession 생성

            NSURLSessionConfiguration *defaultSessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];

            NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration:defaultSessionConfiguration];

            

            // UTF8 인코딩을 사용하여 POST 문자열 매개 변수를 데이터로 변환

            NSString *post = [NSString stringWithFormat:@"param1=%@", shopCode];

            NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];

            NSString *postLength = [NSString stringWithFormat:@"%d", (int)[postData length]];


            // 셋팅

            [urlRequest setURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/proc/gameport.asp", SERVER_URL]]];

            [urlRequest setHTTPMethod:@"POST"];

            [urlRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];

            //[urlRequest setValue:@"Mozilla/4.0 (compatible;)" forHTTPHeaderField:@"User-Agent"];

            [urlRequest setHTTPBody:postData];

            [urlRequest setTimeoutInterval:30.0];

            

            

            // dataTask 생성

            NSURLSessionDataTask *dataTask = [defaultSession dataTaskWithRequest:urlRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

                

                if (data!=nil){

                    

                    rtnPort = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];

                    NSLog(@"portStr : %@", rtnPort);

                    

                    /*

                     포트 체크 후 서버에 접속 실행 START

                     */

                    if(rtnPort){

                        if([rtnPort isEqualToString:@"0"]){

                            [self alertViewPlay:@"QR 로그인" Msg:@"유효한 매장이 아닙니다." noBtn:NO Close:YES];

                            

                        }else if([rtnPort isEqualToString:@"1"]){

                            [self alertViewPlay:@"QR 로그인" Msg:@"등록된 포트 정보가 없습니다." noBtn:NO Close:YES];

                            

                        }else{

                            //서버연결 시도 (스레드)

                            NSLog(@"서버연결 시도 포트 : %@", rtnPort);

                            dispatch_async(dispatch_get_main_queue(), ^{

                                [self connectToServer:rtnPort];

                            });

                            

                        }

                    }else{

                        [self alertViewPlay:@"QR 로그인" Msg:@"포트 조회가 정상적이지 않습니다." noBtn:NO Close:YES];

                        

                    }

                    /*

                     포트 체크 후 서버에 접속 실행 END

                     */

                    

                    

                    /*

                    NSDictionary* json = [NSJSONSerialization

                                          JSONObjectWithData:data

                                          options:kNilOptions

                                          error:&error];

                    

                    //                    for (NSDictionary *project in json[@"projects"]) {

                    //                        NSLog(@"Project name: %@", project[@"name"]);

                    //                    }

                    

                    NSLog(@"username %@",  [json objectForKey:@"username"]);

                    NSLog(@"password %@", [json objectForKey:@"password"]);

                    */

                    

                }else{

                    NSLog(@"error");

                }

                

            }];

            

            

            // request 종료

            [dataTask resume];

            

            

        }

    }else{

        NSLog(@"매장코드 오류");

    }

    

}

/*===================================

 접속포트 조회 END

 ===================================*/