Rumah  >  Soal Jawab  >  teks badan

objective-c - 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,请问,有没有办法解决这种问题?

PHP中文网PHP中文网2741 hari yang lalu1052

membalas semua(3)saya akan balas

  • 怪我咯

    怪我咯2017-04-18 09:17:31

    Mungkin anda boleh menyesuaikan NSURLProtocol untuk menambah NSURLRequestPengepala bagi setiap khusus, jika pustaka anda menggunakan NSURLRequest di bahagian bawah

    balas
    0
  • 大家讲道理

    大家讲道理2017-04-18 09:17:31

    Anda boleh merealisasikan keperluan anda dengan mengubah suai beberapa kod di bawah.

     //
        //  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

    balas
    0
  • 巴扎黑

    巴扎黑2017-04-18 09:17:31

    Saya juga menghadapi masalah ini. Saya ingin tahu bagaimana untuk mencapainya. Bolehkah anda berikan saya demo?

    balas
    0
  • Batalbalas