Dynamic UIWebView Link - xcode

So normally to load a UIWebView I do..
NSURL *url = [NSURL URLWithString:#"http://www.google.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[_webView loadRequest:request];
However, now I need to do this dynamically, depending on a table cell that a user clicks. I load the table cells from JSON and everything works fine. Now I need to get the "id" of the post in the cell, and load it dynamically. Currently I've tried this but it doesn't work...
NSURL *url = [NSURL URLWithString:#"http://www.google.com/story.php?id="];
url = [NSURL URLWithString:storyId relativeToURL:url];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[_webView loadRequest:request];
Any tips?

Why don't you just format the URL string?
NSURL *url = [NSURL URLWithString:[NSString
stringWithFormat:#"http://www.google.com/story.php?id=%#", storyId]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[_webView loadRequest:request];

Related

App terminates when using NSURL with parameters

In my app I sent a request to my PHP file to download some info. Whenever I use the code below it will terminate:
-(void)downloadItems
{
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *myString = [prefs stringForKey:#"commentsId"];
NSLog(#"The comments id is : %#",myString);
// Download the json file
NSString *urlstring = [NSString stringWithFormat:#"http:/MyWebSite.com/checkphp.php?title=%#", myString];
NSURL *jsonFileUrl = [NSURL URLWithString:urlstring];
NSLog(#"The qasida id is: %#", jsonFileUrl);
// Create the request
NSURLRequest *urlRequest = [[NSURLRequest alloc] initWithURL:jsonFileUrl];
// Create the NSURLConnection
[NSURLConnection connectionWithRequest:urlRequest delegate:self];
}
But if I remove myString from urlString like this:
NSURL *jsonFileUrl = [NSURL URLWithString:#"http:/myWebSite/checkphp.php?title=%#"];
the app will not terminate and it will retrieve data.
Can any one tell me what is wrong with my code?
Thanks

how to load pdf file in uiwebview from document directory in xcode?

i'm downloading my pdf file in document directory and then trying to show that pdf file
from that path to load in uiwebview but its not opening just a blank white page, i dont
understand why its not loading in uiwebview.
below is my code ,
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathExtension:#".pdf"];
NSURL *url = [NSURL fileURLWithPath:filePath];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
and my filepath is:
NSLog(#"Filepath------->> %#", filePath);
/Users/zee/Library/Application Support/iPhone Simulator/6.0/Applications/BD79829B-95FF-4B91-94AD-830C6B1CD31D/Documents/myPDF.pdf
Please tell me why its not working and what should be the best solution
Thanks
Zeeshan Shaikh
Try this,
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathExtension:#"sample.pdf"];
NSURL *url = [NSURL fileURLWithPath:filePath];
NSString *urlString = [url absoluteString];
NSString *encodedString=[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *webURL = [NSURL URLWithString:encodedString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];

Error when entering Facebook login in iPhone App

I am attempting to integrate the Facebook API in my iOS app, but am getting the following error:
WebKit discarded an uncaught exception in the
webView:decidePolicyForNavigationAction:request:frame:decisionListener:
delegate: Application tried to present
modally an active controller .
How can I solve this error?
Here is my code:
[_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"about:blank"]]];
NSString *redirectUrlString = #"http://www.facebook.com/connect/login_success.html";
NSString *authFormatString = #"https://graph.facebook.com/oauth/authorize?client_id=%#&redirect_uri=%#&scope=%#&type=user_agent&display=touch";
NSString *urlString = [NSString stringWithFormat:authFormatString, _apiKey, redirectUrlString, _requestedPermissions];
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[_webView loadRequest:request];
For checking the login credential, i am writing the below code inside the function logincheck
if ([urlString rangeOfString:#"login.php"].location != NSNotFound)
{
[_delegate displayRequired];
}

How do I load html into a webview?

I have just re-designed my iPhone app using Storyboard... questin is: how do I address the the UIWebView so I can load the html? (I am a newbie when it comes to Obj-C, having done my previous app in MonoTouch, so I'm kinda lost). :D
- (void)viewDidLoad {
NSString *urlAddress = #”http://www.stackoverflow.com”;
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
}
Apple Developer docs are really good resource. The usage of UIWebView is outlined here enter link description here
To load a network resource simply pass a NSURLRequest object to loadRequest.
[self.myWebView loadRequest:[NSURLRequest
requestWithURL:[NSURL URLWithString:#"http://www.apple.com/"]]];
You can also do this in viewDidLoad (a UIWebView is a subclass of UIView and inherits all its methods). For example, loading a local pdf resource.
- (void)viewDidLoad {
[super viewDidLoad];
NSString *thePath = [[NSBundle mainBundle] pathForResource:#"iPhone_User_Guide" ofType:#"pdf"];
if (thePath) {
NSData *pdfData = [NSData dataWithContentsOfFile:thePath];
[(UIWebView *)self.view loadData:pdfData MIMEType:#"application/pdf"
textEncodingName:#"utf-8" baseURL:nil];
}
}
To setup your UIWebView with a link, just use the following code.
var URLPath = "Insert email address here"
Then use...
loadAddressURL()
and finally...
func loadAddressURL(){
let requestURL = NSURL(string:URLPath)
let request = NSURLRequest(URL: requestURL!)
Webview.loadRequest(request)
}

Session Maintenance for Login Page

My Requirement: I want to send login details(email, password) to server, have to maintain session for valid username.
How to create & maintain a session using "NSURLConnection" of iphone SDK?
If you found any Tutorials/Code snippets on this issue,Please post the corresponding linke here.
Advance thanks for your help.
NSString *urlString = [NSString stringWithFormat:#"http://www.mywebsite.com/login.php?email=username#yahoo.com&pwd=pass"];
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL: url];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection release];
[request release];

Resources