I am using UIWebview to show user profile details. Now when trying to upload the image whole Webview reloading and showing back to the same stage before uploading.
UIWebView *ProfileCellWebview = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, cell.layer.frame.size.width, (MainFrame.size.height - 210))];
[ProfileCellWebview setBackgroundColor:[UIColor clearColor]];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:USERPROFILEPUBLIC,self.appDelegate.currentUser.userId]];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[ProfileCellWebview setDelegate:self];
[ProfileCellWebview loadRequest:requestObj];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
[cell addSubview:ProfileCellWebview];
Where i am doing wrong
Implement the code to load the url in viewDidLoad. Hope you write the code in viewWillApear or viewDidApper.
Related
I am working on WKWebView for loading some web pages. I need to pass some header inside WKWebView for change of language. I have passed successfully, however on server side, its showing other language. Please let me know whether the mechanism of passing is right or wrong.
- (void)viewDidLoad {
[super viewDidLoad];
WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init];
WKWebView *webView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:theConfiguration];
webView.navigationDelegate = self;
NSURL *nsurl=[NSURL URLWithString:#""];
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
[webView loadRequest:nsrequest];
[self.view addSubview:webView];
}
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
NSLog(#"%#",navigationAction.request.allHTTPHeaderFields);
NSMutableURLRequest *request = [navigationAction.request mutableCopy];
[request setValue:#"sv" forHTTPHeaderField:#"Accept-Language"];
decisionHandler(WKNavigationActionPolicyAllow);
}
There are two mistakes in your code:
1) You define the header fields too late (after the webview has already started to use the request to load the page)
2) You set the header on a mutable copy of the actual request (so not on the one that's used). This copy is then just dealloced once the method finishes.
Try this here in your viewDidLoad:
// ... start as you did
NSURL *nsurl=[NSURL URLWithString:#""]; // I assume you're using a correct URL in your actual code?
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:nsurl];
[request setValue:#"sv" forHTTPHeaderField:#"Accept-Language"];
[self.view addSubview:webView];
[webView loadRequest:nsrequest]; // I just prefer to add to the view hierarchy before I do anything with it, personal preference.
You do not need to do anything regarding the header fields in your webView:decidePolicyForNavigationAction:decisionHandler: delegate method.
How can i get the alert in click of submit ?
i am using WebView..
Here is the code
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
[webView setDelegate:self];
NSString *urlAddress = #“sssssss”;
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
[self.view addSubview:webView];
Thanks in Advance..
Check this:
- (BOOL)webView: (UIWebView*)webView shouldStartLoadWithRequest: (NSURLRequest*)request navigationType: (UIWebViewNavigationType)navigationType {
NSString *fragment, *scheme;
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
//1
[webView stopLoading];
fragment = [[request URL] fragment];
scheme = [[request URL] scheme];
if ([scheme isEqualToString: #"file"] && [self respondsToSelector: NSSelectorFromString(fragment)]) {
[self performSelector: NSSelectorFromString(fragment)];
return NO;
}
[[UIApplication sharedApplication] openURL: [request URL]];
}
return YES;
}
You need to set delegate to WebView.
Hope this helps.
1 - Add a IBAction to your submit button.
2 - Inside the method that gets called use UIAlertController to create a alert that will be displayed when the button is clicked.
I create app to use UIWebview and show log to url if touch in content in UIWebview.
this is my code
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *url1 =[NSURL URLWithString:#"MyWebSite"];
NSURLRequest *request1 = [NSURLRequest requestWithURL:url1];
[_webView1 loadRequest:request1];
NSURL *url2 =[NSURL URLWithString:#"MyWebsite2"];
NSURLRequest *request2 = [NSURLRequest requestWithURL:url2];
[_webView2 loadRequest:request2];//url menu 2
NSString *currentURL = [_webView1 stringByEvaluatingJavaScriptFromString:#"document.title"];
NSLog(#"%#",currentURL);
}
but I touch the content log is not print and change webview log is print (null)
sorry for my poor English.
You can not get title of a website before you receive data from the URL.
So
Set your webview delegate to self
Then in
- webViewDidFinishLoad: to get title
I have an iPhone app that uses the UIWebView to display a site that I have built as part of a much larger .net application. Everything was running beautifully until a change on my website side. Now what is happening in the iOS app is that the changes to fix the problem in the Website itself aren't appearing in the UIWebView. I verified the changes were in place by going into Safari on my iPhone with the exact same URL and all the changes were there. Why isn't the app recognizing the WebSite changes?
The code for loading the Website is below. Unfortunately I have to not include the actual URL. I'm fairly new to iOS so please be kind :)
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#" " style:UIBarButtonItemStyleBordered target:nil action:nil];
[[self navigationItem] setBackBarButtonItem:backButton];
// Set Logo in Navbar
self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Logo"]];
NSString *urlString = #"http://website.com/page.aspx";
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
}
Hey you could try out below line but this will be a good approach if website is changing contents in some time interval
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:YOUR_WEBSITE_URL] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10.0]];
Or you could simply
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:strurl]];
[request setHTTPShouldHandleCookies:NO];
setHTTPshouldhandlecookie is made NO so no cookies will be saved and everytime it will go for remote resource.
Also for removing cookies from UIWebview check this link out. Hope helps you out !
modify your code with this
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#" "
style:UIBarButtonItemStyleBordered
target:nil
action:nil];
[[self navigationItem] setBackBarButtonItem:backButton];
// Set Logo in Navbar self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Logo"]];
NSString *urlString = #"http://website.com/page.aspx";
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[[NSURLCache sharedURLCache] removeAllCachedResponses];
[webView loadRequest:request];
}
The problem is not that it is reloading. It's handling calls differently in the UIWebView than it is in Safari in the iPhone browser
I have an UIWebview, when user taps a UIButton, my webview will be loaded. I have an 10 HTML files in bundle i want to show the first html in webview and when user **swipes** i want to show the next HTML file. When user wants to see previous HTML file, i want to allow swipe and see. I have used UISwipeGesture but how can i load the files one by one.. Please help me. I have loaded the files from bundle and i used
NSURLREQUEST *req = [NSURLREQUEST requestwithURL : myURL];
[webView loadRequest:req];
myURL is the path for first HTML. How can i do this for 10 HTML files using SWIPE
One simple thing you can do is rename your HTML files like 1.html, 2.html ..... 10.html
Declare an integar variable in your .h file initialize it in viewDidLoad and on detecting swipe right increment in that variable and use formatString to make correct url and reload the webview and on detecting swipe left decrement in that variable make correct url and reload the webview.
-(void)swipeRightDetected
{
if(fileNumber <= 10)
{
fileNumber++;
NSString *urlString = [NSString stringWithFormat:#"http://172.161.331.25/casa/admmathematic/Page00%d.html",fileNumber];
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
[webview loadRequest:request];
}
else
{
//No more files
}
}
-(void)swipeLeftDetected
{
if(fileNumber > 0)
{
fileNumber--;
NSString *urlString = [NSString stringWithFormat:#"http://172.161.331.25/casa/admmathematic/Page00%d.html",fileNumber];
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
[webview loadRequest:request];
}
else
{
//No more files
}
}