FLEXUIAppShortcuts.m 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. //
  2. // FLEXUIAppShortcuts.m
  3. // FLEX
  4. //
  5. // Created by Tanner on 5/25/20.
  6. // Copyright © 2020 FLEX Team. All rights reserved.
  7. //
  8. #import "FLEXUIAppShortcuts.h"
  9. #import "FLEXRuntimeUtility.h"
  10. #import "FLEXShortcut.h"
  11. #import "FLEXAlert.h"
  12. @implementation FLEXUIAppShortcuts
  13. #pragma mark - Overrides
  14. + (instancetype)forObject:(UIApplication *)application {
  15. return [self forObject:application additionalRows:@[
  16. [FLEXActionShortcut title:@"Open URL…"
  17. subtitle:^NSString *(UIViewController *controller) {
  18. return nil;
  19. }
  20. selectionHandler:^void(UIViewController *host, UIApplication *app) {
  21. [FLEXAlert makeAlert:^(FLEXAlert *make) {
  22. make.title(@"Open URL");
  23. make.message(
  24. @"This will call openURL: or openURL:options:completion: "
  25. "with the string below. 'Open if Universal' will only open "
  26. "the URL if it is a registered Universal Link."
  27. );
  28. make.textField(@"twitter://user?id=12345");
  29. make.button(@"Open").handler(^(NSArray<NSString *> *strings) {
  30. [self openURL:strings[0] inApp:app onlyIfUniveral:NO host:host];
  31. });
  32. make.button(@"Open if Universal").handler(^(NSArray<NSString *> *strings) {
  33. [self openURL:strings[0] inApp:app onlyIfUniveral:YES host:host];
  34. });
  35. make.button(@"Cancel").cancelStyle();
  36. } showFrom:host];
  37. }
  38. accessoryType:^UITableViewCellAccessoryType(UIViewController *controller) {
  39. return UITableViewCellAccessoryDisclosureIndicator;
  40. }
  41. ]
  42. ]];
  43. }
  44. + (void)openURL:(NSString *)urlString
  45. inApp:(UIApplication *)app
  46. onlyIfUniveral:(BOOL)universalOnly
  47. host:(UIViewController *)host {
  48. NSURL *url = [NSURL URLWithString:urlString];
  49. if (url) {
  50. if (@available(iOS 10, *)) {
  51. [app openURL:url options:@{
  52. UIApplicationOpenURLOptionUniversalLinksOnly: @(universalOnly)
  53. } completionHandler:^(BOOL success) {
  54. if (!success) {
  55. [FLEXAlert showAlert:@"No Universal Link Handler"
  56. message:@"No installed application is registered to handle this link."
  57. from:host
  58. ];
  59. }
  60. }];
  61. } else {
  62. [app openURL:url];
  63. }
  64. } else {
  65. [FLEXAlert showAlert:@"Error" message:@"Invalid URL" from:host];
  66. }
  67. }
  68. @end