objective-c - iOS 网络视频播放url referer的问题
PHP中文网
PHP中文网 2017-04-18 09:15:31
0
3
1083

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

我们网站的视频的不能直接拿来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,请问,有没有办法解决这种问题?

PHP中文网
PHP中文网

认证高级PHP讲师

reply all(3)
刘奇

Perhaps it can be customized NSURLProtocol 来实现为每一个特定的 NSURLRequest 添加 Header,如果你那个库底层是用到 NSURLRequest if

大家讲道理

You can realize your needs by modifying some of the codes below.

 //
    //  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
巴扎黑

I also encountered this problem. I would like to know how to achieve it. Can you give me a demo or something like that?

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template