View Controller Trouble - ios

I am making a music streaming app but i can only seem to add the controls to the default view controller this is my code for ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController {
IBOutlet UIWebView *WebView;
}
-(IBAction)Play:(id)sender;
#end
and this is my code for viewcontrol.m
-(IBAction)Play:(id)sender{
NSString *stream = #"http://117.53.175.113:15008/listen.pls";
NSURL *url = [NSURL URLWithString:stream];
NSURLRequest *urlrequest = [NSURLRequest requestWithURL:url];
[WebView loadRequest:urlrequest];
}
#end
Please help

Drag you IBOutlet from interface builder and to outside class parentheses {}.
Something like:
#interface ViewController : UIViewController {
IBOutlet UIWebView *WebView; // remove this.
}
#property (unsafe_unretained, nonatomic) IBOutlet UIWebView * WebView;

Related

[iOS]How can I omit protocols?

I'm newbie of iOS
I'm trying to make a simple browsers using UIWebView.
This is my Code.
"ViewController.h"
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (strong, nonatomic) IBOutlet UIWebView *webView;
#property (weak, nonatomic) IBOutlet UITextField *addrInputField;
-(IBAction)enterButton:(id)sender;
/*go back btn*/
-(IBAction)goBackBtn:(id)sender;
/*go front btn*/
-(IBAction)goForwardBtn:(id)sender;
#end
"ViewController.m"
/*url enter button*/
-(IBAction)enterButton:(id)sender{
NSString *str = [_addrInputField text];
NSURLRequest *rqst = [NSURLRequest requestWithURL:[NSURL URLWithString:str]];
[_webView loadRequest:rqst];
}
/*go back button*/
-(IBAction)goBackBtn:(id)sender{
if([self.webView canGoBack]){
[self.webView goBack];
}
}
/*go forward button*/
-(IBAction)goForwardBtn:(id)sender{
if([self.webView canGoForward]){
[self.webView goForward];
}
}
This simple things are all of my code.
Input url in Text Field and touch enterButton then webView is changed.
When I input url in textField like : "http://apple.com", then view is changed.
But input url : "apple.com", view didn't work.
how can I omit the protocol when I input urls?
p.s I wonder how to handling both http and https.
Thanks for reading.
******Add details.
when I pressed Enter..
The Chrome modify my inputs "apple.com" to "https://apple.com" automatically adding protocol "https://".
I want adding this function in my Apps. Thanks.
Once you get the string from your textfield, after creating NSURL, you can retrieve URLScheme from it. If it's nil, you can add http yourself.
NSURL *url = [NSURL URLWithString:str];
if url.scheme == nil {
[NSString stringWithFormat:#"http://%#",str];
url = [NSURL URLWithString:str];
}

How to access instance from another function in objective c?

I am trying to access a webView object from another class.
viewController.m
#property (strong, nonatomic) TOWebViewController *webViewController;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.webViewController = [[TOWebViewController alloc] initWithURLString:[NSString stringWithFormat:#"http://www.%#/", url] ];
[self setViewControllers:#[_webViewController]];
}
- (void)request {
NSURL *url=[NSURL URLWithString:#"http://www.google.com"];
NSURLRequest *request=[NSURLRequest requestWithURL:url];
[_webViewController.webView loadRequest:request];
}
TOWebViewController has the webView property to access the webview. When (void)request is called, something starts loading but it does not load it into the webview that was created in viewDidLoad. How do I have it reference the correct webViewController in (void)request?
I just looked at TOWebviewController on github and it states the following in TOWebViewController.h:
/**
The web view used to display the HTML content. You can access it through this
read-only property if you need to anything specific, such as having it execute arbitrary JS code.
#warning Usage of the web view's delegate property is reserved by this view controller. Do not set it to another object.
*/
#property (nonatomic,readonly) UIWebView *webView;
So the class doesn't sound like you need to interact with it by accessing it directly.
You more than likely want to just set the url:
/**
Get/set the current URL being displayed. (Will automatically start loading)
*/
#property (nonatomic,strong) NSURL *url;
So try:
- (void)request {
NSURL *url=[NSURL URLWithString:#"http://www.google.com"]
_webViewController.url = url;
}
You should try to use childViewController.
The reference document UIViewController Class Reference
Here are the essential methods you might need to call:
addChildViewController:
removeFromParentViewController:
willMoveToParentViewController:
didMoveToParentViewController:
My best guess is what you really want is to display the UIWebView inside the "parent" view controller, and that there is no need for a "child" view controller.
Why not do it like this?
#interface ViewController : UIViewController
#property (weak) IBOutlet UIWebView *webView; // set outlet in IB
#end
#implementation ViewController
// ...
- (void)request {
NSURL *url=[NSURL URLWithString: #"http://www.google.com"];
NSURLRequest *request = [NSURLRequest requestWithURL: url];
[self.webView loadRequest:request];
}
#end

Change from UIWebView to WKWebView

I'm trying to change my UIWebView to WKWebView.
My WebPageViewController just looks this simple on Storyboards:
I'd like to implement the Reading View and back buttons and estimated progress, but I'm not sure if I'm even getting this first step right.
If the first step is right though, and I'm wanting to set up the configuration, am I adding something like this to my code?:
WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init];
And would I need to add a #property (strong, nonatomic) WKWebViewConfiguration *configuration; to get it all working?
Here's my code changes so far if needed:
WebPageViewController.m UIWebView
#property (strong, nonatomic) IBOutlet UIWebView *webView;
-(void)viewWillAppear:(BOOL)animated {
NSURL *url = [NSURL URLWithString:stringWeb];
[self.webView loadRequest:[NSURLRequest requestWithURL:url]];
}
WebViewController.m WKWebView
#property (strong, nonatomic) IBOutlet WKWebView *webView;
-(void)viewWillAppear:(BOOL)animated {
NSURL *url = [NSURL URLWithString:stringWeb];
[self.webView loadRequest:[NSURLRequest requestWithURL:url]];
}
You can instantiate and set the WKWebViewConfiguration on the web view in viewDidLoad without storing it in a property.
- (void)viewDidLoad {
[super viewDidLoad];
WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
// ...
self.webView.configuration = configuration;
}

How can I test my class which uses UIWebView

I want to make test, but I do not know how to make it (I can use GHUnit).
This is the class I want to test:
Communicator.h
#interface Communicator : NSObject
-(void)loadURLWithString:(NSString*)urlString withWebView:(UIWebView*)webView;
#end
Communicator.m
#import "Communicator.h"
#implementation Communicator
##### I WANT TO EXCLUDE THE WEBVIEW BELOW #####
-(void)loadURLWithString:(NSString*)urlString withWebView:(UIWebView*)webView
{
NSURL *url = [[NSURL alloc] initWithString:urlString];
NSURLRequest *r = [[NSURLRequest alloc] initWithURL:url];
[webView loadRequest:r];
}
#end
And this is ViewController which control Communicator class.
ViewController.h
#import "Communicator.h"
#interface ViewController : UIViewController <UITextFieldDelegate>
{
Communicator *communicator;
}
#property (weak, nonatomic) IBOutlet UIWebView *webView;
#property (weak, nonatomic) IBOutlet UIButton *startButton;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
communicator = [[Communicator alloc] init];
[_startButton addTarget:self action:#selector(startButtonPushed) forControlEvents:UIControlEventTouchDown];
}
- (void)startButtonPushed
{
[communicator loadURLWithString:#"http://www.apple.co.jp" withWebView:_webView];
}
#end
Thank you for your help.
What is it exactly you want to test? That pressing the button causes the webview to load a URL or that the webview actually brings the URL content?

Example of WebView tool

I don't know how to create WebView control. I want to display any web page in a WebView, so how can I do this?
First create a view based application, in the View, add a UIWebView, and in the .plist add a Application Uses Wi-Fi key, and set it YES. In the ViewController class' .h file make the code look like this:
#import <UIKit/UIKit.h>
#interface ClassNameHere : UIViewController {
UIWebView *webView;
}
#property (nonatomic, retain) IBOutlet UIWebView *webView;
#end
In the .m file, add this code:
#synthesize webView;
- (void)viewDidLoad {
NSString *urlString = #"http://www.google.com";
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlString];
//URL Requst Object
NSURLRequest *webRequest = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[webView loadRequest:webRequest];
}
Finally, in your nib file, connect the webView outlet to the UIWebView. When you compile, it should open Google.

Resources