約 1 時間ほど前に正常に動作していたこのコード セットがあります。その後、数分前にもう一度起動しようとすると、UIWebView に白い画面が表示されます。しかし、ここで -(bool) メソッドをブロックすると、viewDidLoad が発生します。(NSLogでテスト済み)コードに何が起こったのか聞いてもよろしいですか?? 機能していたのに、突然機能しなくなりました。
.m
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
NSString *titleString = @"Error Loading Page";
NSString *messageString = [error localizedDescription];
NSString *moreString = [error localizedFailureReason] ?
[error localizedFailureReason] :
NSLocalizedString(@"Try typing the URL again.", nil);
messageString = [NSString stringWithFormat:@"%@. %@", messageString, moreString];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:titleString
message:messageString delegate:self
cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
[alertView show];
}
上記のメソッドを実装すると、UIWebView を開いたときにアプリが常にポップアップし、エラー ドメイン エラー -999 ノンストップと表示されます。
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSString *currentURL = [[request URL] absoluteString];
NSRange range1= [currentURL rangeOfString:@"news"];
NSRange range2= [currentURL rangeOfString:@"pdf"];
if (range1.location == NSNotFound)
{
NSURL *url = [NSURL URLWithString:@"http://www.imc.jhmi.edu/news.html"];
[webView loadRequest:[NSURLRequest requestWithURL:url]];
return YES;
}
if(range2.location==NSNotFound)
{
NSURL * url = [NSURL URLWithString:@"http://www.imc.jhmi.edu/news.html"];
[webView loadRequest:[NSURLRequest requestWithURL:url]];
return YES;
}
return NO;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
webView.scalesPageToFit=YES;
webView.delegate = self;
NSString *urlAddress = @"http://www.imc.jhmi.edu/news.html";
NSURL *url =[NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
NSLog(@"sadfasdf");
}
.h @interface ViewController : UIViewController{ IBOutlet UIWebView*webView; }
@property(nonatomic,strong) UIWebView*webView;
@end