網絡請求所有轉成HTTPS請求的方式

經過runtime運行機制,交換方法的實現。url

+ (NSString *)HTTPTransToHTTPS:(NSString *)urlci

{get

    NSError * error = nil;string

    NSString * urlString = url;it

    NSRegularExpression * regex = [[NSRegularExpression alloc]initWithPattern:@"^http:" options:NSRegularExpressionCaseInsensitive error:&error];io

    urlString = [regex stringByReplacingMatchesInString:urlString options:NSMatchingReportProgress range:NSMakeRange(0, urlString.length) withTemplate:@"https:"];class

    return  urlString;import

 

}select

 

#import "NSURL+HTTPS.h"方法

#import <objc/runtime.h>

#import <objc/message.h>

 

@implementation NSURL (HTTPS)

 

+ (void)initialize

{

    

    Method  method_pre = class_getClassMethod(self,@selector(URLWithString:relativeToURL:));

    Method  method_cur = class_getClassMethod(self,@selector(p_URLWithString:relativeToURL:));

    if(method_cur&&method_pre)

    {

        method_exchangeImplementations(method_pre, method_cur);

    }

}

 

+ (instancetype)p_URLWithString:(NSString *)URLString relativeToURL:(NSURL *)baseURL

{

    NSError * error = nil;

    NSString * urlString = URLString;

    NSRegularExpression * regex = [[NSRegularExpression alloc]initWithPattern:@"^http:" options:NSRegularExpressionCaseInsensitive error:&error];

    urlString = [regex stringByReplacingMatchesInString:urlString options:NSMatchingReportProgress range:NSMakeRange(0, urlString.length) withTemplate:@"https:"];

    return [self p_URLWithString:urlString relativeToURL:baseURL];

    

}

相關文章
相關標籤/搜索