← Back to bang590/JSPatch

How to Deploy & Use bang590/JSPatch

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)

  1. Install CocoaPods if not already installed:

    sudo gem install cocoapods
    
  2. Create a Podfile in your project directory:

    # Your Podfile
    platform :ios, '6.0'
    pod 'JSPatch'
    
  3. Install the pod:

    pod install
    
  4. Open the generated .xcworkspace file in Xcode.

Manual Installation

  1. Copy the following files from the JSPatch repository to your project:

    • JSEngine.m
    • JSEngine.h
    • JSPatch.js
  2. Add these files to your Xcode project.

Configuration

Objective-C Integration

  1. Import the JSPatch engine in your application delegate:

    #import "JPEngine.h"
    
  2. 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

  1. 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];
    
  2. Running JavaScript directly:

    [JPEngine evaluateScript:@"\
     var alertView = require('UIAlertView').alloc().init();\
     alertView.setTitle('Alert');\
     alertView.setMessage('AlertView from js'); \
     alertView.addButtonWithTitle('OK');\
     alertView.show(); \
    "];
    
  3. 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

  1. Static JavaScript: Include all JavaScript files in your app bundle
  2. Remote Updates: Use JSPatch Platform for secure remote hotfix deployment
  3. 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

  1. Engine Not Starting

    • Ensure [JPEngine startEngine] is called before evaluating any scripts
    • Check that the JavaScriptCore framework is available (iOS 7.0+)
  2. Class Not Found

    • Verify the class name is correct in the require() statement
    • Ensure the class is properly imported in your Objective-C code
  3. Method Not Found

    • Check method signatures match exactly (including parameter types)
    • Use defineClass to override methods if needed
  4. Memory Issues

    • JavaScript objects are automatically garbage collected
    • Be cautious with large data structures passed between Objective-C and JavaScript
  5. Threading Issues

    • JavaScript execution runs on the main thread by default
    • Use GCD for background operations when needed

Debugging Tips

  1. Console Logging:

    console.log("Debug message");
    
  2. Error Handling:

    try {
        // JavaScript code
    } catch (e) {
        console.log("Error: " + e);
    }
    
  3. 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.