2014-05-19 65 views
0

我使用此代碼,打開一個網頁視圖YouTube網址:錯誤:無效SendDelegateMessage(NSInvocation的*):委託上加載Web視圖

-(void)youtube_page 
{ 

    [webview addSubview:webview1]; 

    self.navigationController.navigationBarHidden = NO; 

    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style: UIBarButtonItemStyleBordered target:self action:@selector(Back)]; 
    self.navigationItem.leftBarButtonItem = backButton; 

initWithBarButtonSystemItem:UIBarButtonSystemItemBack target:self action:@selector(goBack)]; 
initWithObjects:backBarButtonItem, nil]; 

    [webview1 loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.youtube.com/watch?v="]]]; 
} 

,我得到這個錯誤:

void SendDelegateMessage(NSInvocation *): delegate 
(uiwebView:decidePolicyForNavigationAction:request:frame:decisionListener:) failed to return 
after waiting 10 seconds. main run loop mode: kCFRunLoopDefaultMode 

回答

0

試試這個代碼:

[YoutubeWebview.scrollView setBounces:NO]; 
YoutubeWebview.mediaPlaybackAllowsAirPlay = YES; 
YoutubeWebview.mediaPlaybackRequiresUserAction = YES; 

檢查您的網絡是否可用..

- (NSInteger)checkNetworkStatus 
{ 
    if ([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] == NotReachable) 
    { 
     [NSTimer scheduledTimerWithTimeInterval:0.6 target:self selector:@selector(Alertview_Called) userInfo:nil repeats:NO]; 
     return -1; 
    } 
    else 
    { 
     return 0; 
    } 
} 

-(void)youtube_page 
{ 
    if (0 == [self checkNetworkStatus]) 
    { 
      [self embedYouTube:[self getYTUrlStr:@"https://www.youtube.com/watch?v=Zq4o_Ca14aw"] frame:CGRectMake(0,50,self.view.frame.size.width, self.view.frame.size.height-50)]; 
    } 
} 


- (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 = @"<html><head><style type=\"text/css\">\body { \background-color: transparent;color: white;}</style>\ 
</head><body style=\"margin:0\"><embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" width=\"%0.0f\" height=\"%0.0f\"></embed></body></html>"; 

    NSString* html = [NSString stringWithFormat:embedHTML, url, frame.size.width, frame.size.height]; 

    if(YoutubeWebview == nil) 
    { 
     YoutubeWebview = [[UIWebView alloc] initWithFrame:frame]; 
     YoutubeWebview.opaque=NO; 
     [YoutubeWebview setBackgroundColor:[UIColor blackColor]]; 
     YoutubeWebview.delegate=self; 
     [Youtubeview addSubview:YoutubeWebview]; 
    } 

    YoutubeWebview.mediaPlaybackAllowsAirPlay=YES;  
    YoutubeWebview.allowsInlineMediaPlayback=YES; 
    [YoutubeWebview loadHTMLString:html baseURL:nil]; 
}