ActionURL以协议执行处理方法

//

//  SMMURIActionHandlerProtocol.h

//  SocialMediaMonitor

//

//  Created by Pen on 15/10/26.

//  Copyright © 2015 wendy. All rights reserved.

//

 

#import <Foundation/Foundation.h>

@protocol SMMURIActionHandlerProtocol <NSObject>

@optional

// 所支持的Scheme

- (NSString *)supportedScheme;

// 所支持的Host

- (NSString *)supportedHost;

// 处理URI

- (BOOL)handleUri:(NSURL *)uri;

 

@end

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

//

//  SMMUriActionHandlerProtocol.h

//  SocialMediaMonitor

//

//  Created by Kratos on 15/10/26.

//  Copyright © 2015 wendy. All rights reserved.

//

 

#import <Foundation/Foundation.h>

@protocol SMMURIActionHandlerProtocol;

@interface SMMUriActionEngine :NSObject

KSingletonH(Engine)//单例

// 注册处理器

- (void)register:(id <SMMURIActionHandlerProtocol>)handler;

//处理URI

- (BOOL)handle:(NSURL *)uri;

@end

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

//

//  SMMUriActionHandlerProtocol.m

//  SocialMediaMonitor

//

//  Created by Pen on 15/10/26.

//  Copyright © 2015 wendy. All rights reserved.

//

 

#import "SMMUriActionEngine.h"

#import "SMMURIActionHandlerProtocol.h"

 

 

 

@interface SMMUriActionEngine ()

 

@end

 

 

@implementation SMMUriActionEngine

{

    NSMutableDictionary *_mapping;

 

}

 

KSingletonM(Engine)//单例

- (instancetype)init

{

    self = [super init];

    if (self)

    {

        _mapping = [[NSMutableDictionary alloc] init];

    }

    return self;

}

 

- (void)register:(id <SMMURIActionHandlerProtocol>)handler

{

    NSString *key = [self generateKeyWithHandler:handler];

    _mapping[key] = handler;

}

 

- (BOOL)handle:(NSURL *)uri

{

    NSString *key = [self generateKeyWithUri:uri];

    

    id <SMMURIActionHandlerProtocol> handler = _mapping[key];

    if (handler == nil)

    {

        return NO;

    }

    

    return [handler handleUri:uri];

}

 

- (NSString *)generateKeyWithHandler:(id <SMMURIActionHandlerProtocol>)handler

{

    return [NSString stringWithFormat:@"%@_%@", handler.supportedScheme, handler.supportedHost];

}

 

- (NSString *)generateKeyWithUri:(NSURL *)uri

{

    return [NSString stringWithFormat:@"%@_%@", uri.scheme, uri.host];

}

 

@end

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

posted @ 2016-08-15 12:33  老茶叶  阅读(535)  评论(0)    收藏  举报