Trouble with new View Controller called from a UITextField Xcode - uiviewcontroller

In the current App that I am writing, I have a UITextField that has a link in it. So I want to call a new View Controller instead of Safari. I can have it open in Safari, but I don't want the user to leave my App. I have done some research and am doing the following.
I made a new file called MyApplication and placed the following inside the .m file to override openURL
#import "MyApplication.h"
#implementation MyApplication
- (BOOL)openURL:(NSURL *)url
{
if ([self handleOpenURL:url])
return YES;
else
return [super openURL:url];
}
- (BOOL)handleOpenURL:(NSURL *)url
{
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:#"71"];
[self presentViewController:vc animated:YES completion:nil];
return YES;
}
#end
In the attributes inspector under View Controller I called the Title 71. I also called the storyboard ID and restoration ID 71 just to be sure! In the main.m file I have replaced the third nil with #"MyApplication" as you can see.
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
int main(int argc, char *argv[])
{
#autoreleasepool {
return UIApplicationMain(argc, argv, #"MyApplication", NSStringFromClass([AppDelegate class]));
}
}
However, the App fails to build because in the method handleOpenURL I am getting an error on the line
[self presentViewController:vc animated:YES completion:nil];
It says No visible #interface for 'MyApplication' declares the selector 'presentViewController:animated:completion:' All the samples and questions I have seen are using the deprecated method presentModal but Xcode tells me to use this newer method presentViewController. So does anyone know what I am doing wrong? I must be missing something, but I can't for the life of me find out. Thanks in advance on any insight to this matter!!!

May be you're missing the :
#import <UIKit/UIKit.h>

Related

UIWebView opening the camera and images when a button is clicked

