UIViewController+HTTP.m 1.67 KB
Newer Older
lee committed
1 2 3 4 5 6 7 8 9
//
//  UIViewController+HTTP.m
//  iCemarose
//
//  Created by Sanchew on 2017/7/13.
//  Copyright © 2017年 kollway. All rights reserved.
//

#import "UIViewController+HTTP.h"
lee committed
10 11
#import <JLRoutes/JLRoutes.h> 
#import <RegexKitLite/RegexKitLite.h>
lee committed
12 13
#import <TOWebViewController/TOWebViewController.h>
#import "AppDelegate.h"
lee committed
14 15 16 17

@implementation UIViewController (HTTP)

- (void)openURLWithString:(NSString *)URLString {
u  
lee committed
18 19 20 21 22 23 24 25 26
    NSString *parten=@"[^%\\da-zA-Z:/.?&=]+";
    NSRegularExpression *reg=[NSRegularExpression regularExpressionWithPattern:parten options:0 error:nil];
    NSArray *matchs=[reg matchesInString:URLString options:0 range:NSMakeRange(0, [URLString length])];
    NSString *encodeURL=[NSString stringWithString:URLString];
    for (long i=matchs.count-1; i>=0; --i) {
        NSTextCheckingResult *tcr=matchs[i];
        encodeURL=[encodeURL stringByReplacingCharactersInRange:tcr.range withString:[[encodeURL substringWithRange:tcr.range] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    }
    NSURL *url = [[NSURL alloc] initWithString:[encodeURL stringByReplacingOccurrencesOfRegex:@"^https?" withString:@"cemarose"]];
lee committed
27 28
    if ([JLRoutes canRouteURL:url]) {
        [JLRoutes routeURL:url];
lee committed
29 30
//    }else if ([application respondsToSelector:@selector(openURL:options:completionHandler:)]) {
//        [application openURL:[NSURL URLWithString:URLString] options:@{} completionHandler:nil];
lee committed
31
    } else {
lee committed
32 33 34
//        [application openURL:[NSURL URLWithString:URLString]];
        TOWebViewController *wv = [[TOWebViewController alloc] initWithURLString:URLString];
        [[AppDelegate mainViewController].selectedViewController pushViewController:wv animated:YES];
lee committed
35
    }
lee committed
36 37 38
}

@end