How to make a history for a web browser - iphone

I'm making my own web browser for the iPad, and I have a problem while I'm trying to make the history. Here's the code:
.m
-(void)viewDidLoad{
[self historyMethod];
//more settings...
}
-(void)historyMethod{
NSString *googleString= #"http://www.google.es";
NSString *currentURLString=TextField.text;
[historyArray addObject:googleString];
if ([googleString isEqual: currentURLString]) {
googleString = nil;
[TableView reloadData];
}
[historyArray addObject: currentURLString ];
[TableView reloadData];
}
The problem that I have is that the historyMethod is executed just one time, and I need it working all the time! Since I'm a newbie, I don't know too much about methods, and how I can make it work well. I tried it with a while loop but it didn't work. Please help me people!

You mean you want the method to be called repeatedly?
you can use something like NSTimer to repeat a method call;
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(historyMethod) userInfo:nil repeats:YES];
This will call your method every 1 seconds, place this in your viewDidLoad method, instead of calling your historyMethod.
However, if you don't understand methods, I'd recommend some looking for some Objective-C tutorials.

If you're using UIWebView then you might wanna make your controller a UIWebViewDelegate.
By implementing - (void)webViewDidFinishLoad:(UIWebView *)webView you could keep track
of pages being loaded. Inside this delegate method you can get URL by webview.request.URL
Take a look at:
UIWebViewDelegate
UIWebViewDelegateProtocol
NSURLRequest

Related

iOs: UIImageView setImage not working while scrolling