I am currently wrapping a webApp up as an app. With in the web app there is a button that when clicked automatically asks if you want to go to the camera of the stored images and when selected will upload the image for you. In the web app from the phone browser this works as expected.
Now I have wrapped the webApp up in a UIWebView this function no longer works. It actually appears to close the UIWebview when selecting either of the above options. Does anyone know what the procedure is to get this to work through the UIWebView.
Do I have to give permission? Or is there more to it like detecting the HTML when the button is clicked and handling it from the UIWebView.
The HTML on the button looks like this.
<input type="file" required id="file" name="file" accept="image/*" multiple>
ADDED CODE
WebViewController.h
#import <UIKit/UIKit.h>
#interface WebViewController : UIViewController <UIWebViewDelegate,UINavigationControllerDelegate, UIImagePickerControllerDelegate>
{
UIViewController *viewController;
}
........
WebViewController.m
#import "WebViewController.h"
#import <AssetsLibrary/AssetsLibrary.h>
#import "AppDelegate.h"
#end
#implementation WebViewController
- (void)viewDidLoad
{
[super viewDidLoad];
viewController = [[UIViewController alloc] init];
NSString *fullURL = self.mainURL; // get this from defualts - have removed code for this
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_viewWeb loadRequest:requestObj];
ALAssetsLibrary *lib = [[ALAssetsLibrary alloc] init];
[lib enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
NSLog(#"%i",[group numberOfAssets]);
} failureBlock:^(NSError *error) {
if (error.code == ALAssetsLibraryAccessUserDeniedError) {
NSLog(#"user denied access, code: %i",error.code);
}else{
NSLog(#"Other error code: %i",error.code);
}
}];
}
-(void)webViewDidStartLoad:(UIWebView *)viewWeb
{
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
}
- (void)webViewDidFinishLoad:(UIWebView *)viewWeb
{
}
//Note: I check to make sure the camera is available. If it is not (iPod touch or Simulator) I show the photo library instead.
-(void) showCamera
{
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
}
else
{
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}
imagePicker.delegate = self;
[viewController presentViewController:imagePicker animated:YES completion:nil];
}
//Here we intercept any time the webview tries to load a document. When the user hits our "showcamera: url" we go to work.
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:
(UIWebViewNavigationType)navigationType
{
if ([request.URL.scheme isEqualToString:#"myApp"])
{
if ([request.URL.host isEqualToString:#"myFunction"])
{
[self showCamera];
}
return NO;
}
return YES;
}
//After the imagepicker has done it's thing, we pass the data back to the webview to be displayed.
//If we wanted to be fancy here, we could have done this via Javascript so we could dynamically insert an image without reloading the page.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[viewController dismissViewControllerAnimated:YES completion:nil];
UIImage* image = [info objectForKey:UIImagePickerControllerOriginalImage];
NSData *imageData = UIImageJPEGRepresentation (image, 0.5);
NSURL *tmp = [NSURL URLWithString:#"ignore"];
[_viewWeb loadData:imageData MIMEType:#"image/jpeg" textEncodingName:#"UTF-8" baseURL:tmp];
}
#end
I have removed some code that was irrelevant
I have managed to fix the issue and it goes way back to bug that was found in iOS 8 that still hasn't been fixed. Its to do with how the UIWebView is being presented.
Adding this code fixed it.
-(void)dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion
{
if ( self.presentedViewController)
{
[super dismissViewControllerAnimated:flag completion:completion];
}
}
For any one else confused or struggling with this Note that you do not need to start messing around with UIImagePicker or UIWebView delegates. Your web view should take care of opening the camera library or camera. This was a real pain of an issue so hopefully this may help others in my position. you can also see THIS Question as this is where i got my answer from. Thanks Andrey for helping too
As an alternative you can try to use UIWebViewDelegate's method webView:shouldStartLoadWithRequest:navigationType: to catch custom events:
In your HTML:
Do something
<button onclick="window.location.href='myApp://myFunction'">Do some action</button>
And then:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if ([request.URL.scheme isEqualToString:#"myApp"]) {
if ([request.URL.host isEqualToString:#"myFunction"]) {
// do something
}
return NO;
}
return YES;
}

How to add UIViewcontroller to a spritebuilder cocos2d 3.x project?

same as the title above. Please provide helpful link if possible. Googled it but did not find anything regarding Spritebuilder and uiviewcontroller integration. As it is needed for me to set up Google Analytics screen tag method inside viewDidLoad() method.
Thanks
This code will allow you to present UIViewController above your CCScene.
MyViewController *vc=[[MyViewController alloc]initWithNibName:#"MyViewController" bundle:nil];
UIViewController *rootViewController=[UIApplication sharedApplication].keyWindow.rootViewController;
if (!rootViewController) {
//some devices have no root view controller so... this code fix the issue
[[CCDirector sharedDirector]presentModalViewController:vc animated:YES];
}else{
[rootViewController presentViewController:vc animated:NO completion:nil];
}

Whats the best way to setup & populate my nstableview with json data?

I'm sorry if this has already been answered. I'm a bit of a noob. From all the examples I've found I can't seem to work this out.
I'm building a mac osx application using Xcode 5. My interface that I've created in Interface builder has three tableviews. My data to populate these views is json format and it is being received successfully using NSURLConnection.
What is the best way to popular the tableviews? A lot of the tutorials I've followed use an arraycontroller that is dragged and dropped using IB. From what I understand I don't need an array controller and need to hand code controllers for each of tableviews. Is that correct? If so, please explain how I go about this and setup the bindings.
Also, when clicking on a column in tableview 1 I'm planning on making it adjust the data in tableview 2 & 3 so if that impacts anything please let me know.
Thanks in advance!
Update 6 Jan
Here is my code currently..
ContactsTableViewController.h
#import <Foundation/Foundation.h>
#interface ContactsTableViewController : NSObject <NSTableViewDataSource, NSTableViewDelegate> {
IBOutlet NSTableView *contactsTableView;
}
-(id) init;
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView;
-(NSView *) tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row;
#end
ContactsTableViewController.m
#import "ContactsTableViewController.h"
#implementation ContactsTableViewController
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
NSLog(#"numberOfRowsInTableView called");
return 1;
}
- (id) init {
NSLog(#"init called");
[contactsTableView setDelegate:self];
[contactsTableView setDataSource:self];
[contactsTableView reloadData];
return self;
}
- (void) initme {
NSLog(#"customfunc");
}
-(NSView *) tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
NSLog(#"viewForTableColumn called");
NSTextField *result = [tableView makeViewWithIdentifier:#"name" owner:self];// result is now guaranteed to be valid,
result.stringValue = #"My Name"; //[self.nameArray objectAtIndex:row];
return result;
}
#end
In my appDelegate:
- (void) awakeFromNib {
NSLog(#"AWAKE");
ContactsTableViewController *contactsController = [[ContactsTableViewController alloc] init];
}
Currently nothing is showing in the tableview and numberOfRowsInTableView & viewForTableColumn aren't getting called.
Look at the UITableViewDataSource, you need to implement the protocol in one of your classes, probable your view controller, implement the UITableViewDelegate to get row selection events in the same class.

Pass Variable (cookie) from local html to UILabel - iOS

I wanted to know how I could go about passing a Variable (cookie) in a local HTML file in my iOS App, and pass what the cookie says onto a UILabel. Here is what the HTML code is. I need to pass this message into a UILabel
<script language="javascript">setCookie('Test','You Sir have succeeded. Congratulations.', 300);
</script>
And here is my updated code for iOS app
Header File:
import
#interface ViewController : UIViewController {
IBOutlet UIWebView *webView;
IBOutlet UILabel *mylabel;
}
#end
Main File
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"DTPContent/cookietest" ofType:#"html"]isDirectory:NO]]];
[super viewDidLoad];
NSString *myCookie = [self->webView stringByEvaluatingJavaScriptFromString:#"getCookie('Test')"]; self->mylabel.text = myCookie;
}
#end
Thank You in Advance!
Assuming you already have a javascript function to get the cookies, like that, you can simply execute the javascript with webview's method stringByEvaluatingJavaScriptFromString , get it's returned string and set it in a UILabel.
It will be something like this:
NSString *myCookie = [self.webView stringByEvaluatingJavaScriptFromString:#"getCookie('Test')"];
self.mylabel.text = myCookie;

implementing TTThumbsViewController on a UIViewController?

Im trying to implement add TTThumbsViewController as a subview of UIViewController but the app is crashing. Here you see the code-
.h file-
#import <UIKit/UIKit.h>
#import <Three20/Three20.h>
#import <Three20UI/UIViewAdditions.h>
#import "MockPhotoSource.h"
#interface photos : UIViewController <NSXMLParserDelegate, TTThumbsViewControllerDelegate>
{
TTThumbsViewController *photoSource;
...........
}
.m File-
- (void)viewDidLoad {
// parsing a feed
[self parseXMLFileAtURL:#"http://feed203.photobucket.com/albums/aa15/vikysaran/Worst%20Parents/feed.rss"];
NSMutableArray *arr = [[NSMutableArray alloc]init];
for (int i=0; i < [stories count];i++) {
[arr addObject:[[[MockPhoto alloc] initWithURL:[[stories objectAtIndex:i] objectForKey:#"fullimageurl"] smallURL:[[stories objectAtIndex:i] objectForKey:#"thumburl"]
size:CGSizeMake(960, 1280)] autorelease]];
}
photoSource = [[[MockPhotoSource alloc] initWithType:MockPhotoSourceNormal title:nil photos:[NSArray arrayWithArray:arr] photos2:nil]autorelease];
// till here the code is working fine if i am not using uiviewcontroller
NSLog(#"mockphoto%#", arr);
[self.view addSubview:photoSource.view];// adding to uiview
}
here is the error displays-
-[MockPhotoSource view]: unrecognized selector sent to instance 0x6924ca0
2011-09-22 13:07:07.375 JesonTerry[1969:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MockPhotoSource view]: unrecognized selector sent to instance 0x6924ca0'
Please anybody help. i am totally confused where i am wrong...
your view controller has to be a type TTThumbsViewController and not a UIViewControler for starters.. Check the TTCatalog app in the samples folder