Skip to content

Latest commit

 

History

History
245 lines (195 loc) · 6.68 KB

README.md

File metadata and controls

245 lines (195 loc) · 6.68 KB

Getting started with iOS

license PRs Welcome wiki

3.0.0 support!

To integrate VasSonic into your Xcode project using CocoaPods, specify it in your Podfile:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'

target 'TargetName' do
    pod 'VasSonic', '3.0.0'
end

support cocoapods

To integrate VasSonic into your Xcode project using CocoaPods, specify it in your Podfile:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'

target 'TargetName' do
    pod 'VasSonic', '3.0.0'
end

Step 1: import and declare

Build Sonic.framework for each platform or using the cocoapods;

Add Sonic.framework to dependency in your main project. Then @import Sonic and register SonicURLProtocol :

Objective-C

[NSURLProtocol registerClass:[SonicURLProtocol class]];

@interface SonicWebViewController : UIViewController<SonicSessionDelegate,UIWebViewDelegate>

Swift

URLProtocol.registerClass(SonicURLProtocol.self)

class SonicWebViewController : UIViewController, UIWebViewDelegate, SonicSessionDelegate

Step 2: Implement SonicSessionDelegate

Objective-C

#pragma mark - Sonic Session Delegate
/*
 * Call back when Sonic will send request.
 */
- (void)sessionWillRequest:(SonicSession *)session
{
    //This callback can be used to set some information, such as cookie and UA.
}
/*
 * Call back when Sonic require WebView to reload, e.g template changed or error occurred. 
 */
- (void)session:(SonicSession *)session requireWebViewReload:(NSURLRequest *)request
{
    [self.webView loadRequest:request];
}

Swift

// MARK: - SonicSessionDelegate
/*
 * Call back when Sonic will send request.
 */
func sessionWillRequest(_ session: SonicSession!)
{
    // This callback can be used to set some information, such as cookie and UA.
}
/*
 * Call back when Sonic require WebView to reload, e.g template changed or error occurred. 
 */
func session(_ session: SonicSession!, requireWebViewReload request: URLRequest!) {
{
    self.webView.loadRequest(request)
}

Step 3: Use Sonic in WebView ViewController

Objective-C

- (instancetype)initWithUrl:(NSString *)aUrl
{
    if (self = [super init]) {
        
        self.url = aUrl;
        
        //Create a Sonic session with url.
        [[SonicClient sharedClient] createSessionWithUrl:self.url withWebDelegate:self];
    }
    return self;
}
/*
 * Send request with Sonic property immediately after the WebView initialization.
 */
- (void)loadView
{
    [super loadView];
    
    self.webView = [[UIWebView alloc]initWithFrame:self.view.bounds];
    self.webView.delegate = self;
    self.view = self.webView;
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:self.url]];
    
    /*
     * If SonicSession is not null, Sonic uses custom SonicWebRequest instead of original network request. 
     */
    if ([[SonicClient sharedClient] sessionWithWebDelegate:self]) {
        [self.webView loadRequest:sonicWebRequest(request)];
    }else{
        [self.webView loadRequest:request];
    }
}

Swift

func initWithUrl(aUrl: String) -> SonicWebViewController {
    if self = super.init() {
        self.url = aUrl
        
        //Create a Sonic session with url.
        SonicClient.shared().createSession(withUrl: self.url, withWebDelegate: self)
    }
    
    return self
}
/*
 * Send request with Sonic property immediately after the WebView initialization.
 */
override func loadView() {
    super.loadView()
    
    self.webView = UIWebView.init(frame: self.view.bounds)
    self.webView.delegate = self
    self.view = self.webView
    
    let request = URLRequest.init(url: URL.init(string: self.url))
    
    /*
     * If SonicSession is not null, Sonic uses custom SonicWebRequest instead of original network request.
     */
    if SonicClient.shared().session(withWebDelegate: self) != nil {
        self.webView.loadRequest(sonicWebRequest(request))
    }
    else {
        self.webView.loadRequest(request)
    }
}

Step 4: Interacts with websites by JavaScript callback.

Objective-C

- (void)getDiffData:(NSDictionary *)option withCallBack:(JSValue *)jscallback
{
    /*
     * ViewController which sends the Sonic request and return result through callback. 
     */
    [[SonicClient sharedClient] sonicUpdateDiffDataByWebDelegate:self.owner completion:^(NSDictionary *result) {
       
        /*
         * Return the result.
         */
        NSData *json = [NSJSONSerialization dataWithJSONObject:result options:NSJSONWritingPrettyPrinted error:nil];
        NSString *jsonStr = [[NSString alloc]initWithData:json encoding:NSUTF8StringEncoding];
        
        JSValue *callback = self.owner.jscontext.globalObject;
        [callback invokeMethod:@"getDiffDataCallback" withArguments:@[jsonStr]];
        
    }];
}

Swift

func getDiffData(option: Dictionary<String, Any>, withCallBack jscallback: JSValue) {
    /*
     * ViewController which sends the Sonic request and return result through callback.
     */
    SonicClient.shared().sonicUpdateDiffData(byWebDelegate: self.owner) { (result) in
        guard let result = result else {return}
        
        /*
         * Return the result.
         */
        guard let json = try? JSONSerialization.data(withJSONObject: result, options: JSONSerialization.WritingOptions.prettyPrinted) else {return}
        
        guard let jsonStr = String.init(data: json, encoding: String.Encoding.utf8) else {return}
        
        guard let callback = self.owner.jscontext.globalObject else {return}
        
        callback.invokeMethod("getDiffDataCallback", withArguments: [jsonStr])
    }
}

Step 5: Remove sonic session.

Objective-C

- (void)dealloc
{
[[SonicClient sharedClient] removeSessionWithWebDelegate:self];
}

Swift

deinit {
    SonicClient.shared().removeSession(withWebDelegate: self)
}

Support

Any problem?

  1. Learn more from sample.
  2. Contact us for help.

License

VasSonic is under the BSD license. See the LICENSE file for details.