I have an UITableView which has UIViews inside each cell (actually I'm using EasyTableView) and inside that view there are 3 UIImageViews changing images every 1/3 of a second.
The problem is, that the images change only while there's no scrolling happening.
I read some issues about this and I found people suggesting the use of NSRunLoop, but that's for NSURLConnection when loading external images, I'm using "UIImage imageNamed" and UIImageView's setImage. The other suggestion I read was to use NSInvocationOperation, but had no luck with that either.
NSInvocationOperation *invocation = [[NSInvocationOperation alloc] initWithTarget:self selector:#selector(changeProgress) object:nil];
[invocation start];
Maybe I'm doing it wrong, please help! Thanks.
Did you try this?
UIScrollView redrawing content while scrolling?
A friend found the solution, I had to create the timer without scheduling it, and add it to the NSRunLoop, so the code would be like this:
_timer = [NSTimer timerWithTimeInterval:1.0/3.0 target:self selector:#selector(update:) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];

NSURLConnection threading problem

i have a big problem and i need your help. Here's what i need to accomplish:
The user select a row from a
TableView
A new view controller is pushed in
the NavigationController, and
displays only a "Loading" message
Meanwhile some data is read from an
XML file (via http)
When the data has been read, an
NSUConnection is used to load an
image from an URL (this URL is part
of the data)
While the image is still loading,
the other data is displayed on the
screen
The image has been downloaded and is
shown, completing the appearance of
the view
The big problem is that i can't use detachNewThreadSelector and NSURLConnection together!
So how can i make a workaround for this? How would you do this?
Thank you VERY much!
You can use following approach...(if you are using asynchronous request)
When your application comes in - (void)connectionDidFinishLoading:(NSURLConnection *)connection ... add a NSInvocationOperation object in NSOperationQueue (which you can handle at application level, by synthesizing it in appDelegate) ..
create NSInvocationOperation as follows..(in connectionDidFinishLoading)
NSInvocationOperation *operation = [[NSInvocationOperation alloc]initWithTarget:self selector:#selector(parseIt) object:nil];
[appDelegate.operationQueue addOperation:operation];
[operation release];
-(void) parseIt
{
//ask for parsing stuff....what you have earlier wrote directly in connectionDidFinishLoading
}
Thanks,
I would use a NSTimer to solve the problem using detachNewThreadSelector and NSURLConnection together.
I have similar scenario where there is a downloading Progress UIViewController showing till the file getting complete, here is what i do:
I Draw a loading View contains a Activity Indicator for example.
I initialize a NSTimer to keep checking if the file is complete.
I call the method that contains the Download Logic.
[1]
-(void) vManageFileRequest
{
[self.oFilesManager vGetSingleFileRequest];
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:#selector(vValidateFileRequest) userInfo:nil repeats:NO]];
}
[2]
[self performSelectorOnMainThread:#selector(vManageFileRequest) withObject:nil waitUntilDone:NO];

"[[UIApplication sharedApplication] openURL" after Link is tapped in UIWebView - Multitasking

I show an UIWebView inside an Application which sends it delegate methods calls, after it receives taps on links.
In the Delegate Method
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
I catch those calls, process the link-urls and run
[[UIApplication sharedApplication] openURL:processedURL];
This works as intended, I end up at the right place.
Problem Multitasking:
if I get back into my app, which is still running in the background after it closed, it will again call the "webView shouldStartLoadWithRequest" with the same link, so I end up sending the user to the page twice. Is there a preferred way to avoid this?
Solution:
You guys are totally right, I did a quite elongated if-else to analyze the given URL, but not in all branches the decision could end up in there existed a "return no"... duh, totally stupid error ;)
Clear the UIWebView so that when it reloads itself, it doesn't have a request pending. This is probably best done this way:
[webView loadRequest:nil];
But if that doesn't work, you can use:
[webView loadHTMLString:#"" baseURL:nil];
And of course you should be returning NO from this delegate method since you don't want the webview to try to load this content.
Not tested, but I'd suggest doing this:
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
[[UIApplication sharedApplication] performSelector:#selector(openURL:)
withObject:processedURL
afterDelay:0];
return NO;
}
This way the callback can return as desired, and in the very next runloop iteration it will call openURL:.

Doing some downloading without blocking you app

I'm working on my first app that's doing a few different web connections at once.
My first screen is my Menu.
And at the bottom of viewDidLoad of MenuViewController i call a method that gets and parses a .xml file that is located on my webserver.
Also at the bottom of viewDidLoad i do
FootballScores = [[FootBallScores alloc] init];
and FootballScores makes a connection to a html page which it loads into a string and then parses out data.
Now since both of these are getting called at the bottom of viewDidLoad of the class thats is responsible for the main menu(first screen in the app) it means the app is kinda slow to load.
What is the right way to do the above? Should i remove the 2 pieces of code from my viewDidLoad and replace with maybe
dataGetterOne = [NSTimer scheduledTimerWithTimeInterval:1.000 target:self
selector:#selector(xmlParser) userInfo:nil repeats:NO];
dataGetterTwo = [NSTimer scheduledTimerWithTimeInterval:2.000 target:self
selector:#selector(htmlParser) userInfo:nil repeats:NO];
This would mean that the methods get called later on and the viewDidLoad gets to finish before the i try get the data from the web servers.
Making 2 connections to we bservers a second apart to quick? Can the iphone handle having 2 connections open at once?
I'm really unsure of anything bad/dangerous I am doing in regards to connections.
Many Thanks
-Code
Try using ASIHTTPRequest. It's easy to use and lets you make asynchronous requests that don't block your app with few lines of code.
Hope this helps,
ender
Also, I wrote a brief tutorial on writing your own XML/JSON data-parsing app. I hope it may be helpful in thinking about how to structure such an app.

problem updating uiimageview from NSTimer method

I have problems updating the image of an UIImageView from within a method that is called from a NSTimer.
It works within the viewDidLoad method where I do something like this
[imageView setImage:[self getNextImage]];
later on I do
NSTimer* readTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(readSource:) userInfo:nil repeats:NO];
[readTimer fire];
that works and the readSource method gets called.
There I put this code:
UIImageView* tempImageView = (UIImageView*)[containerView viewWithTag:nextDefragSourcePosition ];
That returns exactly the imageView I want to have -
but the following line does not change the image of the UIImageView
[tempImageView setImage:dataBeingReadImage];
Any ideas ? Thanks in advance.
Heiko
You are aware that you are updating the GUI from a different thread than where the UIImageView is created? You can execute selectors on the main thread to work around this problem.
Just take a look at the performSelectorOnMainThread method. Just call that from the method that gets called when your NSTimer triggers (readSource:) and supply it with the image you want to display. Then just update the UIImageView from THAT selector (not the readSource one) and it SHOULD work.
There really isn't that much code for me to go on.