How do I load html into a webview? - xcode

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)
}

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 display a image when there is no internet connection (web view)

I have a web view based program I was just testing. How do you display a image when there is no internet connection?
I have a basic webview
//
// XYZViewController.m
// Phantomore
//
// Created by Kevin Jin on 5/15/14.
// Copyright (c) 2014 Kevin Jin. All rights reserved.
//
#import "XYZViewController.h"
#interface XYZViewController ()
#end
#implementation XYZViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *fullURL = #"http://www.phantomore.com";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_viewWeb loadRequest:requestObj];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (BOOL)prefersStatusBarHidden
{
return YES;
}
#end
Thats it,I just want to know what I have to do to have it displaying a image when tehre is no internet connection
I just simplified mamnun answer(Get Reachability here).
You can use this method after you have implemented the reachability class
- (BOOL)isConnectedToInternet
{
Reachability *reachability = [Reachability reachabilityForInternetConnection];
NetworkStatus networkStatus = [reachability currentReachabilityStatus];
return !(networkStatus == NotReachable);
}
In any of your class
- (void)viewDidLoad
{
[super viewDidLoad];
if([self isConnectedToInternet]){
//Hide your imageview
NSString *fullURL = #"http://www.phantomore.com";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_viewWeb loadRequest:requestObj];
}
else{
// Hide your webview
// Show your imageview
// Do not forget to import reachability class on top
}
}
There is two way of doing it.
Before even performing the url request, check if internet connection is available. Use Apple provided Reachability classes or better yet use the open-source version. If internet is available load the request. If not add an UIImageView with your preferred UIImage as a subview of the webview. You can do something like this:
//inside #interface
#property(nonatomic, strong) UIImageView *imageView;
//inside -(void)viewDidLoad
self.imageView = [UIImageView alloc] initWithImage:[UIImage imageNamed:#"as.as"];
Reachability* reach = [Reachability reachabilityWithHostname:#"www.google.com"];
reach.reachableBlock = ^(Reachability*reach) {
// load request
NSString *fullURL = #"http://www.phantomore.com";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_viewWeb loadRequest:requestObj];
};
reach.unreachableBlock = ^(Reachability*reach) {
//show imageview
[_viewWeb addSubView:self.imageView];
};
[reach startNotifier];
Implement UIWebView delegate. In the – webView:didFailLoadWithError: method, check the error. If the error is caused by no internet connection, add the imageview(same as before).

send URL to detail view with button selection in master view

I am trying to make master-detail view with generic webview in detail where the URL changes with selection of button in master view controller. Obviously I'm a novice and I need help!
in master view mfile I send through button like this:
-(IBAction)myButton1:(UIButton *) sender {
[self.detailVC location:#"http://google.com"];
}
-(IBAction)myButton2:(UIButton *) sender {
[self.detailVC location:#"http://yelp.com"];
}
in detail view mfile I have:
-(void)location(NSString *)myLocation{
_theLocation = myLocation;
}
-(void) viewDidLoad {
[super viewDidLoad];
NSURL *videoURL = [NSURL URLWithString:self.theLocation];
NSURLRequest *request = [NSURL Request requestWithURL:videoURL];
[self.webview loadRequest:request];
}
I am getting no build errors, but none of the URL's are loading. Please help!
You should know - (void)viewDidLoad Called after the controller’s view is loaded into memory , in your issue, it seems the self.detailVC is always alive in memory, that should not call viewDidLoad.
you can update the web view in - (void)setLocation(NSString *)myLocation directly:
- (void)setLocation(NSString *)myLocation
{
_location = myLocation;
NSURL *videoURL = [NSURL URLWithString:self.location];
NSURLRequest *request = [NSURL Request requestWithURL:videoURL];
[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];
}

EXC_BAD_ACCESS issue when I'm trying to load View

- (void)viewDidLoad {
NSString *request=[NSString stringWithFormat:#"http://blablabla"];
[self parseXMLFileAtURL:request];
[super viewDidLoad];
}
It might be that you need to send NSURL instead of NSString.
Try with
NSURL *request = [NSURL URLWithString:#"http://blablabla"];

Resources