Simple objective-c GET request

后端 未结 4 1592
栀梦
栀梦 2020-12-13 04:17

Most of the information here refers to the abandoned ASIHTTPREQUEST project so forgive me for asking again.

Effectively, I need to swipe a magnetic strip and send th

4条回答
  •  执笔经年
    2020-12-13 04:50

    Tested 100% working

    Only for Objective C

    -(void)fetchData
    {
    
        NSURLSessionConfiguration *defaultSessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
        NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration:defaultSessionConfiguration];
    
        // Setup the request with URL
        NSURL *url = [NSURL URLWithString:@"https://test.orgorg.net/ios/getStory.php?"];
        NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
    
        // Convert POST string parameters to data using UTF8 Encoding
        NSString *postParams = @"";
        NSData *postData = [postParams dataUsingEncoding:NSUTF8StringEncoding];
    
        // Convert POST string parameters to data using UTF8 Encoding
        [urlRequest setHTTPMethod:@"POST"];
        [urlRequest setHTTPBody:postData];
    
        // Create dataTask
        NSURLSessionDataTask *dataTask = [defaultSession dataTaskWithRequest:urlRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    
            NSDictionary *results = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
            //JSON Parsing....
            NSString *message = results[@"Message"];
            BOOL status = results[@"Status"];
            if (status){
               // Here you go for data....
    
            }else{
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"App"
                                                                               message:message
                                                                        preferredStyle:UIAlertControllerStyleAlert]; // 1
                UIAlertAction *firstAction = [UIAlertAction actionWithTitle:@"Ok"
                                                                      style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
                                                                          NSLog(@"You pressed button one");
                                                                      }]; // 2
    
    
                [alert addAction:firstAction]; // 4
    
                [self presentViewController:alert animated:YES completion:nil];
            }
    
    
    
        }];
    
        // Fire the request
        [dataTask resume];
    }
    

提交回复
热议问题