ios - UIWebView 将加载一个 URL 而不是另一个

标签 ios objective-c iphone uiwebview

我在自定义 Cell 中设置了 UIWebView,但加载自定义 url 时遇到问题。我想在 UIWebView 中全屏显示 youtube 视频,要在浏览器中执行此操作,您可以删除“watch?v=”并在其位置放置“v/”,然后在末尾添加“&index=18”网址。这在浏览器中工作正常,但 UIWebView 似乎不喜欢它。我已经测试了在日志中生成的 url,它在浏览器中运行良好,但在 UIWebView 中运行良好。

ViewController.h

 @interface ViewController : UIViewController < UITableViewDelegate, UITableViewDataSource, UIWebViewDelegate>

{
NSMutableArray *tableArray;

CGFloat screenWidth;
CGFloat screenHeight;
}

@property (strong, nonatomic) IBOutlet UITableView *tableView;

@property (strong, nonatomic) NSMutableArray *listArray;

@end

ViewController.m

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

GameChooserCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GameChooserCell"];

//If the cell is nil it means no cell was available for reuse and that we should create a new one.

if (cell == nil) {

    // Actually create a new cell (with an identifier so that it can be dequeued).

    cell = [[GameChooserCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"GameChooserCell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

}

/*
 *   Now that we have a cell we can configure it to display the data corresponding to
 *   this row/section
 */


cell.userNameLabel.text                = [[tableArray objectAtIndex:indexPath.row]objectAtIndex:0];
cell.userNameLabel.textColor           = [UIColor grayColor];
cell.userNameLabel.layer.shadowColor   = [UIColor grayColor].CGColor;
cell.userNameLabel.layer.shadowOffset  = CGSizeMake(0.0, 1.0);
cell.userNameLabel.layer.shadowOpacity = 0.5;
cell.userNameLabel.layer.shadowRadius  = 1.0;


NSString *string = [[tableArray objectAtIndex:indexPath.row]objectAtIndex:1];

string = [string stringByReplacingOccurrencesOfString:@"https" withString:@"http"];
string = [string stringByReplacingOccurrencesOfString:@"watch?v=" withString:@"v/"];
string = [string stringByReplacingOccurrencesOfString:@"&feature=youtube_gdata" withString:@""];
string = [NSString stringWithFormat:@"%@&index=18",string];

//string = @"http://www.google.com/images";

NSURL *url = [NSURL URLWithString:string];

NSLog(@"url = \"%@\"", url);

NSURLRequest *request = [NSURLRequest requestWithURL:url];

[cell.webView loadRequest:request];
[cell.webView setBackgroundColor:[UIColor blackColor]];
[cell.webView setOpaque:NO];
 cell.webView.scalesPageToFit = YES;    /* Now that the cell is configured we return it to the table view so that it can display it */
cell.webView.delegate = self;

return cell;

 }

- (void)webViewDidFinishLoad:(UIWebView *)webview {
if (webview.isLoading)
    return;
}

如果有人能阐明我的问题,那就太好了。

干杯尼克。

最佳答案

试试这个:

  • 添加UIWebViewDelegate协议(protocol)
  • 连接委托(delegate)cell.webView.delegate = self;

    - (void)webViewDidFinishLoad:(UIWebView *)webview { if (webview.isLoading) 返回; }

关于ios - UIWebView 将加载一个 URL 而不是另一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26611283/

相关文章:

ios - 如何在 iOS8 Objective C 中更改 UIWebView 中 UIWebBrowserView 的背景颜色

iphone - 添加到收藏夹的 Sprite 或类似效果

iphone - 在数组中搜索字符串,然后在iphone中的textview中显示

iphone - 我可以使用 Xcode 访问 iPhone 的文件系统吗?

iphone - UIScroll : enable touch event even when it is scrolling

ios - UIScrollView xcode 8 swift 3

ios - NSProgress fractionCompleted 键值观察方法未调用

ios - iOS 8 中呈现的 View Controller 中的界面方向不正确

objective-c - Objective-C - 如果在 CGRectmake 上声明?

ios - 异步加载大型 UIImages