didRegisterUserNotificationSettings.js 4.6 KB

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