passing a NSArray to NSURLRequest - objective-c

I would like to pass a NSArray containing NSstrings to NSURLRequest, is it possible ? I'm not quite sure how to approach this, I'm getting it to work fine with just one URL but I can't seem to be able to pass an Array or urls . Any ideas ?
This is the code I'm using which is obviously wrong calling url1, I would like to call an array instead :
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 768, 1024)];
NSString *url1 = #"http://www.apple.com/";
NSString *url2 = #"http://www.google.com/";
urls = [[NSArray alloc] initWithObjects:url1,url2,nil];
NSURL *url = [NSURL URLWithString:url1];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
Thanks !

So you want to load multiple webpages, right?
urls = [[NSArray alloc] initwithObjects:url1, url2, nil];
for (NSURL *url in urls) {
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
[webView loadRequest:request];
}
However, I'm not sure why you would want to do this? This may send a request to url1, but will instantly cancel that request and start loading url2.

Related

load pages in UIWebview

I am trying to display web pages using UIwebview on iPad.
I used following code :-
NSString *urlAddress=#"http://google.com";
NSURL *url = [NSURL URLWithString:urlAddress];
NSLog(#"The value of url in screen 2 is %#",url);
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[web loadRequest:requestObj];
web is an object of type uiwebview .
This works fine with certain links but when i tried to open below link
http://deverpids.ltisap.com:8000/sap(bD1lbiZjPTgwMA==)/bc/bsp/sap/zbbdashboard/demo.htm#infohttp://deverpids.ltisap.com:8000/sap(bD1lbiZjPTgwMA==)/bc/bsp/sap/zbbdashboard/demo.htm#info
It does not display anything .Read few articles and taught this could be related to a proxy issue
How to deal with proxy setting in UIWebView?
so tried to use this but no success . code used is as below :
NSURL *url = [NSURL URLWithString:#"http://google.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request startSynchronous];
NSError *error = [request error];
if (!error) {
NSString *response = [request responseString];
NSLog(#"error is %#",response);
}
But now nothing loads up in webview .how do i tackle this .

Injecting JavaScript into UIWebView

I am trying to use the stringByEvaluatingJavaScriptFromString: to alter a WebView and it's not executing as I expect.
Here is the code, this JS doesn't execute though. Any idea why?
UIWebView *wv = [UIWebView alloc] init];
NSURL *url = [[NSURL alloc] initWithString:#"http://www.google.com"];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
[wv loadRequest:request];
[wv stringByEvaluatingJavaScriptFromString:#"document.write('This Works')"];
Probably you'll have to wait for the request to finish before you can use stringByEvaluatingJavaScriptFromString. Set wv.delegate = self; and then implement -webViewDidFinishLoad like this
-(void)webViewDidFinishLoad:(UIWebView *)webView {
[webView stringByEvaluatingJavaScriptFromString:#"document.write('This Works')"];
}

UIWebView doesn't load content

I don't understand why if I load the content of a UIWebView in XCode this way:
NSString *string = #"http://www.dummyurl.org/dummy.pdf";
NSURL *url = [NSURL URLWithString:string];
[my_view loadRequest:[NSURLRequest requestWithURL:url]];
everything works fine, but if a build the original string from a php script I wrote:
NSString *strURL = [NSSTring stringWithFormat:#"www.myserver.php"];
NSString *string = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:strURL]];
NSURL *url = [NSURL URLWithString:string];
[my_view loadRequest:[NSURLRequest requestWithURL:url]];
nothing works. I checked my script and it returns EXACTLY the original string (http://www.dummyurl.org/dummy.pdf) that works fine with the first method.
I build from a PHP script the content of a UITextView too, but that works fine.
I don't understand why this method works with the UITextView but not with the UIWebView to load the .pdf.
It may be useful
NSCharacterSet *characterSet = [NSCharacterSet whitespaceAndNewlineCharacterSet];
NSString *tempString = [string stringByTrimmingCharactersInSet:characterSet];
NSURL *url = [NSURL URLWithString:string];
[my_view loadRequest:[NSURLRequest requestWithURL:url]];
Make sure you linked the webview (IBOutlet) and the delegate.
With this lines it should load a url:
webView.delegate = self;
NSURLRequest *urlRequest;
NSString *urlToOpen = [NSString stringWithFormat:#"http://www.stackoverflow.com"];
urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:urlToOpen]];
[self.webView loadRequest:urlRequest];
Hope this helps...
I had this and it was a cache problem:
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.0]];

Set UIWebView Address String

Declared Function
- (IBAction) changeProductWeb:(NSString *)str;
- (IBAction) changeProductWeb:(NSString *)str{
NSString *urlAddress = str;
NSURLRequest *request =[NSURLRequest requestWithURL:urlAddress];
[webView loadRequest:request];
}
Set string using Array
[cell changeProductWeb:[webTitle objectAtIndex:indexPath.row]];
The Array
webTitle = [[NSArray alloc] initWithObjects:
#"bar.html",
#"bar.html",
#"bar.html",
#"bar.html",
nil];
When I launches it chrashes, if I set the string staticaly in the:
- (IBAction) changeProductWeb:(NSString *)str{
It works fine
[NSURLRequest requestWithURL:] wants an NSURL, not an NSString. Try something like this instead:
NSURLRequest *request =[NSURLRequest requestWithURL:[NSURL URLWithString:urlAddress]];
I am not sure how it works fine setting it statically because you are passing the incorrect type to NSURLRequest. requestWithURL: requires an NSURL not an NSString.
NSURL *urlAddress = [NSURL URLWithString:str];
NSURLRequest *request =[NSURLRequest requestWithURL:urlAddress];

How can i detect Phone number and Hyperlink from Pdf file?

I am displaying my pdf file in UIWebView now i want to detect links and Phone number which is available in my pdf and by pressing link it should open in browser and touching on number it should be called on that particular number so please anybody have idea about it.Give me some suggestion on it.I have written this code for achieving this task but it do not work:
webView=[[UIWebView alloc] initWithFrame:CGRectMake(0, 20, 764, 1004)];
webView.scalesPageToFit = YES;
[self.view addSubview:webView];
webView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:#"Documents"]];
NSString *filePath = [resourceDocPath stringByAppendingPathComponent:#"Gita.pdf"];
NSLog(#"FilePath==>%#",filePath);
//Now create Request for the file that was saved in your documents folder
NSURL *url = [NSURL fileURLWithPath:filePath];
NSLog(#"url==>%#",url);
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView setUserInteractionEnabled:YES];
[webView setDelegate:self];
[webView setDataDetectorTypes: UIDataDetectorTypeAll];
[webView loadRequest:requestObj];
check this link:
http://bill.dudney.net/roller/objc/entry/tiledlayer_on_the_iphone and http://bill.dudney.net/roller/objc/entry/catiledlayer_example.
Fast and Lean PDF Viewer for iPhone / iPad / iOs - tips and hints?