首页 > iOS 网络视频播放url referer的问题

iOS 网络视频播放url referer的问题

今天尝试做视频播放,遇到的一个问题。

我们网站的视频的不能直接拿来URL播放,需要给Request加上Referer才可以请求到资源。

    - (void)viewDidLoad {
    //referer
    NSString *referer = @"https://scontent.cdninstagram.com/hphotos-xpa1/t50.2886-16/11200303_1440130956287424_1714699187_n";

    //视频url
    NSURL *videoURL = [NSURL URLWithString:@"http://test.3dker.cn/api/files/get/file/by/576a4b22cc673f1c008f444e.mp4"];
    
   //传给视频url给播放库进行播放
    [self playVideoWithURL:request.URL];
}

- (void)playVideoWithURL:(NSURL *)url
{
    if (!self.videoController) {
        CGFloat width = [UIScreen mainScreen].bounds.size.width;
        self.videoController = [[KRVideoPlayerController alloc] initWithFrame:CGRectMake(0, 0, width, width*(9.0/16.0))];
        __weak typeof(self)weakSelf = self;
        [self.videoController setDimissCompleteBlock:^{
            weakSelf.videoController = nil;
        }];
        [self.videoController showInWindow];
    }
    //此库就直接调用系统MPMoviePlayerController Api了,里面又不可以修改
    self.videoController.contentURL = url;
}

//若不加referer资源就请求不到,但是Api又并没有生成Request,请问,有没有办法解决这种问题?


您可以通过修改下面的部分代码来实现自己的需求。

 //
    //  SunVideoURLProtocol.h
    //  SunVideoURLProtocol
    //
    //  Created by sunbohong on 16/6/27.
    //
    //

    #import <Foundation/Foundation.h>

    @interface SunVideoURLProtocol : NSURLProtocol

    @end


    //
    //  SunVideoURLProtocol.m
    //  SunVideoURLProtocol
    //
    //  Created by sunbohong on 16/6/27.
    //
    //

    #import "SunVideoURLProtocol.h"

    static NSString *const hasInitKey = @"SunVideoURLProtocolKey";

    @interface SunVideoURLProtocol ()

    @property (nonatomic, strong) NSMutableData *responseData;
    @property (nonatomic, strong) NSURLConnection *connection;

    @end

    @implementation SunVideoURLProtocol

    + (void)load {
        [NSURLProtocol registerClass:[SunVideoURLProtocol class]];
    }

    + (BOOL)canInitWithRequest:(NSURLRequest *)request {
        if ([NSURLProtocol propertyForKey:hasInitKey inRequest:request]) {
            return NO;
        }
        //可以根据项目修改此处的逻辑
        if ([request.URL.absoluteString rangeOfString:@"mp4"].length > 0) {
            return YES;
        } else{
            return NO;
        }
    }

    + (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request {
        NSMutableURLRequest *mutableReqeust = [request mutableCopy];
        //修改header
        [mutableReqeust setValue:<#(nullable NSString *)#> forHTTPHeaderField:@"referer"];
        return mutableReqeust;
    }

    - (void)startLoading {
        NSMutableURLRequest *mutableReqeust = [[self request] mutableCopy];
        //防止递归调用
        [NSURLProtocol setProperty:@YES forKey:hasInitKey inRequest:mutableReqeust];
        self.connection = [NSURLConnection connectionWithRequest:mutableReqeust delegate:self];
    }

    - (void)stopLoading {
        [self.connection cancel];
    }

    #pragma mark- NSURLConnectionDelegate

    - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
        [self.client URLProtocol:self didFailWithError:error];
    }

    #pragma mark - NSURLConnectionDataDelegate
    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
        self.responseData = [[NSMutableData alloc] init];
        [self.client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
    }

    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
        [self.responseData appendData:data];
        [self.client URLProtocol:self didLoadData:data];
    }

    - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
        [self.client URLProtocolDidFinishLoading:self];
        NSLog(@"%@", [[NSString alloc] initWithData:self.responseData encoding:NSUTF8StringEncoding]);
    }

    @end

也许可以自定义 NSURLProtocol 来实现为每一个特定的 NSURLRequest 添加 Header,如果你那个库底层是用到 NSURLRequest 的话

【热门文章】
【热门文章】