ios7 UIWebView Youtube Video

前端 未结 5 573
滥情空心
滥情空心 2021-02-04 10:33

I have a UIWebView subclass that I used to play both youtube and local videos. Worked perfectly under iOS6. In upgrading to iOS7 I\'ve encountered a problem I don\'t really kn

5条回答
  •  既然无缘
    2021-02-04 11:11

    This is the tested code in IOS7 , its working fine

    [self embedYouTube:[self getYTUrlStr:@"https://www.youtube.com/watch?v=Zq4o_Ca14aw"] frame:CGRectMake(0,40,self.view.frame.size.width, self.view.frame.size.height-40)];
    
    
    - (NSString*)getYTUrlStr:(NSString*)url
    {
        if (url == nil)
            return nil;
    
        NSString *retVal = [url stringByReplacingOccurrencesOfString:@"watch?v=" withString:@"v/"];
    
        NSRange pos=[retVal rangeOfString:@"version"];
        if(pos.location == NSNotFound)
        {
            retVal = [retVal stringByAppendingString:@"?version=3&hl=en_EN"];
        }
        return retVal;
    }
    
    
    
    - (void)embedYouTube:(NSString*)url frame:(CGRect)frame {
    
        NSString* embedHTML = @"\
        \
        \
        \
        \
        ";
        NSString* html = [NSString stringWithFormat:embedHTML, url, frame.size.width, frame.size.height];
        if(videoView == nil) {
            videoView = [[UIWebView alloc] initWithFrame:frame];
            videoView.delegate=self;
            [self.view addSubview:videoView];
        }
    
    
        videoView.mediaPlaybackAllowsAirPlay=YES;
        videoView.allowsInlineMediaPlayback=YES;
        [videoView loadHTMLString:html baseURL:nil];
    
    }
    

提交回复
热议问题