我知道这是一个常见的问题,但没有一个答案可以解决我的问题。我的应用程序中有一个UIWebView。我把一切都设置得正确;如果设置了委托,则webView是我的视图控制器的属性。它被设置为在加载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];
}
答案 0 :(得分:1)
我有同样的问题,我不完全确定原因,但我想它必须对viewControllers生命周期做些什么。无论如何尝试在viewDidAppear方法中加载URL。它为我修好了。希望它会有所帮助
答案 1 :(得分:0)
事实证明我的问题与UIWebView
无关。我只需要更改我为我的应用设置的URLConnectionDelegate
中的凭据持久性。我将其设置为NSURLCredentialPersistanceNone
,但我将其更改为NSURLCredentialPersistenceSession
。我希望这可以帮助那些像我一样遇到确切问题的其他人