didRegisterUserNotificationSettings.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #!/usr/bin/env node
  2. /*
  3. * Copyright (c) 2013-2015 by appPlant UG. All rights reserved.
  4. *
  5. * @APPPLANT_LICENSE_HEADER_START@
  6. *
  7. * This file contains Original Code and/or Modifications of Original Code
  8. * as defined in and that are subject to the Apache License
  9. * Version 2.0 (the 'License'). You may not use this file except in
  10. * compliance with the License. Please obtain a copy of the License at
  11. * http://opensource.org/licenses/Apache-2.0/ and read it before using this
  12. * file.
  13. *
  14. * The Original Code and all software distributed under the License are
  15. * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  16. * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  17. * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
  19. * Please see the License for the specific language governing rights and
  20. * limitations under the License.
  21. *
  22. * @APPPLANT_LICENSE_HEADER_END@
  23. */
  24. // Hook inserts the UIApplicationRegisterUserNotificationSettings constant
  25. // and the didRegisterUserNotificationSettings method to AppDelegate.
  26. var fs = require('fs'),
  27. path = require('path'),
  28. rootdir = process.argv[2];
  29. if (!rootdir)
  30. return;
  31. module.exports = function (context) {
  32. var cordova_util = context.requireCordovaModule('cordova-lib/src/cordova/util'),
  33. ConfigParser = context.requireCordovaModule('cordova-lib/src/configparser/ConfigParser'),
  34. projectRoot = cordova_util.isCordova(),
  35. xml = cordova_util.projectConfig(projectRoot),
  36. cfg = new ConfigParser(xml);
  37. // Cordova moved the platforms stuff; try both locations so we'll work for new and old file layouts.
  38. var platforms;
  39. try {
  40. platforms = context.requireCordovaModule('cordova-lib/src/cordova/platforms');
  41. } catch(e) {
  42. platforms = context.requireCordovaModule('cordova-lib/src/platforms/platforms');
  43. }
  44. /**
  45. * The absolute path for the file.
  46. *
  47. * @param {String} platform
  48. * The name of the platform like 'ios'.
  49. * @param {String} relPath
  50. * The relative path from the platform root directory.
  51. *
  52. * @return String
  53. */
  54. function getAbsPath(platform, relPath) {
  55. var platform_path = path.join(projectRoot, 'platforms', platform),
  56. parser = new platforms[platform].parser(platform_path);
  57. return path.join(parser.cordovaproj, relPath);
  58. }
  59. /**
  60. * Replaces a string with another one in a file.
  61. *
  62. * @param {String} path
  63. * Absolute or relative file path from cordova root project.
  64. * @param {String} to_replace
  65. * The string to replace.
  66. * @param {String}
  67. * The string to replace with.
  68. */
  69. function replace (path, to_replace, replace_with) {
  70. var data = fs.readFileSync(path, 'utf8'),
  71. result;
  72. if (data.indexOf(replace_with) > -1)
  73. return;
  74. result = data.replace(new RegExp(to_replace, 'g'), replace_with);
  75. fs.writeFileSync(path, result, 'utf8');
  76. }
  77. // Absolute paths for AppDelegate's header and member
  78. var h_path = getAbsPath('ios', 'Classes/AppDelegate.h'),
  79. m_path = getAbsPath('ios', 'Classes/AppDelegate.m');
  80. // Decleration of UIApplicationRegisterUserNotificationSettings
  81. var h_UIApplicationRegisterUserNotificationSettings =
  82. "extern NSString* const UIApplicationRegisterUserNotificationSettings;\n";
  83. // Implementation of UIApplicationRegisterUserNotificationSettings
  84. var m_UIApplicationRegisterUserNotificationSettings =
  85. "NSString* const UIApplicationRegisterUserNotificationSettings = @\"UIApplicationRegisterUserNotificationSettings\";\n";
  86. // Implementation for didRegisterUserNotificationSettings
  87. var didRegisterUserNotificationSettings =
  88. "#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000\n" +
  89. "\n" +
  90. "- (void) application:(UIApplication*)application\n" +
  91. " didRegisterUserNotificationSettings:(UIUserNotificationSettings*)settings\n" +
  92. "{\n" +
  93. " NSNotificationCenter* center = [NSNotificationCenter\n" +
  94. " defaultCenter];\n" +
  95. "\n" +
  96. " // re-post (broadcast)\n" +
  97. " [center postNotificationName:UIApplicationRegisterUserNotificationSettings\n" +
  98. " object:settings];\n" +
  99. "}\n" +
  100. "#endif\n";
  101. // Inserts the constant decleration
  102. replace(h_path, '@interface AppDelegate', h_UIApplicationRegisterUserNotificationSettings + "\n@interface AppDelegate");
  103. // Inserts the constant implementation
  104. replace(m_path, '@implementation AppDelegate', m_UIApplicationRegisterUserNotificationSettings + "\n@implementation AppDelegate");
  105. // Inserts the didRegisterUserNotificationSettings implementation
  106. replace(m_path, '@end', didRegisterUserNotificationSettings + "\n@end");
  107. };