I have one webview and 3 url.
So when application starts i am showing URL1 in webview.
Now when i select any portion of webview it will redirect to URL2.
But only i want to fetch some data from URL2 and dont want to show it to user.
Which i can able to do by using shouldStartLoadWithRequest: method with return NO.
but Now i need to show URL 3 with data received from URL2 in my Webview.
But it is not showing anything ,how can i do it ?
For this i am using following code
-(void)viewDidLoad
{
//Normal showing of URL1 in webview
}
- (BOOL)webView:(UIWebView*)webViewRef shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
{
if(selectedDataExist){
//get data from URL2
//Make New URL3 string
[webView loadRequest: [NSURLRequest requestWithURL:[NSURL URLWithString:myNewUrlString]]];
return NO;
}
else
{
//by default URL1 comes
return YES;
}
i did this:
i was trying to send a info via GET method so i substring the last 10 characters (in my case) of any url requested, if it doesn't have the GET method, it make a new request appending the GET method to the url and return NO, next time this function get called it will have the GET method so it will continue.
-(BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
NSString *urls = [request.URL absoluteString];
NSString *code = [urls substringFromIndex: [urls length] - 10];
if (![code isEqualToString:#"?iphone=si"]) {
NSURL *nueva = [NSURL URLWithString:[NSString stringWithFormat:#"%#?iphone=si",urls]];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:nueva];
[self.mywebview loadRequest:requestObj];
return NO;
}
return YES;
}
This is how I do it
NSURL *LocalUrl = [[NSURL alloc] initWithString:[newUrl stringByAddingPercentEscapesUsingEncoding: NSASCIIStringEncoding]];
NSURLRequest *objNSURLRequest;
objNSURLRequest = [NSURLRequest requestWithURL:LocalUrl];
[yourwebview loadRequest:ObjNSURLRequest];
[LocalUrl release];
return NO;
Related
I am trying to authorize LinkedIn with iPhone..
I am using following code to redirect url
NSString *authUrl = [NSString stringWithFormat:#"https://www.linkedin.com/uas/oauth2/authorization?response_type=code&client_id=%#&scope=%#&state=%#&redirect_uri=%#" ,
API_KEY ,
#"r_fullprofile",
#"ASDKASIIWER23432KKQ",
#"http://www.myappname.com"
];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: authUrl]];
In my Url types, i have added URL Scheme as http:// and url identifier as
www.myappname.com
however after authorizing , i don't get back to my application from browser.
Any ideas where i am wrong?
i have used a diff approach now, i have used webView inside app and is using it. Works perfect so far.
- (void)viewDidLoad
{
[super viewDidLoad];
[self.myWebView setDelegate:self];
self.indicator = [[CustomActivityViewer alloc] initWithView:self.view];
NSString *authUrl = [NSString stringWithFormat:#"https://www.linkedin.com/uas/oauth2/authorization?response_type=code&client_id=%#&scope=%#&state=%#&redirect_uri=%#" ,
API_KEY ,
#"r_fullprofile rw_nus r_emailaddress r_network w_messages",
SCOPE_CODE
REDIRECT_URI
];
authUrl = [authUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[self.myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:authUrl]]];
}
-(void)webViewDidStartLoad:(UIWebView *)webView
{
[self.indicator startAnimating];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[self.indicator stopAnimating];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
[self.indicator stopAnimating];
}
- (BOOL) webView: (UIWebView *) webView shouldStartLoadWithRequest: (NSURLRequest *) request navigationType: (UIWebViewNavigationType) navigationType
{
NSURL *url = request.URL;
NSLog(#"%#", url.absoluteString);
if ( [url.host isEqualToString:HOST])
{
URLParser *parser = [[URLParser alloc] initWithURLString:url.absoluteString];
NSString *code = [parser valueForVariable:#"code"];
if (code != nil)
{
NSString *authUrl = [NSString stringWithFormat:#"https://www.linkedin.com/uas/oauth2/accessToken?grant_type=authorization_code&code=%#&redirect_uri=%#&client_id=%#&client_secret=%#",
code,
REDIRECT_URI_OAUTH,
API_KEY,
SECRET_KEY];
NSLog(#"%#" , authUrl);
authUrl = [authUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[Utilities responseFromURL:[NSURL URLWithString:authUrl] completionBlock:^(NSString *response, NSError *err)
{
if (err != nil)
{
[Utilities errorDisplay];
}
else
{
NSDictionary *results = [response JSONValue];
[defaults setObject:[results objectForKey:#"access_token"] forKey:#"access_token"];
}
}];
}
}
return YES;
}
You are going to need to register a custom URL scheme, as iOS and OS X don't have a way to redirect a specific host within a URL scheme.
Generally, you can use something like x-myapp: as the url scheme.
Further, the URL Identifier is not a host, but an identifier that describes the URL scheme, much like a UTI identifier for a file type describes a specific file type. For example, you could use com.myCompany.myApp.url or something similar as the identifier.
You should be fine if you create a scheme of form x-myapp: and then use that as the redirect URL.
An example from a proposed Info.plist would be:
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLName</key>
<string>com.myCompany.myApp.url</string>
<key>CFBundleURLSchemes</key>
<array>
<string>x-myapp</string>
</array>
</dict>
The CFBundleURLName corresponds in the Xcode GUI to URL Identifier.
Hey guys in my APP i have created custom class for cache by inheriting NSURLCache class and implemented required methods to pass my local files data as cached data.I have done it same as this tutorial
In my APP there is one refresh button,on click of it i have to refresh the current page for this i have done following to load locally stored CSS and JS files
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
if (isGoingToRefresh) {
[NSURLCache setSharedURLCache:cache];
isGoingToRefresh = NO;
return YES;
}
NSString *path = [mainWebView stringByEvaluatingJavaScriptFromString:#"window.location.pathname"];
path = [NSString stringWithFormat:#"%#%#",[[[AppDelegate sharedAppDelegate]configurationFile]objectForKey:#"RootURL"],path];
[NSURLCache setSharedURLCache:cache];
if ([path isEqualToString:[[request URL]absoluteString]]) {
[self showRefreshLoader];
isGoingToRefresh = YES;
NSURLRequest *localRequest = [NSURLRequest requestWithURL:[request URL] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:240] ;
[webView loadRequest:localRequest];
return NO;
}
return YES;
}
Above code is loading data from cache by calling following NSURLCache method -
- (NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request
But on iOS4 webView not calling above NSURLCache method.
I am not getting why please suggest me some good solution for it.
try using to make your request
NSURLRequest *theRequest = [NSURLRequest requestWithURL:url
cachePolicy:NSURLCacheStorageAllowed
timeoutInterval:60];
I want my app to recognize if the current contents of the Clipboard are a URL, and if it is, I want it to load that URL into a web-view.
I'm using the following statement to do this checking:
if ([pasteboard containsPasteboardTypes: [NSArray arrayWithObject:#"public.url-name"]])
...code to load the URL into the webView
but its not working - the IF statement always returns as FALSE, even when the clipboard's contents are clearly a URL.
Strangely enough though, when I remove this IF statement and just go ahead and load the URL I read from the Clipboard into the webView it works perfectly well. So its definitely just the IF statement that's not working for some reason.
Here's the full code:
// executed on a Button-click:
-(IBAction) showClipBoard {
pasteboard = nil; // resetting the pasteBoard each time
pasteboard = [UIPasteboard generalPasteboard];
NSURL *tempURL = pasteboard.URL;
//Check the pasteboard's value-type:
if ([pasteboard containsPasteboardTypes: [NSArray arrayWithObject:#"public.url-name"]]) {
NSLog(#"URL is: %#", pasteboard.string);
NSURL *url = [NSURL URLWithString: pasteboard.string];
NSURLRequest *req = [NSURLRequest requestWithURL: url];
[webView loadRequest:req];
}
else
NSLog(#"=NOT a valid web-address");
NSString *tempString = [pasteboard valueForPasteboardType:#"public.utf8-plain-text"];
// Show the URL in a text-view box called "clipText":
clipText.text = tempString;
}
Anyone see what's wrong here?
You've probably figured this out by now, but, I ran into the same issue & the following worked for me :-
if ([pasteboard containsPasteboardTypes: [NSArray arrayWithObject:#"public.url"]])
...code to load the URL into the webView
I want to open an iTunes link in my webView, but when the webView launches the page, it redirects to the Safari browser. There, the url is getting opened, but I want it to open in my webView.
- (void)viewDidLoad {
NSString *urlAddress = #"http://itunes.apple.com/us/app/foodcheck-traffic-light-nutrition/id386368933?mt=8";
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[webView loadRequest:requestObj];
[super viewDidLoad];
}
Please suggest a way to sort out this problem.
You may want to try logging the load requests when you run the app. It may be that apple is automatically changing http:// to itms-apps or http://phobos or something along these lines. If so, then you can block the load when it's call using something like this:
- (BOOL)webView:(UIWebView *)webView
shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType;
{
NSURL *loadURL = [[request URL] retain];
NSLog(#"%#",loadURL);
if([[loadURL absoluteString] hasPrefix:#"http://"])
{
[loadURL release];
return TRUE;
}
[loadURL release];
return FALSE;
}
Good luck. I'm curious to know what finally works.
A note from the Apple reference documents- Q: How do I launch the App Store from my iPhone application? Also, how do I link to my application on the store?
Note: If you have iTunes links inside
a UIWebView, you can use this
technique after intercepting the links
with the -[UIWebViewDelegate webView:shouldStartLoadWithRequest:navigationType:]
delegate method.
Not sure if you ever got it working, but this works well for me:
NSString *responseString = [NSString stringWithContentsOfURL:myURL];
[self.webView loadHTMLString:responseString baseURL: nil)];
I have a WebView loading a Twitter mobile user's wall:
NSString *urlAddress = [NSString stringWithString:#"http://mobile.twitter.com/gatrecords"];
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[myWebView loadRequest:requestObj];
When web is loaded it freezes for 10- 15 secs then I can navigate normally. On the iPhone's Safary Browser happens the same. That behavior only happens with loading mobile.twitter plus a user's wall.
On the simulator all is ok.
Any suggostions on what could be wrong ?
I found a workaround for the freezeing. Apparently WebView freezed bacause tries to execute twitter's javascript for 10 seconds, after that time the system will stop trying and the WebView will be scrollable. So I stopLoading only when twitter's web is being loaded:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSString *twitter = [NSString stringWithFormat:#"%#", [[myWebView request] URL]];
if ([twitter isEqualToString:#"http://mobile.twitter.com/gatrecords"]) {
[myWebView stopLoading];
}
}
Then I get an error alert after the 10 seconds regarding javascript did not load correctly, I fix that as follows:
- (void)webViewDidStartLoad:(UIWebView *)webView {
[myWebView stringByEvaluatingJavaScriptFromString:#"window.alert=null;"];
}