加载另一个URL时,UIWebView会使应用程序崩溃

时间:2013-06-05 07:57:00

标签: ios objective-c uiwebview delegates

在我第一次加载URL时,它运行正常,但在我加载新URL之后,应用程序崩溃了。我的类是UIView的子类。

@implementation web
{
    UIWebView * wv;
    BOOL isGoogle;
    UIImageView *iview;
}

- (id)initWithFrame:(CGRect)frame loadWithUrl:(NSString *)url
{
    self = [super initWithFrame:frame];
    if (self) {
        wv = [[UIWebView alloc] initWithFrame:frame];
        wv.delegate = self;
        developerUrl = url;
        [wv loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
        [self addSubview:wv];
    }
    return self;
}
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSString * url = [[request URL] absoluteString];
    if ([url hasPrefix:@"https://google.com"])
    {
        isGoogle = true;
        NSArray * images = [[NSArray alloc] initWithObjects:[ UIImage imageNamed:@"1.tiff"],[ UIImage imageNamed:@"2.tiff"],[ UIImage imageNamed:@"3.tiff"],[ UIImage imageNamed:@"4.tiff"],[ UIImage imageNamed:@"5.tiff"],[ UIImage imageNamed:@"6.tiff"],[ UIImage imageNamed:@"7.tiff"],[ UIImage imageNamed:@"8.tiff"],[ UIImage imageNamed:@"9.tiff"],[ UIImage imageNamed:@"10.tiff"], nil];
                iview = [[UIImageView alloc] initWithFrame:self.frame];
            iview.animationImages = images;
                [iview startAnimating];
        [self addSubview:iview];
    }
    return true;
}


-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    if (isGoogle)
    {
        NSString * content = [webView stringByEvaluatingJavaScriptFromString:@"document.body.innerHTML"];
        [wv stringByEvaluatingJavaScriptFromString :@"document.open();document.close()"];
        NSData * data = [content dataUsingEncoding:NSUTF8StringEncoding];
        id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        NSDictionary * jsonDict = json;
        NSDictionary * dict = [jsonDict objectForKey:@"RESULT"];
        NSDictionary * dict2;
        if ([dict objectForKey:@"ERROR"])
        {
              UIAlertView * aview = [[UIAlertView alloc] initWithTitle:@"unknown error" message:nil delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
              [aview show];
        }
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [iview removeFromSuperview];
    if([wv isLoading])
     {
            [wv stopLoading];
     }
     [wv loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://yahoo.com"]]]
   //APP CRASHES HERE......
}

/

*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
`

0 个答案:

没有答案