Google URL Shortener

Acortar URLs.

  • Petición: https://www.googleapis.com/urlshortener/v1/url?key=API_KEY
  • Parámetro longURL con la URL a acortar

JSON obtenido:

{
    id = "https://goo.gl/C5ZWB2";
    kind = "urlshortener#url";
    longUrl = "https://github.com/copanonga";
}

Petición:

-(void)getURLShortener {
    
    // URL: https://www.googleapis.com/urlshortener/v1/url?key=API_KEY
    
    NSString *URL_BASE = [NSString stringWithFormat:@"https://www.googleapis.com/urlshortener/v1/url?key="];
    NSString *API_KEY = [NSString stringWithFormat:@"API_KEY"];
    NSString *URL = [NSString stringWithFormat:@"%@%@",URL_BASE, API_KEY];
    
    NSString *longURL = [NSString stringWithFormat:@"https://github.com/copanonga"];
    
    NSArray *campos = [NSArray arrayWithObjects:@"longUrl",nil];
    NSArray *valores = [NSArray arrayWithObjects:longURL, nil];
    NSDictionary *parametros = [NSDictionary dictionaryWithObjects:valores forKeys:campos];
    
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    
    [manager POST:URL parameters:parametros progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSError* error;
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:responseObject
                                                           options:0
                                                             error:&error];
        
        NSDictionary* json = [NSJSONSerialization
                              JSONObjectWithData:jsonData
                              options:kNilOptions
                              error:&error];
        
        NSLog(@"JSON: %@", json);
        NSLog(@"Short URL: %@", [json objectForKey:@"id"]);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"Error getURLShortener: %@", error);
    }];
    
}

Código completo: https://github.com/copanonga/GoogleURLShortener

Imagen en escala de grises

- (UIImage *)grayscaleImage:(UIImage *)image {

	CIImage *ciImage = [[CIImage alloc] initWithImage:image];
	CIImage *grayscale = [ciImage imageByApplyingFilter:@"CIColorControls"
									withInputParameters: @{kCIInputSaturationKey : @0.0}];
	return [UIImage imageWithCIImage:grayscale];

}

NSSortDescriptor ordenar con número un string

NSArray *sortedArray;
sortedArray = [listado sortedArrayUsingComparator:(NSComparator)^(id a, id b) {
Modelo *modelo1 = a;
Modelo *modelo2 = b;
NSNumber *num1 = [NSNumber numberWithInt:[modelo1.stringAComparar intValue]];
NSNumber *num2 = [NSNumber numberWithInt:[modelo2.stringAComparar intValue]];

return [num2 compare:num1];
}];
listado = [sortedArray mutableCopy];

UIAlertController: style action sheet

UIAlertController *alertController = [UIAlertController
											  alertControllerWithTitle:nil
											  message:nil
											  preferredStyle:UIAlertControllerStyleActionSheet];
		
		UIAlertAction *cancelar = [UIAlertAction
									   actionWithTitle:NSLocalizedString(@"Cancelar", nil)
									   style:UIAlertActionStyleCancel
									   handler:^(UIAlertAction *action)
									   {
										   NSLog(@"Cancel action");
									   }];
		
		UIAlertAction *accion = [UIAlertAction
								 actionWithTitle:NSLocalizedString(@"Acción", nil)
								 style:UIAlertActionStyleDefault
								 handler:^(UIAlertAction *action)
								 {
									
								 }];
		
		NSMutableAttributedString *textoTitulo = [[NSMutableAttributedString alloc] initWithString:NSLocalizedString(@"Título...", nil)];
		[textoTitulo addAttribute:NSFontAttributeName
					  value:[UIFont fontWithName:NSLocalizedString(@"tipoLetra", nil) size:19.0]
					  range:NSMakeRange(0, textoTitulo.length)];
		[alertController setValue:textoTitulo forKey:@"attributedTitle"];
		
		[alertController addAction:cancelar];
		[alertController accion];
		
		[self presentViewController:alertController animated:YES completion:nil];

UIAlertController: style alert

-(void)mostrarAviso {
    
    UIAlertController * alert=   [UIAlertController
                                  alertControllerWithTitle:NSLocalizedString(@"Título", nil)
                                  message:NSLocalizedString(@"Mensaje", nil)
                                  preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction* okButton = [UIAlertAction
                                actionWithTitle:NSLocalizedString(@"Ok", nil)
                                style:UIAlertActionStyleDefault
                                handler:^(UIAlertAction * action)
                                {
                                    
                                    [alert dismissViewControllerAnimated:YES completion:nil];
                                    
                                }];
    [alert addAction:okButton];
    
    [self presentViewController:alert animated:YES completion:nil];
    
}