Post

Replies

Boosts

Views

Activity

How to get input string from UIAlertController in a synchronous method
Hi, My methodA is synchronous and be called in main thread. methodA gets input strings from methodB via poping a UIAlertController with UITextField. I have two versions of codes as below. On some iphones with iOS versions, both two versions codes works, such as iPhone SE2 with iOS 17.7. But on some iphones with iOS versions, the two versions of codes can't work, such as iPhone XR with iOS 18.0.1 or iPhone 14 plus with iOS 13.6. How can I get the input string from UIAlertController in the synchronous method on all iPhones and iOS versions. /* //version 1. -(NSString *)methodA { // Prepare a local variable to capture the input __block NSString *inputString = nil; // Call methodB to display the UIAlertController modally [self methodBWithCompletion:^(NSString *result) { inputString = result; // Stop the run loop when input is received CFRunLoopStop(CFRunLoopGetCurrent()); }]; // Run the run loop manually to block execution until CFRunLoopStop() is called CFRunLoopRun(); // Once the run loop stops, return the user input return inputString; } -(void)methodBWithCompletion:(void (^)(NSString *))completion { // Create the UIAlertController for user input UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Enter PIN" message:nil preferredStyle:UIAlertControllerStyleAlert]; [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { textField.placeholder = @"PIN"; textField.secureTextEntry = YES; }]; UIAlertAction *submitAction = [UIAlertAction actionWithTitle:@"Submit" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { // Get the text from the UITextField UITextField *textField = alertController.textFields.firstObject; NSString *input = textField.text; // Pass the input back via the completion handler if (completion) { completion(input); } }]; [alertController addAction:submitAction]; // Present the alert modally on the main window's rootViewController [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertController animated:YES completion:nil]; } */ /* //version 2. -(NSString *)methodA { // Prepare a variable to hold user input __block NSString *inputString = nil; NSLog(@"methodA:...."); // Call methodB to display the UIAlertController asynchronously [self methodBWithCompletion:^(NSString *result) { inputString = result; }]; // Run the main run loop until the inputString is not nil (user input provided) while (inputString == nil) { // Run the loop for a short time interval, allowing the UI to remain responsive [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]]; } // Return the input string once the user has submitted the input return inputString; } -(void)methodBWithCompletion:(void (^)(NSString *))completion { // Create and configure the UIAlertController UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Enter PIN" message:nil preferredStyle:UIAlertControllerStyleAlert]; [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { textField.placeholder = @"PIN"; textField.secureTextEntry = YES; }]; UIAlertAction *submitAction = [UIAlertAction actionWithTitle:@"Submit" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { UITextField *textField = alertController.textFields.firstObject; NSString *input = textField.text; // Return the input via the completion handler if (completion) { completion(input); } }]; [alertController addAction:submitAction]; // Present the alert on the main thread [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertController animated:YES completion:nil]; } */
Topic: UI Frameworks SubTopic: UIKit
2
0
537
Oct ’24
Display certificate content like Keychain Access app does
Hi, I have a certificate, how can I display the certificate content in my Mac app just like Keychain Access app does. Can I popup the certificate content dialog just like Keychain Access app?
Replies
4
Boosts
0
Views
1.1k
Activity
Oct ’25
How does NFC tag for default app clip link in TestFlight
My NFC tag for default app clip link (https://appclip.apple.com/id?p= xxxxxx) can work when I distribute to App Store. But NFC tag fail when I want to test in TestFlight. I already set the title and the url (the default app clip link such as https://appclip.apple.com/id?p= xxxxxx )in the TestFlight. Why did this happen?
Replies
1
Boosts
0
Views
160
Activity
Apr ’25
How to get input string from UIAlertController in a synchronous method
Hi, My methodA is synchronous and be called in main thread. methodA gets input strings from methodB via poping a UIAlertController with UITextField. I have two versions of codes as below. On some iphones with iOS versions, both two versions codes works, such as iPhone SE2 with iOS 17.7. But on some iphones with iOS versions, the two versions of codes can't work, such as iPhone XR with iOS 18.0.1 or iPhone 14 plus with iOS 13.6. How can I get the input string from UIAlertController in the synchronous method on all iPhones and iOS versions. /* //version 1. -(NSString *)methodA { // Prepare a local variable to capture the input __block NSString *inputString = nil; // Call methodB to display the UIAlertController modally [self methodBWithCompletion:^(NSString *result) { inputString = result; // Stop the run loop when input is received CFRunLoopStop(CFRunLoopGetCurrent()); }]; // Run the run loop manually to block execution until CFRunLoopStop() is called CFRunLoopRun(); // Once the run loop stops, return the user input return inputString; } -(void)methodBWithCompletion:(void (^)(NSString *))completion { // Create the UIAlertController for user input UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Enter PIN" message:nil preferredStyle:UIAlertControllerStyleAlert]; [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { textField.placeholder = @"PIN"; textField.secureTextEntry = YES; }]; UIAlertAction *submitAction = [UIAlertAction actionWithTitle:@"Submit" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { // Get the text from the UITextField UITextField *textField = alertController.textFields.firstObject; NSString *input = textField.text; // Pass the input back via the completion handler if (completion) { completion(input); } }]; [alertController addAction:submitAction]; // Present the alert modally on the main window's rootViewController [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertController animated:YES completion:nil]; } */ /* //version 2. -(NSString *)methodA { // Prepare a variable to hold user input __block NSString *inputString = nil; NSLog(@"methodA:...."); // Call methodB to display the UIAlertController asynchronously [self methodBWithCompletion:^(NSString *result) { inputString = result; }]; // Run the main run loop until the inputString is not nil (user input provided) while (inputString == nil) { // Run the loop for a short time interval, allowing the UI to remain responsive [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]]; } // Return the input string once the user has submitted the input return inputString; } -(void)methodBWithCompletion:(void (^)(NSString *))completion { // Create and configure the UIAlertController UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Enter PIN" message:nil preferredStyle:UIAlertControllerStyleAlert]; [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { textField.placeholder = @"PIN"; textField.secureTextEntry = YES; }]; UIAlertAction *submitAction = [UIAlertAction actionWithTitle:@"Submit" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { UITextField *textField = alertController.textFields.firstObject; NSString *input = textField.text; // Return the input via the completion handler if (completion) { completion(input); } }]; [alertController addAction:submitAction]; // Present the alert on the main thread [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertController animated:YES completion:nil]; } */
Topic: UI Frameworks SubTopic: UIKit
Replies
2
Boosts
0
Views
537
Activity
Oct ’24