gpt4 book ai didi

iphone - UIWebView 未加载 url

转载 作者:行者123 更新时间:2023-11-29 04:24:08 25 4
gpt4 key购买 nike

我知道这是一个常见问题,但没有一个答案可以解决我的问题。我的应用程序中有一个 UIWebView。我已正确设置所有内容;委托(delegate)已设置,webView 是我的 View Controller 的属性。它被设置为在加载 viewController 时加载 html 字符串。当 html 字符串加载时,在 webViewDidFinishLoad 方法中它会检查这是否是第一次加载,如果是,则被告知加载带有 url 的请求。我用于 url 的字符串是我的 viewController 的属性。该字符串正是它应该的样子。只是有一些东西阻止了 webView 实际加载请求。任何想法将不胜感激。谢谢

    @interface WebViewController : UIViewController <UIWebViewDelegate, UIActionSheetDelegate, UIPrintInteractionControllerDelegate, MFMailComposeViewControllerDelegate, NSURLConnectionDelegate>

@property (nonatomic, assign) BOOL firstLoad;
@property (nonatomic, retain) UIWebView *webView;
@property (nonatomic, retain) NSString *prefixURLString;
@property (nonatomic, retain) NSString *suffixURLString;
@property (nonatomic, retain) UIActionSheet *actionSheet;

- (id)initWithTitle:(NSString *)newTitle andSuffixURL:(NSString *)suffix;
- (void)reload;
- (void)addActionButton;
- (void)showActions:(UIBarButtonItem *)sender;
- (void)dismissActionSheet;

@end



@implementation WebViewController

@synthesize webView;
@synthesize prefixURLString;
@synthesize suffixURLString;
@synthesize firstLoad;
@synthesize actionSheet;

- (id)initWithTitle:(NSString *)newTitle withPrefixURL:(NSString *)prefix andSuffixURL:(NSString *)suffix
{
self = [super initWithNibName:nil bundle:nil];
if (self) {
// Custom initialization
[self setPrefixURLString:prefix];
[self setFirstLoad:YES];
[self setTitle:newTitle];
[self setSuffixURLString:suffix];
[self setActionSheet:nil];
}
return self;
}

- (id)initWithTitle:(NSString *)newTitle andSuffixURL:(NSString *)suffix
{
return [self initWithTitle:newTitle withPrefixURL:@"https://customer.stld.com/flatroll/ios/%@" andSuffixURL:suffix];
}

- (void)reload
{
if(self.prefixURLString && self.suffixURLString)
{
NSString *fullURLString = [NSString stringWithFormat:self.prefixURLString, [self.suffixURLString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:fullURLString]]];
}
}
- (void)dealloc
{
[suffixURLString release]; suffixURLString = nil;
[prefixURLString release]; prefixURLString = nil;
[actionSheet release]; actionSheet = nil;
[super dealloc];
}


- (void):(UIWebView *)aWebView didFailLoadWithError:(NSError *)error
{
//NSLog(@"WebViewController:DidFailLoadWithError: %@", [error localizedFailureReason]);
[self setTitle:@"Loading Error"];
[self.webView loadHTMLString:[NSString stringWithFormat:@"<html><head><meta name=\"viewport\" content=\"width=device-width, user-scalable=yes\" /></head><body>Loading error:<br />%@</body></html>", [error localizedFailureReason]] baseURL:nil];
}

- (void)webViewDidFinishLoad:(UIWebView *)aWebView
{
if(self.firstLoad)
{
[self setFirstLoad:NO];
[self reload];
[self addActionButton];
}
}

#pragma mark - View lifecycle

- (void)loadView
{
UIWebView *newWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
[newWebView setDelegate:self];
[newWebView setScalesPageToFit:YES];
[newWebView setUserInteractionEnabled:YES];
[self setWebView:newWebView];
[self setView:newWebView];
[newWebView release];
}

- (void)viewDidLoad
{
[super viewDidLoad];
[self.webView loadHTMLString:@"<html><head><meta name=\"viewport\" content=\"width=device-width, user-scalable=yes\" /></head><body><br />Gathering data and generating report.<br />Depends on data and parameters you have requested, this could take time. Please wait...</body></html>" baseURL:nil];
}

- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
[self.webView setDelegate:nil];
[self setWebView:nil];
[self setView:nil];
}

最佳答案

我也遇到了同样的问题,我不完全确定为什么,但我想它必须与 viewControllers 生命周期有关。不管怎样,尝试在 viewDidAppear 方法中加载 URL。它为我修好了。希望对您有帮助

关于iphone - UIWebView 未加载 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12602313/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com