UIWebView & NavigationController crashes while loading - objective-c

I have very weird problem
I build a simple app with UIWebView and NavigationController but the problem is when the seconds page is loading and I go back to the first page and visited a link the APP crashes and the console says nothing
Here is my code on finish loading
-(void)webViewDidFinishLoad:(UIWebView *)aWebView {
NSString *str = [aWebView stringByEvaluatingJavaScriptFromString:#"document.title"];
self.navigationItem.title = str;
[navigationActivity stopAnimating];
}
and this for the web view should start loading
if(navigationType == UIWebViewNavigationTypeOther)
{
NSURL *url2 = [request URL];
NSString *URLStr = [url2 absoluteString];
RootViewController* viewController = [[RootViewController alloc] init];
NSString *holder = [self getQueryStringInner:URLStr];
[self getQueryString:URLStr];
if([holder length] != 0 && [flag length] != 0 && !facebook )
{
appDelegate.title =#"Title";
appDelegate.query = queryString;
appDelegate.url = holder;
[self.navigationController pushViewController:viewController animated:YES];
[viewController release];
return NO;
}
}
I am really new to Objective-c and iOS development so any help will be appreciated
Any Help

Seems that the necessary step is to call [webView stopLoading] in your viewWillDisappear. Apparently the web view delegate was sending the webViewDidFinishLoading method message to a deallocated instance. Glad you got it worked out.

Related

How to Play Video from Within Collapsible UITableViewCell?

HI All: I have set up collapsible uitableviewcells and am trying to get video to play in the didSelectRowAtIndexPath method. I can get cells to fire a print statement to the console, but no matter what I try to get a video to play nothing works! Here is the code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if(indexPath.section == 0)
{
if(indexPath.row == 0)
{
printf("hello");
}
else
{
printf("good bye");
}
}
else if(indexPath.section == 1)
{
if(indexPath.row == 0)
{
printf("hello");
}
else
{
printf("good bye again");
}
}
}
Can you please let me know what I need to do to get a local video file to play? Obviously I need help in Objective C please! I have tried to programmatically create an AVPlayerViewController and then use [self presentviewcontroller] method but I always get the following error (which is same error if I use prepareforSegue):
NSString *path = [[NSBundle mainBundle] pathForResource:#"PrelimFig1" ofType:#"m4v"];
_videoURL = [NSURL fileURLWithPath:path];
NSLog(#"I want to log: %#", _videoURL);
AVPlayerViewController *avp = [[AVPlayerViewController alloc]init];
AVPlayer *player = [AVPlayer playerWithURL:_videoURL];
avp.player = player;
[self presentViewController:avp animated:YES completion:nil]
[player play];
Thank you in advance for your help!
You can do like that
NSURL *videoURLLocal = [[NSBundle mainBundle]URLForResource:#"PrelimFig1" withExtension:#"m4v"];
AVPlayerViewController *avp = [[AVPlayerViewController alloc]init];
AVPlayer *player = [AVPlayer playerWithURL:videoURLLocal];
avp.player = player;
[self presentViewController:avp animated:YES completion:^{
[player play];
}];
or
NSURL *videoURLLocal = [[NSBundle mainBundle]URLForResource:#"PrelimFig1" withExtension:#"m4v"];
// create an AVPlayer
AVPlayer *player = [AVPlayer playerWithURL:videoURLLocal];
// create a player view controller
AVPlayerViewController *controller = [[AVPlayerViewController alloc]init];
controller.player = player;
[player play];
// show the view controller
[self addChildViewController:controller];
[self.view addSubview:controller.view];
controller.view.frame = self.view.frame;

Html form in UIwebview send back to ios?

i am trying to pass information between an HTML form and ios i tryed javascrip but it isn't working?
// Init Web View
UIWebView *webView;
webView = [[UIWebView alloc] initWithFrame:boundsRect];
webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
webView.scalesPageToFit = YES;
webView.contentMode = UIViewContentModeCenter;
webView.multipleTouchEnabled = NO;
webView.userInteractionEnabled = YES;
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
[self addSubview:webView];
NSString *testArrayString = [webView stringByEvaluatingJavaScriptFromString:#"testString"];
NSArray *testArray = [testArrayString componentsSeparatedByString:#"#"];
NSLog(#"%#", testArray);
[webView release];
return self;
any ideas?
----EDIT----
Ok guys if u only want to make data from a form pass to a ios app you only do this to your webview
[webView setDelegate:self];
Then you can implement this methods in your program marks
like:
-(void)webViewDidFinishLoad:(UIWebView *) webView {
//implement code to do here
}
if you want to capture a for just do a webview shouldStartWithRequest like this:
-(BOOL)webView:(UIwebView *) webView shouStartWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigation{
//implement code to do afeter the user click the form don't forget to return YES or NO because it is a BOOL
}
that is how i solve it any question please do tell me i try my best to help
To pass data from UIWebView and back to your Objective-C runtime, you need to implement UIWebViewDelegates webView:shouldStartLoadingWithRequest: and pass your form-data as parameters in the URL. If you craft all your URL's with a scheme like f.ex. my-app://?parameter1=value1&parameter2=value2, you can easily filter out the URLs the web view should not load, and return NO.
EDIT:
Added example-code.
- (void)initWebView
{
UIWebView *webView;
webView = [[UIWebView alloc] initWithFrame:boundsRect];
webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
webView.scalesPageToFit = YES;
webView.contentMode = UIViewContentModeCenter;
webView.multipleTouchEnabled = NO;
webView.userInteractionEnabled = YES;
webView.delegate = self;
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *url = request.URL;
if ([url.scheme isEqualToString:#"http://"]) {
return YES;
}
else if ([url.scheme isEqualToString:#"my-app://"]){
// Process data from form
return NO;
}
return YES;
}
EDIT2: You are releasing the web view in your initialisation-code. This prevents the delegate-method from being called when you are loading the form-url
EDIT3: See this question for more code-examples: Invoke method in objective c code from HTML code using UIWebView

Objective C Objects Only Being Changed In Method Scope

I am making a chat application, and when the person logs onto the chat successfully, the server replies with the people that are online at that moment. That string (the server sends a string) gets converted to a NSMutableArray, which is then stored into a NSMutableArray called tableData, which is the data source for a NSTableView. When the online people are stored into tableData, the NSLog output shows that tableData is filled. However, when that method is done, and the login view is closed, in the debugger tableData says 0 Objects, and the NSTableView doesn't fill, which it normally does. Here is are my methods (one calls another):
- (void)getPeople:(NSString *)outputMessage
{
NSArray *newTableData = [outputMessage componentsSeparatedByString:#";"];
self.tableData = [NSMutableArray arrayWithArray:newTableData];
//[self.tableData removeObjectAtIndex:0];;
NSLog(#"Data: %#", self.tableData);
[self.people reloadData];
}
- (void)accessGrantedWithOnlineUsers:(NSString *)users
{
NSLog(#"Access Granted");
self.isLoggedIn = YES;
[self.loginButton setTitle:#"Logout"];
[self getPeople:users];
}
Here is my method that opens and closes the login view:
- (IBAction)loginToChat:(id)sender {
NSLog(#"Called");
if (self.loginPopover == nil) {
NSLog(#"Login Popover is nil");
self.loginPopover = [[NSPopover alloc] init];
self.loginPopover.contentViewController = [[LoginViewController alloc] initWithNibName:#"LoginViewController" bundle:nil];
}
if (!self.loginPopover.isShown) {
NSLog(#"Login Popover is opening");
[self.loginButton setTitle:#"Cancel"];
[self.settingsButton setEnabled:NO];
[self.send setEnabled:NO];
[self.message setEnabled:NO];
[self.loginPopover showRelativeToRect:self.loginButton.frame ofView:self.view preferredEdge:NSMinYEdge];
}
else {
NSLog(#"Login Popover is closing");
if (self.isLoggedIn) {
[self.loginButton setTitle:#"Logout"];
}
else {
[self.loginButton setTitle:#"Login"];
}
[self.settingsButton setEnabled:YES];
[self.send setEnabled:YES];
[self.message setEnabled:YES];
[self.loginPopover close];
}
}
Any help would be greatly appreciated because I have a deadline for this project.
Instead of
self.tableData = [NSMutableArray arrayWithArray:newTableData];
can you try
self.tableData = [newTableData copy];
The only thing i can think of is - is the tableData a strong property?
After this
self.tableData = [NSMutableArray arrayWithArray:newTableData];
write this snippet:
[self setTableData:tableData]

From Login screen to `UITabBarController`

I have a Login screen which checks if the inserted Password is correct.
After that I want to switch from the Login screen to a UITabBarController.
Code from LoginViewController.m:
-(IBAction)LoginButton:(id)sender {
[PassWortEingabe resignFirstResponder];
NSString *pnssPasswortEingabe = [NSString stringWithFormat:#"%#",PassWortEingabe.text];
NSString *pnssPasswortString = [NSString stringWithFormat:#"%s","Hallo"];
if( [pnssPasswortEingabe isEqualToString: pnssPasswortString ]){
DebugTextView.text = #"Login succesfull";
//PassWortEingabe = 0;
//[PassWortEingabe resignFirstResponder];
}else{
DebugTextView.text = #"Login unsuccesfull";
//PassWortEingabe = 0;
//[PassWortEingabe resignFirstResponder];
}
}
I want jump to the UITabBarController when the Login is sucessful ...
if( [pnssPasswortEingabe isEqualToString: pnssPasswortString ]){
DebugTextView.text = #"Login succesfull";
MyTabBarClass *myTabBar = [[MyTabBarClass alloc]initWithNibName:nil bundle:nil];
myTabBar.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:myTabBar animated:YES];
//PassWortEingabe = 0;
//[PassWortEingabe resignFirstResponder];
}else{
DebugTextView.text = #"Login unsuccesfull";
//PassWortEingabe = 0;
//[PassWortEingabe resignFirstResponder];
}
It is as simple as the code below.
if( [pnssPasswortEingabe isEqualToString: pnssPasswortString ]){
// This is for iOS 5.0 and above.
UITabBarController *myTabBarController = [self.storyboard instantiateViewControllerWithIdentifier:#"myTabBarController"];
[myTabBarController setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[self presentViewController:myTabBarController animated:YES completion:^(void){
// You can set some sort of completion block here which will run when all other code finishes, this can just be nil.
}];
} else {
DebugTextView.text = #"Login unsuccesfull";
}
You can also replace this line with
[self presentViewController:myTabBarController animated:YES completion:nil];
replace with
[self presentModalViewController:myTabBarController animated:YES];
but this has been deprecated in iOS 6.0. So if you are making your app for iOS 5.0 and above you are best of using the first line so you don't have to make changes in the future.
The above code is made to use storyboards if you want to do it with nib files then change
[self.storyboard instantiateViewControllerWithIdentifier:#"myTabBarController"];
to
[[UITabBarController alloc] initWithNibName:#"myTabBarController" bundle:[NSBundle mainBundle]];
Hope this helps.

ShareKit modal view controller won't go away

I'm using ShareKit 0.2.1 on Xcode 4.2 (iOS SDK 5) to share text on Twitter. It shares fine, but the modal view controller wont go away after successfully sharing on after clicking on the cancel button (see below):
And this is my code:
-(IBAction)shareOnTwitter:(id)sender{
// Item to share
NSString *text = #"Go away, modal view controller!";
[SHKTwitter shareText:text];
}
What am I doing wrong?
It is an iOS 5 issue. It's because ShareKit is using a method on UIViewController called parentViewController and according to the Apple docs you can no longer use this in iOS 5. Instead, you must use presentingViewController.
So to fix it in the ShareKit code, go into SHK.m, find the method with signature (void)hideCurrentViewControllerAnimated:(BOOL)animated, and replace it with:
- (void)hideCurrentViewControllerAnimated:(BOOL)animated
{
if (isDismissingView)
return;
if (currentView != nil)
{
// Dismiss the modal view
if ([currentView parentViewController] != nil)
{
self.isDismissingView = YES;
[[currentView parentViewController] dismissModalViewControllerAnimated:animated];
} else if ([currentView presentingViewController] != nil) {
self.isDismissingView = YES;
[[currentView presentingViewController] dismissModalViewControllerAnimated:animated];
} else
self.currentView = nil;
}
}
This works for me on iOS 5.
if (isDismissingView)
return;
if (currentView != nil)
{
// Dismiss the modal view
if ([currentView parentViewController] != nil)
{
self.isDismissingView = YES;
[[currentView parentViewController] dismissModalViewControllerAnimated:animated];
}
else {
//## ADD BELOW ##
self.isDismissingView = YES;
[currentView dismissModalViewControllerAnimated:animated];
self.currentView = nil;
}
}
else {
[[self getTopViewController].navigationController popViewControllerAnimated:YES];
}
This is the code I use in one of my apps.
It dismisses fine.
NSURL *url = [NSURL URLWithString:#"http://itunes.apple.com/us/app/packager/id459511278?l=nl&ls=1&mt=8"];
NSString *twittertext = [[NSString alloc] initWithFormat: #"Tweet Text"];
SHKItem *item = [SHKItem URL:url twittertext];
// Share the item
[SHKTwitter shareItem:item];
[twittertext release];
I have used the following code in my app (ARC disabled)
NSString *text = #"Go away, modal view controller!";
[SHKTwitter shareText:text];
I can confirm it dismisses fine.
You probably changed some code in SHKTwitterForm.m when attempting to make Sharekit ARC compatible. Which resulted in your bug