iphone - App crashes due to WebView -


in app using uiwebview display websites, whenever click button , start playing other parts in app, app crashes no reason. suspect webview causing issue because crashes when try open webview.

i have used nsurlconnection load webview , have made webview, connection objects nil in view disappear method.

@implementation newswebsiteviewcontroller  @synthesize connection,rcvddata,spinner1,currentsite,webview,newswebsite;  - (void)viewdidload {     [super viewdidload];     @try {         self.webview.delegate=self;         [uiapplication sharedapplication].networkactivityindicatorvisible=yes;         self.title= self.newswebsite.title;         self.webview.backgroundcolor =[uicolor grouptableviewbackgroundcolor];         self.spinner1 = [[uiactivityindicatorview alloc] initwithactivityindicatorstyle:uiactivityindicatorviewstylewhitelarge];         cgrect center = [self.view bounds];         cgsize wincenter = center.size;         cgpoint pont = cgpointmake(wincenter.width/2,wincenter.height/2);         [spinner1 setcenter:pont];         [self.view addsubview:spinner1];         [self.spinner1 startanimating];         nsstring *url = newswebsite.link;         nsurlrequest *thereq =[nsurlrequest requestwithurl:[nsurl urlwithstring:url] cachepolicy:nsurlrequestuseprotocolcachepolicy timeoutinterval:20.0];         self.connection = [[nsurlconnection alloc] initwithrequest:thereq delegate:self];         if(self.connection) {             self.rcvddata = [[nsmutabledata data] retain];         }         else {         uialertview *alert = [[[uialertview alloc] initwithtitle:@"error!" message:@"we having problem connecting internet, why not try again or try sometime later!.." delegate:self cancelbuttontitle:@"ok" otherbuttontitles:nil] autorelease];         [alert show];         }         webview.multipletouchenabled=yes;         webview.scalespagetofit=yes;     }     @catch (nsexception * e) {         }   }  -(void) goback {     self.webview =nil;     [self.navigationcontroller popviewcontrolleranimated:yes]; }  -(void) viewwilldisappear:(bool)animated {      [self.connection cancel];     self.connection=nil;     [self.webview stoploading];     self.webview=nil;      [uiapplication sharedapplication].networkactivityindicatorvisible=no;      if (self.spinner1 ==nil) {      }      else {         [self.spinner1 stopanimating];     }  }  -(void) webviewdidfinishload:(uiwebview *)webview {      [uiapplication sharedapplication].networkactivityindicatorvisible=no;      if (self.spinner1 ==nil) {      }     else {         [self.spinner1 stopanimating];     } }  -(void) viewdidappear:(bool)animated  {     [uiapplication sharedapplication].networkactivityindicatorvisible=no;  }  - (void)didreceivememorywarning {     [super didreceivememorywarning]; }   - (void)viewdidunload {     [super viewdidunload]; }   -(void) connection:(nsurlconnection *)connection didreceiveresponse:(nsurlresponse *)response {     [self.rcvddata setlength:0]; }   -(void) connection:(nsurlconnection *)connection didreceivedata:(nsdata *)data {     [self.rcvddata appenddata:data]; }   -(void) connection:(nsurlconnection *)connection didfailwitherror:(nserror *)error {     if (self.spinner1 ==nil) {         }     else {         [self.spinner1 stopanimating];         }     [connection release];     [rcvddata release];     uialertview *alert = [[[uialertview alloc] initwithtitle:@"error!" message:@"we having problem connecting internet, why not try again or try sometime later!.." delegate:self cancelbuttontitle:@"ok" otherbuttontitles:nil] autorelease];     [alert show]; }   -(void)connectiondidfinishloading:(nsurlconnection *)connection {     [connection release];     [rcvddata release];     nsstring *url = newswebsite.link;     nsurl *url1 =  [nsurl urlwithstring:url];     [self.webview loaddata:self.rcvddata mimetype:@"text/html" textencodingname:@"utf-8" baseurl:url1]; }  - (void)dealloc {     [super dealloc];     [self.spinner1 release]; }  @end 

firstly, here's small primer equality/inequality in c/objective-c:

let's have bool value (ie, value can either yes or no, 'on' or 'off', 'true' or 'false'), called isenabled. now, if had assigned bool value (sometimes called 'flag') 'yes', conditionally test value so:

bool isenabled = yes;  if (isenabled) {    // value set yes } 

in addition above, can use negation operator (an exclamation mark - otherwise known not operator) flip isenabled's value, , test opposite value:

bool isenabled = yes;  // following reads "if *not* enabled" if (!isenabled) {    // value set no } 

now of course in above example isenabled set yes, condition fail. but, if consider following example, property of if whereby if else if 'met' (ie, true) anywhere in series of if's , else if's, execute code inside it, , ignore else:

bool isenabled = no;  if (isenabled) {    // code here not run, above if condition false } else if (!isenabled) {    // code run, since above condition (!isenabled) evaluate true } else if (isenabled) {    // code never run, since previous condition true , following else if's ignored } else if (!isenabled) {    // code never run, since previous condition true , following else if's ignored } 

while above has 2 redundant else if's @ end, way demonstrate how conditional code works.

so, in webviewdidfinishload: method, instead of having blank if evaluate if/else condition, can replace simpler condition:

-(void) webviewdidfinishload:(uiwebview *)webview  {     [uiapplication sharedapplication].networkactivityindicatorvisible=no;      // or, (self.spinner1 != nil)     if (!self.spinner1)      {         [self.spinner1 stopanimating];     } } 

when you've made above change if's , else if's, post stack trace , i'll see else be.


Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -