CDVYHInterface.m 5.14 KB
#import "CDVYHInterface.h"
#import "CDVYHInterfaceDelegate.h"
#import <objc/runtime.h>
#import "WebPProtocol.h"

@interface CDVYHInterface ()
@property (strong, nonatomic) NSMutableDictionary *abilities;
@end

@implementation CDVYHInterface

- (NSMutableDictionary *)abilities
{
    _abilities = _abilities?:[[NSMutableDictionary alloc] initWithCapacity:1];
    return _abilities;
}

- (void)pluginInitialize {
    
    [NSURLProtocol registerClass:[WebPProtocol class]];
}

- (void)dispose {
    [super dispose];
    
    [NSURLProtocol unregisterClass:[WebPProtocol class]];
}

- (void)dealloc
{
    
}

- (void)eventTriggered:(CDVInvokedUrlCommand *)command
{
    __weak typeof(self)weakSelf = self;
    
    id <CDVYHInterfaceDelegate> yhif = nil;
    if ([self.viewController conformsToProtocol:@protocol(CDVYHInterfaceDelegate)]) {
        yhif = (id <CDVYHInterfaceDelegate>)self.viewController;
    }

    __weak typeof(yhif) weakIf = yhif;
    
    [self.commandDelegate runInBackground:^{
    
        NSDictionary *info = [command argumentAtIndex:0 withDefault:nil];
        if (!info || ![info isKindOfClass:[NSDictionary class]]) {
            CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Invalid parameter format!"];
            [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
            return;
        }

        if (info.allKeys.count < 1) {
            CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Unknown Event"];
            [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
            return;
        }
        
        if ([weakIf respondsToSelector:@selector(nativeActionDidTriggerEventWithUserInfo:completion:)]) {
            
            [weakIf nativeActionDidTriggerEventWithUserInfo:info
                                                 completion:^(id callbackInfo, BOOL result) {
                                                     CDVPluginResult *pluginResult;
                                                     CDVCommandStatus cmdStatus = CDVCommandStatus_ERROR;
                                                     if (result) {
                                                         cmdStatus = CDVCommandStatus_OK;
                                                     } else {
                                                         cmdStatus = CDVCommandStatus_ERROR;
                                                     }
                                                     if ([callbackInfo isKindOfClass:[NSString class]]) {
                                                         pluginResult = [CDVPluginResult resultWithStatus:cmdStatus messageAsString:(NSString *)callbackInfo];
                                                         
                                                     }else if ([callbackInfo isKindOfClass:[NSDictionary class]]){
                                                         pluginResult = [CDVPluginResult resultWithStatus:cmdStatus
                                                                                      messageAsDictionary:(NSDictionary *)callbackInfo];
                                                         
                                                     }else if ([callbackInfo isKindOfClass:[NSArray class]]){
                                                         pluginResult = [CDVPluginResult resultWithStatus:cmdStatus
                                                                                       messageAsMultipart:(NSArray *)callbackInfo];
                                                     }
                                                 
                                                      [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
                                                 }];
        } else {
            CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:nil];
            [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
        }
        
    }];
}

- (BOOL)shouldOverrideLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
    BOOL result = YES;
    
    NSString *urlString = request.URL.absoluteString;
    switch (navigationType) {
        case UIWebViewNavigationTypeLinkClicked:
        case UIWebViewNavigationTypeOther:
        {
            // 先检查是否遵循CDVYHInterfaceDelegate
            id <CDVYHInterfaceDelegate> yhif = nil;
            if ([self.viewController conformsToProtocol:@protocol(CDVYHInterfaceDelegate)]) {
                yhif = (id <CDVYHInterfaceDelegate>)self.viewController;
                result = ![yhif parseLink:urlString];
            }
        }
            break;

//        case UIWebViewNavigationTypeFormSubmitted:
//        case UIWebViewNavigationTypeBackForward:
//        case UIWebViewNavigationTypeReload:
//        case UIWebViewNavigationTypeFormResubmitted:
        default:
            break;
    }
    
    return result;
}

#pragma mark - Private


@end