UIWebView Load URL with parameters - iphone

I have an application that stores the user ID and code in NSUSERDEFAULTS I need to use these values in order to get UIWebView to visit url: www.mywebsite.com/check.php?id=(idhere)&code=(idhere).
I have looked at some other topics but nothing has seemed to help so far.
I'm new to this language so thanks for your patience.
Jack Brown.

Simplest form:
- (void) viewDidLoad
{
[super viewDidLoad];
UIWebView *webView = [UIWebView alloc] initWithFrame:self.view.frame];
[self.view addSubview:webView];
NSString *url = #"http://google.com?get=something&...";
NSURL *nsUrl = [NSURL URLWithString:url];
NSURLRequest *request = [NSURLRequest requestWithURL:nsUrl cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:30];
[webView loadRequest:request];
}

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *userId= [prefs stringForKey:#"userIdkey"];
NSString *code= [prefs stringForKey:#"codeKey"];
and so your url string will be,
NSString *urlStr = [NSString stringWithFormat:#"http://www.mywebsite.com/check.php?id=%#&code=%#",userId,code];

Related

IOS - UIWebview - Get the URL of the link clicked and edit the url

I'm creating an app which has a webview. When user click to a link or a button in the webview, I want to be able to get the new url, and edit the new url.
First I initialize the webview with the following url:
- (void)viewDidLoad
{
NSString *id = #"12212323";
[super viewDidLoad];
NSString *fullURL = [[NSString alloc] initWithFormat:#"www.website.com/index.php?id=%#", id];
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
}
When I click to a link, for example www.website.com/contact.php
Then I edit the new url with www.website.com/contact.php*?id=12212323*
I need to keep the id parameter in all the urls.
The UIWebViewDelegateProtocol should suit your requirements.
In particular the method - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
this is a code to get the current URL
- (void)viewDidLoad
{
NSString *id = #"12212323";
[super viewDidLoad];
NSString *fullURL = [[NSString alloc] initWithFormat:#"www.website.com/index.php?id=%#", id];
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
NSURL *currentURL = [requestObj URL];
NSLog(#"Current URL is %#", currentURL.absoluteString);
}
but to create a URL bar hidden or not is a lot of code, you can start from this tutorial on YouTube is simple and work very well, is possible give you other idea to make a good job ;)
Web Browser Tutorial
hope this help you

Display local pdf file in UIWebView in iphone

I am display local pdf file in UIWebView but it shows exception in path here is my code
NSString*fileName=content_Source;
NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:#"pdf"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView setScalesPageToFit:YES];
[webView loadRequest:request]
You must be using the file name extension while setting the file name. So make sure the extension will not be used in file name if it's setting along with Type (like ofType:#"pdf").
Here's my code:
UIWebView *webView = [[UIWebView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
webView.backgroundColor = [UIColor redColor];
NSString*fileName= #"About_Downloads";
NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:#"pdf"];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:path isDirectory:NO]];
[webView setScalesPageToFit:YES];
[webView loadRequest:request];
[self.view addSubview:webView];
There's a better way to show PDFs:
UIDocumentInteractionController *dc = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:path isDirectory:NO]];
dc.delegate = self;
[dc presentPreviewAnimated:YES];
Make sure use the delegate along with that code to work it properly.
(UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller{
return self;
}
first validate that string and the assign to URL like bellow..
NSString *strURLPath = [self trimString: path];
NSURL *url = [NSURL fileURLWithPath:strURLPath];
use my bellow method...
-(NSString*) trimString:(NSString *)theString {
if (theString != [NSNull null])
theString = [theString stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
return theString;
}
It is working fine with the given code problem was that in my file Name i was giving also the type like ali.pdf they type pdf so it get exception it worked fine now when i use filename like following
NSString*fileName=#"ali";
Instead of
NSString*fileName=#"ali.pdf";
CGRect rect = [[UIScreen mainScreen] bounds];
CGSize screenSize = rect.size;
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,screenSize.width,screenSize.height)];
NSString *path = [[NSBundle mainBundle] pathForResource:#"pdf" ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
[self.view addSubview:webView];

reload a UIwebview from main view automatically (view based application)

I have a view based application. Now on the opening view I have some buttons and a picture and a small web view.
The web view has its own .h/.m file the calls a JSON request to fill it. That works great.
My problem is that when the app is closed and reopened the webview is not updating. How to I get that to work?
welcomeMessage.m (connected to webview)
- (void)awakeFromNib{
[NSThread sleepForTimeInterval:1];
NSUserDefaults *gMess =[NSUserDefaults standardUserDefaults];
NSString *myMess=[gMess stringForKey:#"welcomeMessage"];
NSLog(#"WEBVIEW CLASS %#",myMess);
if (myMess == NULL) {
NSString *html = [NSString stringWithFormat:#"<body style ='background-color:#FFFF33' align='center'><p>Welcome</p><p>Check out our Daily Winners</p></body>"];
[welcomeMessage loadHTMLString:html baseURL:[NSURL URLWithString:#"http://www.myapp.com/api/welcome/welcomemessage.php?iappid=37"]];
}
else{
NSString *html = [NSString stringWithFormat:#"<body style ='background-color:#FFFF33' align='center'> %# </body>", myMess];
[welcomeMessage loadHTMLString:html baseURL:[NSURL URLWithString:#"http://www.myapp.com/api/welcome/welcomemessage.php?iappid=37"]];
}
}
Mainviewcontroller json
- (void)viewDidLoad
{
// Create new SBJSON parser object
SBJsonParser *object = [[SBJsonParser alloc] init];
// Prepare URL request to download statuses from Twitter
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.kickintheapp.com/api/welcome/welcomemessage.php?iappid=37"]];
// NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:#"http://twitter.com/statuses/public_timeline.json"]];
// Perform request and get JSON back as a NSData object
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
// Get JSON as a NSString from NSData response
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
NSError *jsonParseError;
NSDictionary *status = [object objectWithString:json_string error:&jsonParseError];
if (!status) {
// there's been a parse error; look at jsonParseError
// for example:
NSLog(#"JSON parse error: %#", jsonParseError);
}
NSString *messValue = [status objectForKey:#"message"];
NSUserDefaults *gMess = [NSUserDefaults standardUserDefaults];
[gMess setObject:messValue forKey:#"welcomeMessage"];
}
Check out Apple's documentation on an iOS's lifecycle. You will find all callbacks you need in there:
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIApplicationDelegate_Protocol/Reference/Reference.html
Especially – applicationWillEnterForeground: will be interesting for you.

How to implement google search application in iphone

I am new to iPhone development. I am making an application where I need to implement Google Search application and display the results below...
Can anyone please help if there is any tutorial to implement this application. Is there any tutorial?
I think the code given below is right, if so then what else should be added to make it work?
Thank you
NSLog(#"%#",searchBarGoogle.text);
NSString *string1 = [[NSString alloc] initWithFormat:#"%#",searchBarGoogle.text];
NSString *string = [NSString stringWithFormat:#"http://www.google.com/search?q=%#",string1];
NSURL *url = [NSURL URLWithString:string];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
in your .h file
#import <UIKit/UIKit.h>
#interface eddwedViewController : UIViewController {
IBOutlet UITextField *searchtextField;
}
-(IBAction)search;
#end
In your .m file
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
-(IBAction)search
{
NSString *searchString=searchtextField.text;
NSString *urlAddress = [NSString stringWithFormat:#"http://www.google.com/search?q=%#",searchString];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlAddress]];
}
In your view controller.xib make a textfield connect delegate with file owners and outlet with its name.
Make a button and connect it action with search method.
Let me know if it is working for you.
try this:-
NSString *searchString=#"apple";
NSString *urlAddress = [NSString stringWithFormat:#"http://www.google.com/search?q=%#",searchString];
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
It is working fine.Hope It will help you.
what you can do is:-
NSString *searchString = searchBarGoogle.text;

website not upload using webview

ihave write the following code
but it does nothing in consloe it shows Unknown scheme, doing nothing
[webview setDeleagte self];
[webView loadRequest:[[NSURLRequest alloc] initWithURL:[[NSURL alloc] initWithString:sourcelinkStr]]];
NSString *strURL = #"https://facebook.com";
NSURL *url = [NSURL URLWithString:strURL];
webView.delegate = self;
[webView loadRequest:[NSURLRequest requestWithURL:url]];
now ,
we have to define Methods For LoadRequest
thanks..