My UIWebView doesn't seem to be loading:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *urlAddress = [defaults stringForKey:#"webPage"];
NSLog(#"%#", urlAddress);
NSURLRequest *requestObj = [NSURLRequest requestWithURL:[NSURL URLWithString:urlAddress]];
webView.delegate = self;
[webView loadRequest:requestObj];
}
It is in a UIViewController (connected through IB) and urlAddress returns google.com
Can you check if the URL being fetched is valid?
NSString *urlAddress = [defaults stringForKey:#"webPage"];
NSLog(#"%#", urlAddress);
NSURL *u = [NSURL URLWithString:urlAddress];
if(u){
NSURLRequest *requestObj = [NSURLRequest requestWithURL:u];
webView.delegate = self;
[webView loadRequest:requestObj];
}else{
[[[UIAlertView alloc] initWithTitle:#"" message:#"invalid url." delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil] show];
}
google.com is not a valid url, it should be http://www.google.com. So maybe that is the issue here.
Try this code:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
webView.delegate = self;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *urlAddress = [defaults stringForKey:#"webPage"];
NSLog(#"%#", urlAddress);
[self openURLFromString:urlAddress];
}
- (void) openURLFromString:(NSString*) urlString
{
NSURL *url = [self validateAddress:urlString];
NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:url];
[webView loadRequest:request];
}
- (NSURL*) validateAddress:(NSString*) address
{
NSURL* result = [NSURL URLWithString:address];
if (!result.scheme)
{
NSString* modifiedURLString = [NSString stringWithFormat:#"http://%#", address];
result = [NSURL URLWithString:modifiedURLString];
}
return result;
}
#pragma mark - UIWebViewDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSMutableURLRequest *)urlRequest navigationType:(UIWebViewNavigationType)navigationType
{
return YES;
}
You're missing base URL part of the URL ( Click here to read more about this ), which is necessary.
So try this:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *urlAddress = [defaults stringForKey:#"webPage"];
NSString * theURL = [NSString stringWithFormat:#"http://%#", urlAddress]; // you can also use stringByAppendingString if you prefer
NSLog(#"%#", theURL);
NSURLRequest *requestObj = [NSURLRequest requestWithURL:[NSURL URLWithString:theURL]];
webView.delegate = self;
[webView loadRequest:requestObj];
}
HTH :)
Related
I am implementing instagram login, with client id and all credential. From browser it's returning access token but in my code it is showing 403 error with message "You are not sandbox user of this client".from manage client in developer account I have unchecked the disable implicit option but still from my app getting this error.
any help ??
my code is like
- (void)viewDidLoad
{
[super viewDidLoad];
_webview = [[UIWebView alloc]init];
_webview.frame = self.view.frame;
_webview.delegate = self;
[self.view addSubview:_webview];
NSString *scopeStr = #"scope=comments+relationships";
if ([_typeOfAuthentication isEqualToString:#"UNSIGNED"])
{
NSString *url = [NSString stringWithFormat: #"%#?client_id=%#&redirect_uri=%#&response_type=token&scope=%#&DEBUG=True",
KAUTHURL,
KCLIENTID,
kREDIRECTURI,
INSTAGRAM_SCOPE];
[_webview loadRequest: [NSURLRequest requestWithURL: [NSURL URLWithString: url]]];
[_webview setDelegate:self];
} else {
NSString *url = [NSString stringWithFormat: #"%#?client_id=%#&redirect_uri=%#&response_type=code&scope=%#&DEBUG=True",
KAUTHURL,
KCLIENTID,
kREDIRECTURI,
INSTAGRAM_SCOPE];
[_webview loadRequest: [NSURLRequest requestWithURL: [NSURL URLWithString: url]]];
[_webview setDelegate:self];
}
}
when there are connection i load data from the url direct , if there are no Internet i get the data from cache (offline mode) i wrote this code
NSURL *url = [NSURL URLWithString:_url];
NSURLRequest * reqyest = [NSURLRequest requestWithURL:url];
if ([[NSUserDefaults standardUserDefaults]objectForKey:#"data"]) {
NSData * yourData = [[NSUserDefaults standardUserDefaults]objectForKey:#"data"];
NSString *html = [[NSString alloc] initWithData:yourData encoding:NSUTF8StringEncoding];
[_webview loadHTMLString:html baseURL:nil];
NSLog(#"iam here");
}
else
{ [_webview loadRequest:reqyest];
NSCachedURLResponse* response = [[NSURLCache sharedURLCache]
cachedResponseForRequest:reqyest];
NSData* data = [response data];NSLog(#"%#",data);
[[NSUserDefaults standardUserDefaults]setObject:data forKey:#"data"];
[[NSUserDefaults standardUserDefaults]synchronize];
NSLog(#"herer erege");
}
I have created an app which is a web browser. Whenever I enter any URL in text field it goes to the required web page. I have included an UIWebView to display webpage. By default google website is opened. But when I select any website from google I want to display that URL in the textfield. How do I do it?
- (void)viewDidLoad
{
[super viewDidLoad];
self.myTextField.text = #"http://www.google.com";
NSURL *myURL = [NSURL URLWithString:#"http://www.google.com"];
NSURLRequest *myRequest = [NSURLRequest requestWithURL:myURL];
[_myWebView loadRequest:myRequest];
self.myTextField.delegate = self;
}
You can do like this
- (void)viewDidLoad
{
[super viewDidLoad];
self.myTextField.text = #"http://www.google.com";
NSURL *myURL = [NSURL URLWithString:#"http://www.google.com"];
NSURLRequest *myRequest = [NSURLRequest requestWithURL:myURL];
[_myWebView loadRequest:myRequest];
_myWebView.delegate = self;
self.myTextField.delegate = self;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView;
{
NSURL *requestURL = [webView.request URL];
txtView.text = [requestURL absoluteString];
}
I am getting an error in Xcode when adding some labelLoading code in the .m file. Hoping someone can help me out and point out the error in the code. Don't beat me up too bad as I am learning. I just can't get past this error.
#interface ViewController ()
#end
#implementation ViewController
#synthesize viewWeb;
#synthesize labelLoading;
#synthesize tqwWeb;
#synthesize blogWeb;
#synthesize eventsWeb;
#synthesize resWeb;
#synthesize homeWeb;
- (void)viewDidLoad
{
[viewWeb setDelegate:self];
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
[labelLoading setHidden:NO];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[labelLoading setHidden:YES];
}
{ **This is where the error Expected identifier or "(" occurs**
[super viewDidLoad];
NSString *fullURL = #"http://www.rtics.com/DC1/";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[viewWeb loadRequest:requestObj];
[super viewDidLoad];
NSString *afullURL = #"http://www.rtics.com/DC2";
NSURL *aurl = [NSURL URLWithString:afullURL];
NSURLRequest *arequestObj = [NSURLRequest requestWithURL:aurl];
[tqwWeb loadRequest:arequestObj];
[super viewDidLoad];
NSString *bfullURL = #"http:/www.rtics.com/DC3/";
NSURL *burl = [NSURL URLWithString:bfullURL];
NSURLRequest *brequestObj = [NSURLRequest requestWithURL:burl];
[blogWeb loadRequest:brequestObj];
[super viewDidLoad];
NSString *cfullURL = #"https://www.rtics.com/DC4";
NSURL *curl = [NSURL URLWithString:cfullURL];
NSURLRequest *crequestObj = [NSURLRequest requestWithURL:curl];
[eventsWeb loadRequest:crequestObj];
[super viewDidLoad];
NSString *dfullURL = #"http://www.rtics.com/DC5";
NSURL *durl = [NSURL URLWithString:dfullURL];
NSURLRequest *drequestObj = [NSURLRequest requestWithURL:durl];
[resWeb loadRequest:drequestObj];
[super viewDidLoad];
NSString *efullURL = #"http://www.rtics.com/";
NSURL *eurl = [NSURL URLWithString:efullURL];
NSURLRequest *erequestObj = [NSURLRequest requestWithURL:eurl];
[homeWeb loadRequest:erequestObj];
}
- (void)viewDidUnload
{
[self setViewWeb:nil];
[self setLabelLoading:nil];
[self setWebView:nil];
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation(UIInterfaceOrientation)interfaceOrientation
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} else {
return YES;
}
}
#end
The line before your error should have - (void)viewDidLoad and you should only call [super viewDidLoad] once. Move anything in your other viewDidLoad to this new method and delete your other viewDidLoad.
For example:
- (void)viewDidLoad
{ **This is where the error Expected identifier or "(" occurs**
[super viewDidLoad];
[viewWeb setDelegate:self]; // Moved from previous viewDidLoad method
NSString *fullURL = #"http://www.rtics.com/DC1/";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[viewWeb loadRequest:requestObj];
NSString *afullURL = #"http://www.rtics.com/DC2";
NSURL *aurl = [NSURL URLWithString:afullURL];
NSURLRequest *arequestObj = [NSURLRequest requestWithURL:aurl];
[tqwWeb loadRequest:arequestObj];
NSString *bfullURL = #"http:/www.rtics.com/DC3/";
NSURL *burl = [NSURL URLWithString:bfullURL];
NSURLRequest *brequestObj = [NSURLRequest requestWithURL:burl];
[blogWeb loadRequest:brequestObj];
NSString *cfullURL = #"https://www.rtics.com/DC4";
NSURL *curl = [NSURL URLWithString:cfullURL];
NSURLRequest *crequestObj = [NSURLRequest requestWithURL:curl];
[eventsWeb loadRequest:crequestObj];
NSString *dfullURL = #"http://www.rtics.com/DC5";
NSURL *durl = [NSURL URLWithString:dfullURL];
NSURLRequest *drequestObj = [NSURLRequest requestWithURL:durl];
[resWeb loadRequest:drequestObj];
NSString *efullURL = #"http://www.rtics.com/";
NSURL *eurl = [NSURL URLWithString:efullURL];
NSURLRequest *erequestObj = [NSURLRequest requestWithURL:eurl];
[homeWeb loadRequest:erequestObj];
}
I'm following foursquare authen guideline but I still have a problem
this is my code
- (void)viewDidLoad
{
[super viewDidLoad];
self.webView.delegate = self;
NSString *clientID = #"XXX";
NSString *redirectURI = #"http://www.example.com";
NSString *authenticateURLString = [NSString stringWithFormat:#"https://foursquare.com/oauth2/authenticate?client_id=%#&response_type=token&redirect_uri=%#", clientID, redirectURI];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:authenticateURLString]];
[self.webView loadRequest:request];
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if ([request.URL.scheme isEqualToString:#"itms-apps"]) {
[[UIApplication sharedApplication] openURL:request.URL];
return NO;
}
return YES;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSString *URLString = [[self.webView.request URL] absoluteString];
NSLog(#"--> %#", URLString);
if ([URLString rangeOfString:#"access_token="].location != NSNotFound) {
NSString *accessToken = [[URLString componentsSeparatedByString:#"="] lastObject];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:accessToken forKey:#"access_token"];
[defaults synchronize];
[self dismissViewControllerAnimated:YES completion:nil];
}
}
but output from NSLog is only http://www.example.com. It has no access token return. Am i do anything wrong?
I'm open this link in Desktop Google Chrome and access token is successfully return. (www.example.com#access_token=XXX)
But I'm open this link in iPhone simulator's safari and it just return the url.
Thanks for help.
#nearonline
I have the same problem, and the reason is the URL in webViewDidFinishLoading will not contain the access token. But the URL in shouldStartLoadWithRequest will. Therefore you should modify the code into:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSString *URLString = [[request URL] absoluteString];
if ([URLString rangeOfString:#"access_token="].location != NSNotFound) {
NSString *accessToken = [[URLString componentsSeparatedByString:#"="] lastObject];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:accessToken forKey:#"access_token"];
[defaults synchronize];
[self dismissViewControllerAnimated:YES completion:nil];
return NO;
}
return YES;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
}