EXC_BAD_ACCESS issue when I'm trying to load View - xcode

- (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"];

Related

no visible #interface for 'UIImagePickerController

So my code says
no visible #interface for UIImagePicker Controller
I am using a Tab View Controller
My code for my FirstViewController.m is
#import "FirstViewController.h"
#interface FirstViewController () <UIImagePickerControllerDelegate, UINavigationControllerDelegate> {
UIImagePickerController *bailey;
UIImagePickerController *baileys;
UIImage *image;
IBOutlet UIImageView *imageView;
}
- (IBAction)takePhoto;
- (IBAction)chooseExisting;
#end
#implementation FirstViewController
- (IBAction)takePhoto {
bailey = [[UIImagePickerController alloc]init];
bailey.delegate = self;
[bailey setSourceType:UIImagePickerControllerSourceTypeCamera];
[self presentViewController:bailey animated:YES completion:NULL];
}
- (IBAction)chooseExisting {
baileys = [[UIImagePickerController alloc] init];
baileys.delegate = self;
[baileys setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:baileys animated:YES completion:NULL];
}
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
image = [info objectForKey:UIImagePickerControllerOriginalImage];
[imageView setImage:image];
[self dismissViewControllerAnimated:YES completion:NULL];
}
- (void) imagePickerControllerDidCancel:(UIImagePickerController *)bailey {
[self dismissViewControllerAnimated:YES completion:NULL];
}
- (IBAction)ShareFB {
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
slComposeViewController = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[slComposeViewController addImage:[UIImage imageNamed:#"image"]];
[slComposeViewController addURL:[NSURL URLWithString:#"http://www.agfg.com.au"]];
[self presentViewController:slComposeViewController animated:YES completion:NULL];
//I borrowed these lines of code from http://www.bit.ly/174eqjy
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appFile = [documentsDirectory stringByAppendingString:#"upload.png"];
NSString *upLoad = [documentsDirectory stringByAppendingString:#"upload.png"];
NSData *myData = [[NSData alloc]initWithContentsOfFile:appFile];
//Here is the line that fails
[bailey addAttachmentData:myData mimeType:#"image/png" fileName:#"upload"];
//Up to here are the borrowed code lines.
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"No Facebook Account Detected" message:#"There are no Facebook Accounts Detected. You can configure them in Settings." delegate:nil cancelButtonTitle:#"Dismiss" otherButtonTitles:nil];
[alert show];
}}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
And my code for my FirstViewController.h is
#import <UIKit/UIKit.h>
#import <Social/Social.h>
#interface FirstViewController : UIViewController {SLComposeViewController *slComposeViewController;
UIImage *upLoad;}
- (IBAction)ShareFB;
#end
My app is designed to Take a photo and display the photo (and it does that) and then save that image to the local documents folder on the app (which I got from another post) and then specify that image to upload to Facebook (which I have)
All that works except the saving to a local documents.
If you need to store file in documents directory, use like this,
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:#"Images"];
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
{
[[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:nil];
}
NSString *imageFilePath = [NSString stringWithFormat:#"%#/%#",dataPath,#"upload.png"];
UIImage *image = [UIImage imageNamed:#"image"]; //Here place your image
NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(image)];
[imageData writeToFile:imageFilePath atomically:YES];
If you need to attach image in facebook, use this statement,
[slComposeViewController
addAttachmentData:UIImagePNGRepresentation(image, 1) mimeType:#"image/png" fileName:#"upload.png"];
With the help of Karthika, and his facebook upload code, I was typing it in when I realised that I did not need to implement local documents saving.
Facebook let me add the image in without saving it.
Thanks though Karthika, I could not have done it without you.

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

XCode - UIWebView Not Loading

I have two local .html files in the Resources folder. I'm trying to load them the following way, but only the final page loads. What am I doing wrong?
File = please_wait.html
This one does not work.
NSError *error;
NSString* path = [[NSBundle mainBundle] pathForResource:#"please_wait" ofType:#"html"];
NSString* htmlString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
[webView loadHTMLString:htmlString baseURL:[NSURL fileURLWithPath:path]];
//Big "do-while" loop here. It works fine so I omitted it.
File = update_graph.html
This one does not work
path = [[NSBundle mainBundle] pathForResource:#"update_graph" ofType:#"html"];
htmlString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
[webView loadHTMLString:htmlString baseURL:[NSURL fileURLWithPath:path]];
//Lots of code removed. All works correctly and doesn't touch webview
This last one works perfectly. Google displays.
string = #"http://google.com";
NSURL *url = [NSURL URLWithString: string];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
It appears from your comment that your UIWebView loads just fine, but it does not get a chance to refresh itself on the screen until you exit your method. It is not enough to set a break point inside the method and wait for the view to load: you must exit the method before iOS realizes that it needs to call UIWebView's drawRect method.
To fix this, split your method in three parts, A B and C, and set UIWebView's delegate in A to invoke B on webViewDidFinishLoad:, and the delegate in B to call C.
Here is how to implement this: start with a delegate that can call a selector when the loading has completed:
#interface GoToNext : NSObject <UIWebViewDelegate> {
id __weak target;
SEL next;
}
-(id)initWithTarget:(id)target andNext:(SEL)next;
-(void)webViewDidFinishLoad:(UIWebView *)webView;
#end
#implementation GoNext
-(id)initWithTarget:(id)_target andNext:(SEL)_next {
self = [super init];
if (self) {
target = _target;
next = _next;
}
return self;
}
-(void)webViewDidFinishLoad:(UIWebView *)webView {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
[target performSelector:next];
#pragma clang diagnostic pop
}
#end
Now split your method into three parts - loading the first page, loading the second page, and loading the third page:
-(void)loadPleaseWait {
NSError *error;
NSString* path = [[NSBundle mainBundle] pathForResource:#"please_wait" ofType:#"html"];
NSString* htmlString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
webView.delegate = [[GoToNext alloc] initWithTarget:self andNext:#selector(loadUpdateGraph)];
[webView loadHTMLString:htmlString baseURL:[NSURL fileURLWithPath:path]];
// big do-while loop
}
-(void)loadUpdateGraph {
NSError *error;
NSString* path = [[NSBundle mainBundle] pathForResource:#"update_graph" ofType:#"html"];
NSString* htmlString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
webView.delegate = [[GoToNext alloc] initWithTarget:self andNext:#selector(loadGoogle)];
[webView loadHTMLString:htmlString baseURL:[NSURL fileURLWithPath:path]];
// Lots of code removed
}
-(void)loadGoogle {
string = #"http://google.com";
NSURL *url = [NSURL URLWithString: string];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
}

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

xcode debugging terminates when i run app

When I run my app in Xcode using the simulator it runs just file until I add in the first three lines involving the text file. It gives me a message saying terminate called after throwing an instance of 'NSException'. I don't know what that means or why it only happens when I try to read in the text file and display it in the textView.
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"textfiles/brain_01" ofType:#"txt"];//establish file path for text file
NSString *textFile = [[NSString alloc] stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error: nil];
textView.text = textFile;
[super viewDidLoad];
fullButton.hidden = YES;
viewLabel.hidden = YES;
}
NSException means your code has a bug in it. An exception is an error. In your case, it's probably because
NSString *textFile = [[NSString alloc] stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error: nil];
should be
NSString *textFile = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error: nil];
stringWithContentsOfFile is a class method used to make NSString -- only use alloc with init... methods.

Resources