JSPatch Deployment and Usage Guide
Prerequisites
- iOS Development Environment: Xcode 7.0 or later
- Objective-C Runtime: iOS 6.0 or later
- JavaScript Engine: Built-in JavaScriptCore framework (iOS 7.0+)
- Dependency Manager: CocoaPods (recommended) or manual file copying
Installation
Using CocoaPods (Recommended)
-
Install CocoaPods if not already installed:
sudo gem install cocoapods -
Create a
Podfilein your project directory:# Your Podfile platform :ios, '6.0' pod 'JSPatch' -
Install the pod:
pod install -
Open the generated
.xcworkspacefile in Xcode.
Manual Installation
-
Copy the following files from the JSPatch repository to your project:
JSEngine.mJSEngine.hJSPatch.js
-
Add these files to your Xcode project.
Configuration
Objective-C Integration
-
Import the JSPatch engine in your application delegate:
#import "JPEngine.h" -
Start the JavaScript engine in your application delegate's
application:didFinishLaunchingWithOptions:method:- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [JPEngine startEngine]; return YES; }
JavaScript Configuration
- JavaScript files can be loaded from local resources or remote URLs
- No additional configuration required for basic usage
- For production, consider using the JSPatch Platform for secure and controlled hotfix deployment
Build & Run
Local Development
-
Running JavaScript from local files:
NSString *sourcePath = [[NSBundle mainBundle] pathForResource:@"demo" ofType:@"js"]; NSString *script = [NSString stringWithContentsOfFile:sourcePath encoding:NSUTF8StringEncoding error:nil]; [JPEngine evaluateScript:script]; -
Running JavaScript directly:
[JPEngine evaluateScript:@"\ var alertView = require('UIAlertView').alloc().init();\ alertView.setTitle('Alert');\ alertView.setMessage('AlertView from js'); \ alertView.addButtonWithTitle('OK');\ alertView.show(); \ "]; -
Running JavaScript from network:
[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://cnbang.net/test.js"]] queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { NSString *script = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; [JPEngine evaluateScript:script]; }];
Production Build
- Ensure all JavaScript files are included in your app bundle
- Consider minifying JavaScript files for production
- Use the JSPatch Platform for secure remote hotfix deployment
Deployment
App Store Deployment
- Static JavaScript: Include all JavaScript files in your app bundle
- Remote Updates: Use JSPatch Platform for secure remote hotfix deployment
- Code Signing: Ensure your app is properly signed for distribution
Hotfix Deployment
Important Notice: The README strongly recommends not implementing JSPatch directly in your app. Instead, use the JSPatch Platform for:
- Secure hotfix deployment
- Version control and rollback capabilities
- Monitoring and analytics
- Compliance with Apple's App Store guidelines
Alternative Deployment Platforms
- JSPatch Platform: Official platform for secure hotfix deployment
- Custom Backend: Host JavaScript files on your own server (use with caution and proper security measures)
Troubleshooting
Common Issues
-
Engine Not Starting
- Ensure
[JPEngine startEngine]is called before evaluating any scripts - Check that the JavaScriptCore framework is available (iOS 7.0+)
- Ensure
-
Class Not Found
- Verify the class name is correct in the
require()statement - Ensure the class is properly imported in your Objective-C code
- Verify the class name is correct in the
-
Method Not Found
- Check method signatures match exactly (including parameter types)
- Use
defineClassto override methods if needed
-
Memory Issues
- JavaScript objects are automatically garbage collected
- Be cautious with large data structures passed between Objective-C and JavaScript
-
Threading Issues
- JavaScript execution runs on the main thread by default
- Use GCD for background operations when needed
Debugging Tips
-
Console Logging:
console.log("Debug message"); -
Error Handling:
try { // JavaScript code } catch (e) { console.log("Error: " + e); } -
Objective-C Debugging:
- Use Xcode's debugger to set breakpoints in Objective-C code
- Check the JavaScript console for runtime errors
Performance Considerations
- Minimize the size of JavaScript files for faster loading
- Use local files for frequently used scripts
- Consider caching remote scripts for offline functionality
Security Considerations
- Never evaluate untrusted JavaScript code
- Use HTTPS for remote script loading
- Implement proper authentication for remote hotfix deployment
- Consider using the JSPatch Platform for secure deployment
For more detailed documentation, visit the JSPatch Wiki.