I am building an app that would run a website through my app. Basically the webView. I had no problems so far since the last xCode update. It basically runs with no errors but it does not work on iOs 6 simulator or device.
.h fle:
#interface ViewController : UIViewController{
IBOutlet UIWebView *nView;
}
.m file:
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL * myURL = [NSURL URLWithString:#"http://www.google.com"];
NSURLRequest *myRequest = [NSURLRequest requestWithURL:myURL];
[nView loadRequest:myRequest];
}
If anyone can help I would be grateful. Thank you
I managed to get it working!
.h file:
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (weak, nonatomic) IBOutlet UIWebView *myWebView;
#end
.m file:
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *url =[NSURL URLWithString:#"http://www.google.com"];
NSURL*request = [NSURLRequest requestWithURL:url];
[[self myWebView]loadRequest:request];
}
I included a property on h file and with some research I got the .m file working too. Although I got an issue saying:
Incompatible pointer types sending 'NSURL *__strong' to parameter of
type 'NSURLRequest *'
Can anybody help to get rid of this issue as well please?
Related
I have an IOS app that is using RestKit to pull json formatted data from a server into a CoreData Entity. One of the attributes in the data is a URL for the image associated with the particular article. I'm trying to load that image to my collectionViewController. This is what I've been trying with no success.
From within
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
I am trying this
NSManagedObject *object = [self.fetchedResultsController objectAtIndexPath:indexPath];
NSURL *photoURL = [object valueForKey:#"imageUrl"];
NSData *photoData = [NSData dataWithContentsOfURL:photoURL];
cell.cellimg = [[UIImage alloc] initWithData:photoData];
[cell.title setText:[object valueForKey:#"title"]];
If I comment out the attempt to grab and load the image the view loads great with a default image and the titles of the articles received from the json data. Based on that I know everything is coming in correctly.
I have also determined that *photoData is being assigned the correct URL. But each time I encounter the NSData line the console prints out "-[__NSCFString isFileURL]: unrecognized selector sent to instance...."
I honestly don't know if this is even the correct way to do this or if it will even work. I am pretty new at this so any help would be great. Given that I am new some small code examples would really help as well as explaining the proper way to approach this.
Just in case here is the header file where I define *cellimg and *title
#import <UIKit/UIKit.h>
#interface GlobismNewsCollectionViewCell : UICollectionViewCell
#property (strong, nonatomic) IBOutlet UIImage *cellimg;
#property (weak, nonatomic) IBOutlet UILabel *title;
#property (weak, nonatomic) NSURL *imageUrl;
#end
I think you are using NSString as NSURL. So try to use this one...
NSURL *photoURL = [NSURL URLWithString:(NSString *) [object valueForKey:#"imageUrl"]];
Thanks to Dharmbir I had this issue wrapped up within 25 minutes of asking the question!
To clarify for any new guys like me I changed the line Dharmbir suggested so the block looks like this now
NSManagedObject *object = [self.fetchedResultsController objectAtIndexPath:indexPath];
NSURL *photoURL = [NSURL URLWithString:(NSString *) [object valueForKey:#"imageUrl"]];
NSData *photoData = [NSData dataWithContentsOfURL:photoURL];
[cell.cellimg setImage:[[UIImage alloc] initWithData:photoData]];
[cell.title setText:[object valueForKey:#"title"]];
Notice the 4th line in the block also changed from this
cell.cellimg = [[UIImage alloc] initWithData:photoData];
to this
[cell.cellimg setImage:[[UIImage alloc] initWithData:photoData]];
I also had to change my *cellimg property to this
#property (strong, nonatomic) IBOutlet UIImageView *cellimg;
from this
#property (strong, nonatomic) IBOutlet UIImage *cellimg;
I had to change to the UIImageView class because the cell in my storyboard is based on the UIImageView Class. Hope this helps a guy in need thanks again Dharmbir!
I'm trying to load a UIWebView, but I can't understand why doesn't work, I'm doing this:
Loading *load = [[Loading alloc] init];
[load searchName];
then Loading.h
#interface Loading : NSObject <UIWebViewDelegate>
{
UIWebView *myWebView;
}
- (void) searchName;
Loading.m
- (void) searchName{
myWebView = [[UIWebView alloc] init];
myWebView.delegate = self;
NSString *urlAddress = #"www.google.com";
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[myWebView loadRequest:requestObj];
}
- (void)webViewDidFinishLoad:(UIWebView *)thisWebView
{
NSString *yourHTMLSourceCodeString = [myWebView stringByEvaluatingJavaScriptFromString:#"document.body.innerHTML"];
NSLog(#"%#",yourHTMLSourceCodeString);
}
why never call webViewDidFinishLoad method?
The reason your webview is not displayed is you have taken NSObject class and NSObject classes dont have an XIB and the way you have done is just calling your -(void)searchName method and is not adding it to self.view.
Did you created this webview with IB? Did you connected the delegate to the webview?
Try to catch errors with webView:didFailLoadWithError: (post them if there are errors)
Try to set the delegate with code [myWebView setDelegate:self]; or self.myWebView.delegate = self;
Check output of delegate if nothing happend:
self.myWebView.delegate = self;
NSLog(#"%#",self);
Yes, take note into what Nathan said. I am just expanding.
If you are using IB, connect the outlet. Then use [myWebView setDelegate:self];
Be sure to use http://. Http on it's own won't do anything.
Can you get other URL's to load?
Finally, I have an open source basic web browser for Cocoa that you can take a look at.
Basic Web Browser
I've searched and I believe my problem is quite unique. I'm aware of the Simulator 5.1 bug when using AVAudioPlayer which isn't my problem. I'm running on a iOS 5.1 device.
Here's my header file:
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <AVFoundation/AVAudioPlayer.h>
-(IBAction)pushBell;
#end
and my implementation file:
#import "BellViewController.h"
#interface BellViewController ()
#end
#implementation BellViewController
-(IBAction)pushBell {
NSString *soundPath =[[NSBundle mainBundle] pathForResource:#"bell1" ofType:#"caf"];
NSURL *soundURL = [NSURL fileURLWithPath:soundPath];
NSError *error;
AVAudioPlayer *theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:&error];
[theAudio setDelegate:self];
[theAudio setNumberOfLoops:0];
[theAudio play];
}
#end
And I have a button in my .xib that plays the file.
To make sure I referenced my audio file correctly, I deliberately type a wrong file name and indeed I got an exception. To make sure the file is playable, I mailed it to myself and played it on the device.
When I tap the button I hear nothing. There are no errors. I don't know what is wrong.
More information
iPhone 4 with iOS 5.1
Xcode 4.3.2
Audio is about 700kbps converted to .caf format using afconvert from a big .wav file.
SOLVED.
1.- Check if your file has been added to "Copy Bundle Resources" in Build Phases.
2.- ViewController.m
#import <AVFoundation/AVAudioPlayer.h>
#interface ViewController ()
#property (nonatomic, strong) AVAudioPlayer *theAudio;
- (IBAction)pushBell;
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *soundPath =[[NSBundle mainBundle] pathForResource:#"beep-beep" ofType:#"caf"];
NSURL *soundURL = [NSURL fileURLWithPath:soundPath];
NSError *error = nil;
self.theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:&error];
}
- (IBAction)pushBell {
[self.theAudio play];
}
3.- Connect the IBAction to your button in Interface Builder.
Done.
This was my issue, so I'm just posting it here in case it helps anyone!
Believe it or not, there appears to be a bug (in iOS 5.1 on iPad 2 at least) where if you had the Side Switch set to Mute and it was Muted, then set the Side Switch to Lock Rotation, AVAudioPlayer sounds WON'T PLAY except through headphones!
So I had to go to the Settings app, change the switch back to Mute, then unmute the iPad, switch it back to Lock Rotation and my app started to work properly.
Your theAudio AVAudioPlayer is deallocated before it can even start playing (since it's allocated as a local variable inside pushBell method.
Change your code to:
header file:
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <AVFoundation/AVAudioPlayer.h>
#interface BellViewController:UIViewController
#property (nonatomic, strong) AVAudioPlayer *theAudio;
-(IBAction)pushBell;
#end
implementation file:
#import "BellViewController.h"
#implementation BellViewController
#synthesize theAudio = _theAudio;
- (void)viewDidLoad {
if (!theAudio) {
NSString *soundPath =[[NSBundle mainBundle] pathForResource:#"bell1" ofType:#"caf"];
NSURL *soundURL = [NSURL fileURLWithPath:soundPath];
NSError *error;
_theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:&error];
[self.theAudio setDelegate:self];
[self.theAudio setNumberOfLoops:0];
[self.theAudio prepareToPlay];
}
}
-(IBAction)pushBell {
[self.theAudio play];
}
#end
I'm assuming that BellController is a UIViewController, if it's a different kind of class just modify the code accordingly.
I think that it is a memory issue, you should instantiate the AVAudioPlayer *theAudio as a global class object and initialise it in the init method. Then just run the [theAudio play]; method in the -(IBAction)pushBell method.
I responded do a similar issue related to playing a system sound (which in fact is what you are trying to do), check it out my response is at the bottom:
Couldn't play system sound after switching to iOS 5
By the way consider playing the bell as a system sound, AVAudioPlayer is more appropriate to play sound tracks, not bells and effects.
I hope that it helps.
I agree with hooleyhoop. Try
NSLog(#"%#", error.userInfo)
to see what's coming out.
Another alternative would be to set up the player object in either viewDidLoad or initWithNib and storing it as a property on your object. This way you can use something like
[self.theAudio prepareToPlay];
To avoid loading up the audio each time you press the button, which could save you some time if it's high quality audio. Then just call [self.theAudio play]; in you IBAction method.
Sorry, if this answer is a little sloppy - typing on an iPad so can't double check properly.
You should give it some time to prepare the pronunciation since it's probably buffering from a URL.
The code:
[theAudio prepareToPlay];
[self performSelector:#selector(playMusic) withObject:nil afterDelay:3.0];
I have trouble accomplishing something that sounds very simple. Using Interface Builder, I created a UIWebView object. Next, I created a new controller - HomeViewController and assigned it to the WebView under "NIB Name" property.
Next I need to load a web site (let's say http://google.com) into that WebView when user accesses that view. I am not able to find any examples on how to actually accomplish this. Please provide a code example. Sorry about the newbie question. I did RTFM, but no luck.
The .h file:
#interface WebViewController : UIViewController {
IBOutlet UIWebView *webView;
}
#property (nonatomic, retain) UIWebView *webView;
#end
The .m file:
#import "WebViewController.h"
#implementation WebViewController
// Loads the url when the view loads
- (void)viewDidLoad {
NSString *urlAddress = #"http://www.google.com";
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Request Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[webView loadRequest:requestObj];
}
#end
try this :
[webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://google.com"]]]; // webview - your Uiwebview object
I am wondering if anyone can advise on the best way to add a FAQ page in iphone app. I would like to display a 'local' file (perhaps HTML) with local images into a web view. Essentialy to enable users access to FAQ within the app.
Any advice on whether to use HTML or any other way of doing this will be very helpful..
Thanks in advance..
mb
I used a UIWebView and loaded a local index.html file into this.
**HelpViewController.h**
#interface HelpViewController : UIViewController {
UIWebView *webView;
}
#property (retain) IBOutlet UIWebView *webView;
#end
Obviously you will need to #sythesize webView.
**HelpViewController.m**
- (void)viewDidLoad
{
self.title = #"Help";
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Close" style:UIBarButtonItemStyleDone target:self action:#selector(btnReturn)];;
NSString *helpPath = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
NSURL *url = [NSURL fileURLWithPath:helpPath isDirectory:NO];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
NSLog(#"Help loaded");
}
Hope this helps. Also, just a note, if you are presenting this as a modal, it will appear blank for a split second until the HTML file loads into the UIWebView. I got around this using:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[self performSelector:#selector(showAboutWebView) withObject:nil];
}
- (void)showAboutWebView {
[webView setHidden:NO];
}