I get the bad access when followLink is called. This does not happen if I paste the openURL line into textContainsURL, so I’m assuming the object no longer exists once the method finishes? I’m new to this but I though ARC was supposed to handle this sort of thing for you?
@interface MyViewController : UIViewController
{
NSURL *newsURL;
}
@end
following is in the implementation:
- (void)followLink
{
[[UIApplication sharedApplication]openURL:newsURL];
}
- (BOOL)textContainsURL:(NSString*)text
{
NSError *error = NULL;
//scan text to see if there is a link and only set this up if there is
NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:&error];
NSArray *matches = [detector matchesInString:text
options:0
range:NSMakeRange(0, [text length])];
for (NSTextCheckingResult *match in matches)
{
//NSRange matchRange = [match range];
if ([match resultType] == NSTextCheckingTypeLink)
{
newsURL = [[NSURL alloc] init];
newsURL = [match URL];//what's the void state? retain it
return YES;
}
}
return NO;
}
You should copy the matched URL to your newsURL ivar or make your newsURL ivar a copy property and set the value through the accessor method. In your current code the URL is autoreleased.