iosDisablePush.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * Author: Derek Chia <snipking@gmail.com>
  3. * Cordova plugin after hook to disable `Push Notification` capability for XCode 8
  4. */
  5. const fs = require('fs');
  6. const path = require('path');
  7. let commonFuncs = require('./common');
  8. /**
  9. * remove APNS env from cordova project Entitlements-Debug.plist and Entitlements-Release.plist
  10. * This two file will work when xcode archive app
  11. */
  12. let disablePushNotificationForCI = (basePath, xcodeprojName) => {
  13. commonFuncs.removeAPNSinEntitlements(basePath + xcodeprojName + '/Entitlements-Debug.plist');
  14. commonFuncs.removeAPNSinEntitlements(basePath + xcodeprojName + '/Entitlements-Release.plist');
  15. }
  16. /**
  17. * remove APNS env to entitlement file; disable Push Notification capability in .pbxproj file
  18. * This two file will work when xcode archive app
  19. */
  20. let disablePushNotificationForXCode = (entitlementsPath, pbxprojPath) => {
  21. /**
  22. * remove APNS env to entitlement file
  23. */
  24. if( fs.existsSync(entitlementsPath) ) {
  25. commonFuncs.removeAPNSinEntitlements(entitlementsPath);
  26. }
  27. /**
  28. * disable Push Notification capability in .pbxproj file
  29. * equally disable "Push Notification" switch in xcode
  30. */
  31. fs.readFile(pbxprojPath, "utf8", function(err, data) {
  32. if (err) {
  33. throw err;
  34. }
  35. console.log("Reading pbxproj file asynchronously");
  36. // turn off Push Notification Capability
  37. let re4rep = new RegExp('isa = PBXProject;(.|[\r\n])*TargetAttributes(.|[\r\n])*SystemCapabilities(.|[\r\n])*com\.apple\.Push = {(.|[\r\n])*enabled = [01]');
  38. let parts = re4rep.exec(data);
  39. if(parts !== null && parts !== undefined && parts.length > 0) {
  40. result = data.replace(re4rep, parts[0].substr(0, parts[0].length - 1) + '0');
  41. // write result to project.pbxproj
  42. fs.writeFile(pbxprojPath, result, {"encoding": 'utf8'}, function(err) {
  43. if (err) {
  44. throw err;
  45. }
  46. console.log(pbxprojPath + " written successfully");
  47. });
  48. }
  49. });
  50. }
  51. let basePath = './platforms/ios/';
  52. let buildType = 'dev';
  53. let xcodeprojName = commonFuncs.getXcodeProjName(basePath);
  54. let pbxprojPath = basePath + xcodeprojName + '.xcodeproj/project.pbxproj';
  55. let entitlementsPath = basePath + xcodeprojName + '/' + xcodeprojName + '.entitlements';
  56. disablePushNotificationForCI(basePath, xcodeprojName);
  57. disablePushNotificationForXCode(entitlementsPath, pbxprojPath);