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')"];
}
Related
When i will try to post some image or text from application to server that situation MBProgressHUD not worked. but without posting method its work perfectly. i am used below code.Please any one help me. thanks.
HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
HUD.dimBackground = YES;
HUD.delegate = self;
NSString *urlRequest =[NSString stringWithFormat:#"URL"];
NSString *pStrLegalURLString =[urlRequestn stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [[NSURL alloc] initWithString:pStrLegalURLString];
NSMutableURLRequest *request1 = [NSMutableURLRequest requestWithURL:url];
[request1 setHTTPMethod:#"POST"];
NSData *returnData1 = [ NSURLConnection sendSynchronousRequest: request1 returningResponse: nil error: nil ];
NSString *returnString1 = [[NSString alloc] initWithData:returnData1 encoding: NSUTF8StringEncoding];
Try like this:
It will work, but your problem as i guess is because of sendSynchronousRequest which is a blocking call hence if the main thread hangs up you won't see any update in UI. So try to make such calls in background.
Try one of the two approaches as I see:
Approach 1: (send request in background using selectors and do UI update in main thread)
-(void)method1 {
HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
HUD.dimBackground = YES;
HUD.delegate = self;
NSString *urlRequest =[NSString stringWithFormat:#"URL"];
NSString *pStrLegalURLString =[urlRequest stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [[NSURL alloc] initWithString:pStrLegalURLString];
NSMutableURLRequest *request1 = [NSMutableURLRequest requestWithURL:url];
[request1 setHTTPMethod:#"POST"];
// send request in background.
[self performSelectorInBackground:#selector(method2) withObject:nil];
}
-(void)method2 {
NSData *returnData1 = [ NSURLConnection sendSynchronousRequest: request1 returningResponse: nil error: nil ];
NSString *returnString1 = [[NSString alloc] initWithData:returnData1 encoding: NSUTF8StringEncoding];
[self performSelectorOnMainThread:#selector(method3) withObject:nil waitUntilDone:NO];
}
-(void)method3
{
// update ui in main thread.
[MBProgressHUD hideAllHUDsForView:self.view animated:YES];
}
Approach 2: Use Dispatch_Async and Dispatch_Sync
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^(void){
// Do background work here
dispatch_sync(dispatch_get_main_queue(), ^(void){
// Update UI here
});
});
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]];
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];
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.
Im using the openURL command to send an SMS, but it opens the safari,
can I send the command so it opens in the background??
ie, the user dont see the browser opening or the message that it gives, and stays in the app,
NSURL *url = [NSURL URLWithString: #"https://smsgw.exetel.com.au/sendsms/api_sms.php?username=xxx&password=xx&mobilenumber=0xxx&message=xxr&sender=mk&messagetype=Text&referencenumber=04"];
[[UIApplication sharedApplication] openURL: url];
Thank you
You can't have Safari open a URL for you in the background. But supposing you just want to request the URL and ignore whatever comes back, you could try:
NSURL *url = [NSURL urlWithString:#"...whatever..."];
NSURLRequest *request = [NSURLRequest requestWithURL:url delegate:nil];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:request];
[connection start];
You can do this be creating a UIWebView inside your app, and calling loadRequest: on the instance, like this:
UIWebView *webView = [[UIWebView alloc] init];
webView.delegate = self;
NSURL *url = [NSURL URLWithString: #"https://smsgw.exetel.com.au/sendsms/api_sms.php?username=xxx&password=xx&mobilenumber=0xxx&message=xxr&sender=mk&messagetype=Text&referencenumber=04"];
NSUrlRequest *request = [[NSUrlRequest alloc] initWithURL:url];
[webView loadRequest:request];
This will load the URL inside the app, without sending you over to Mobile Safari. The benefit of this method over NSUrlConnection is that if you conform to the UIWebViewDelegate protocol and implement webViewDidFinishLoad: in your delegate class, you can see if the call succeeded or not.