send URL to detail view with button selection in master view - xcode

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];
}

Related

How to share image through WhatsApp?

I'm using this code, but it's not working, how can it fit it?
Anyone help? Thanks!
UIImage *iconImage = [UIImage imageNamed:#"Home01-A01.png"];
NSString *savePath = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents/whatsAppTmp.wai"];
[UIImageJPEGRepresentation(iconImage, 1.0) writeToFile:savePath atomically:YES];
_documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:savePath]];
_documentInteractionController.UTI = #"net.whatsapp.image";
_documentInteractionController.delegate = self;
[_documentInteractionController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
Hi You can share your file on WhatsApp using UIDocumentationView Controller, do the following to achieve the same,
In your viewcontroller.h file,
add Delegate for
UIDocumentInteractionControllerDelegate
declare UIDocumentInteractionController object like this
UIDocumentInteractionController *controller;
In your viewcontroller.m file,
add following Delegate methos for UIDocumentationView controller to track the status for success and failure
pragma mark - Delegate Methods
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller {
return self;
}
- (void)documentInteractionController:(UIDocumentInteractionController *)controller willBeginSendingToApplication:(NSString *)application {
NSLog(#"Starting to send this puppy to %#", application);
}
- (void)documentInteractionController:(UIDocumentInteractionController *)controller didEndSendingToApplication:(NSString *)application {
NSLog(#"We're done sending the document.");
}
add following code to share your file,
-(void)openDocument {
// here's a URL from our bundle
NSURL *documentURL = [[NSBundle mainBundle]URLForResource:#"chat" withExtension:#"png”];// You can set your filename and extention here
// pass it to our document interaction controller
controller = [[UIDocumentInteractionController alloc]init];
controller.delegate = self;
controller.URL = documentURL;
// present the preview
[controller presentPreviewAnimated:YES];
}
Note: the URL that you specify needs to be local.
The goal of the UIDocumentInteractionController is to share (local) documents
between applications.if your file is not stored locally then you can
achive this by storing the file in temp or local caching directory then accessing it from the
main bundle.

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

After viewing web in app, cannot return to app without closing it and relaunching

I have an app which has several different buttons leading to various views. One of the views will open a website in Safari on iPhone which is what I want to happen. THe problem is that if I then want to go back to the app, all I get is a white screen, and I have to double click on home button, close the app and relaunch it.
Would this relate to releasing memory or view perhaps? I'm not sure how to go about fixing it so after viewing the web page in safari I can then go back to the app and continue with other views.
Here is what I am using in webViewController.m
#import "webViewController.h"
#interface webViewController ()
#end
#implementation webViewController
- (void)viewDidLoad
{
NSURL *url = [ [ NSURL alloc ] initWithString: #"http://www.mydomain.com" ];
[[UIApplication sharedApplication] openURL:url];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
you can use webView to load this url, then you can make a back button on this webview to come back to your app without leaving the app.
UIWebView * webView =[[UIWebView alloc]init];
webView.backgroundColor = [UIColor clearColor];
[webView setFrame: CGRectMake(0, 51, 320, 398)];
NSURL *url = [NSURL URLWithString:#"http://www.mydomain.com"];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView setDelegate:self];
webView loadRequest:requestObj];
[self.view addSubview:webView];
You can also use some delegate methods of webView to know whether your page has been loaded or not
-(void)webViewDidStartLoad:(UIWebView *)webView
{
}
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
}
Ah I've figured it out, I've added
web view.scalesPagesToFit = Yes
statement and opening the webpage in the webview works exactly how I wanted it to.

pass url information in segue

I have 2 button set each with a segue to a view controller with a web view. I'm try to load a url in the web view based on which button started the segue.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString: #"toWeb"]){
NSString *abc = #"http://www.abc.com/";
NSURL *url = [NSURL URLWithString:kmb];
NetView *netview =[segue destinationViewController];
[netview.internet loadRequest:[NSURLRequest requestWithURL:url]];
}else if ([segue.identifier isEqualToString: #"toFacebook"]){
NSString *fb = #"http://www.facebook.com/ABC?ref=hl";
NSURL *url2 = [NSURL URLWithString:fb];
NetView *netview =[segue destinationViewController];
[netview.internet loadRequest:[NSURLRequest requestWithURL:url2]];
}
}
This is not working but I'm not getting any error just a bland view on the new screen. I'm not sure what the problem is. Can anyone help point me in the right direction? Thank You.
The problem is that the webview on the destination controller has not been loaded at this point. Just set a property on the destination controller to hold your url and then on its viewDidLoad method send the message to the webview in order to load the request. Something like this:
NetView.h
...
#property (nonatomic, copy) NSString *url;
NetView.m
...
#synthesize url;
Your code snippet:
if ([segue.identifier isEqualToString: #"toWeb"]){
NetView *netview = [segue destinationViewController];
netview.url = #"http://www.abc.com/";
}
...
NetView.m
- (void)viewDidLoad
{
NSURL *urlToLoad = [NSURL URLWithString:self.url];
[self.internet loadRequest:[NSURLRequest requestWithURL:urlToLoad]];
}

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

Resources