CDVYHInterface.m 4.18 KB
#import "CDVYHInterface.h"
#import "NSDictionary+Extensions.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:^(NSDictionary *callbackInfo, BOOL result) {
                                                     CDVPluginResult *pluginResult;
                                                     if (result) {
                                                         pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:callbackInfo];
                                                        
                                                     } else {
                                                         pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary: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
{
    NSString *urlString = request.URL.absoluteString;
    switch (navigationType) {
        case UIWebViewNavigationTypeLinkClicked:
        {
            // 先检查是否遵循CDVYHInterfaceDelegate
            id <CDVYHInterfaceDelegate> yhif = nil;
            if ([self.viewController conformsToProtocol:@protocol(CDVYHInterfaceDelegate)]) {
                yhif = (id <CDVYHInterfaceDelegate>)self.viewController;
            } else {
                break;
            }
            
            return [yhif parseLink:urlString];
            
            break;
        }
//        case UIWebViewNavigationTypeFormSubmitted:
//        case UIWebViewNavigationTypeBackForward:
//        case UIWebViewNavigationTypeReload:
//        case UIWebViewNavigationTypeFormResubmitted:
//        case UIWebViewNavigationTypeOther:
        default:
            break;
    }
    
    return NO;
}

#pragma mark - Private


@end