didRegisterUserNotificationSettings.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. platforms = context.requireCordovaModule('cordova-lib/src/cordova/platforms'),
  35. projectRoot = cordova_util.isCordova(),
  36. xml = cordova_util.projectConfig(projectRoot),
  37. cfg = new ConfigParser(xml);
  38. /**
  39. * The absolute path for the file.
  40. *
  41. * @param {String} platform
  42. * The name of the platform like 'ios'.
  43. * @param {String} relPath
  44. * The relative path from the platform root directory.
  45. *
  46. * @return String
  47. */
  48. function getAbsPath(platform, relPath) {
  49. var platform_path = path.join(projectRoot, 'platforms', platform),
  50. parser = new platforms[platform].parser(platform_path);
  51. return path.join(parser.cordovaproj, relPath);
  52. }
  53. /**
  54. * Replaces a string with another one in a file.
  55. *
  56. * @param {String} path
  57. * Absolute or relative file path from cordova root project.
  58. * @param {String} to_replace
  59. * The string to replace.
  60. * @param {String}
  61. * The string to replace with.
  62. */
  63. function replace (path, to_replace, replace_with) {
  64. var data = fs.readFileSync(path, 'utf8'),
  65. result;
  66. if (data.indexOf(replace_with) > -1)
  67. return;
  68. result = data.replace(new RegExp(to_replace, 'g'), replace_with);
  69. fs.writeFileSync(path, result, 'utf8');
  70. }
  71. // Absolute paths for AppDelegate's header and member
  72. var h_path = getAbsPath('ios', 'Classes/AppDelegate.h'),
  73. m_path = getAbsPath('ios', 'Classes/AppDelegate.m');
  74. // Decleration of UIApplicationRegisterUserNotificationSettings
  75. var h_UIApplicationRegisterUserNotificationSettings =
  76. "extern NSString* const UIApplicationRegisterUserNotificationSettings;\n";
  77. // Implementation of UIApplicationRegisterUserNotificationSettings
  78. var m_UIApplicationRegisterUserNotificationSettings =
  79. "NSString* const UIApplicationRegisterUserNotificationSettings = @\"UIApplicationRegisterUserNotificationSettings\";\n";
  80. // Implementation for didRegisterUserNotificationSettings
  81. var didRegisterUserNotificationSettings =
  82. "#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000\n" +
  83. "\n" +
  84. "- (void) application:(UIApplication*)application\n" +
  85. " didRegisterUserNotificationSettings:(UIUserNotificationSettings*)settings\n" +
  86. "{\n" +
  87. " NSNotificationCenter* center = [NSNotificationCenter\n" +
  88. " defaultCenter];\n" +
  89. "\n" +
  90. " // re-post (broadcast)\n" +
  91. " [center postNotificationName:UIApplicationRegisterUserNotificationSettings\n" +
  92. " object:settings];\n" +
  93. "}\n" +
  94. "#endif\n";
  95. // Inserts the constant decleration
  96. replace(h_path, '@interface AppDelegate', h_UIApplicationRegisterUserNotificationSettings + "\n@interface AppDelegate");
  97. // Inserts the constant implementation
  98. replace(m_path, '@implementation AppDelegate', m_UIApplicationRegisterUserNotificationSettings + "\n@implementation AppDelegate");
  99. // Inserts the didRegisterUserNotificationSettings implementation
  100. replace(m_path, '@end', didRegisterUserNotificationSettings + "\n@end");
  101. };