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

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

我们网站的视频的不能直接拿来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讲师

全部回覆(3)
刘奇

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

大家讲道理

您可以透過修改下面的部分程式碼來實現自己的需求。

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

我也遇到這個問題了,我想問下是怎麼實現的,能給個demo之類的嗎

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板