Error coming up when trying to implement Google map directions, method definition not found - objective-c

I have been trying to add a link to Google Maps so that I can show some directions to a location. The only problem i'm having is the code not recognizing the method. Here's samples below, hope they help.
.h:
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#interface More : UIViewController{
}
- (IBAction)directions:(id)sender;
-(CLLocationCoordinate2D)getCurrentLocation;
#end
.m:
- (IBAction)directions:(id)sender {
CLLocationCoordinate2D currentLocation = [self getCurrentLocation];
NSString* address = #"********";
NSString* url = [NSString stringWithFormat: #"http://maps.google.com/maps?saddr=%f,%f&daddr=%#",
currentLocation.latitude, currentLocation.longitude,
[address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL: [NSURL URLWithString:url]];
}
Error:
Method definition for 'getCurrentLocation not found
Thanks

It looks like you haven't written getCurrentLocation. I'm not sure what that method is but google for CoreLocationControllerDelegate, that will show you how to get your location.
Is there a reason you don't want to use MKMapView? It really makes your life easy.

Related

React Native and Objective C delegates

I am quite new to react native and and the bridging mechanism with native code, especially when the framework has delegates. Assume I am trying to bridge the following framework:
#protocol BRPtouchNetworkDelegate;
#class PLNetworkModule;
#interface BRPtouchNetworkManager : NSObject <NSNetServiceBrowserDelegate,NSNetServiceDelegate>
#property(retain, nonatomic) NSMutableArray* registeredPrinterNames;
#property(assign, nonatomic) BOOL isEnableIPv6Search;
- (int)startSearch: (int)searchTime;
- (NSArray*)getPrinterNetInfo;
- (BOOL)setPrinterNames:(NSArray*)strPrinterNames;
- (BOOL)setPrinterName:(NSString*)strPrinterName;
- (id)initWithPrinterNames:(NSArray*)strPrinterNames;
- (id)initWithPrinterName:(NSString*)strPrinterName;
#property (nonatomic, assign) id <BRPtouchNetworkDelegate> delegate;
#end
#protocol BRPtouchNetworkDelegate <NSObject>
-(void) didFinishSearch:(id)sender;
#end
The following is the bridge module I implemented:
RCTBRPtouchNetworkManager.h
#import <React/RCTBridgeModule.h>
#import <BRPtouchPrinterKit/BRPtouchPrinterKit.h>
#interface RCTBRPtouchNetworkManager : NSObject <RCTBridgeModule, BRPtouchNetworkDelegate>
#end
RCTBRPtouchNetworkManager.m
#import "RCTBRPtouchNetworkManager.h"
#import <BRPtouchPrinterKit/BRPtouchPrinterKit.h>
#import <React/RCTLog.h>
#implementation RCTBRPtouchNetworkManager {
BRPtouchNetworkManager *_networkManager;
}
RCT_EXPORT_MODULE();
RCT_EXPORT_METHOD(addEvent:(NSString *)name location:(NSString *)location)
{
RCTLogInfo(#"Pretending to create an event %# at %#", name, location); //a dummy method to test the bridge
}
RCT_EXPORT_METHOD(startSearchWithTimeout:(int)time) {
RCTLogInfo(#"Bridge started search with time %d", time);
_networkManager = [[BRPtouchNetworkManager alloc] init];
_networkManager.delegate = self; //I'm setting delegate here
_networkManager.isEnableIPv6Search = NO;
NSString * path = [[NSBundle mainBundle] pathForResource:#"PrinterList" ofType:#"plist"];
if( path )
{
NSDictionary *printerDict = [NSDictionary dictionaryWithContentsOfFile:path];
NSArray *printerList = [[NSArray alloc] initWithArray:printerDict.allKeys];
[_networkManager setPrinterNames:printerList];
} else {
RCTLogInfo(#"PrinterList path not found");
}
// Start printer search
[_networkManager startSearch: 5.0];
}
- (void)didFinishSearch:(id)sender {
NSLog(#"didFinishedSearch"); //this delegate method is not called
}
#end
I can easily call the dummy method and see the results in the logs. However, the delegate method didFinishSearch() is never called. I call this from javascript as follows:
componentDidMount() {
let networkManager = NativeModules.BRPtouchNetworkManager;
networkManager.startSearchWithTimeout(5.0);
}
I there something I am missing? Am I implementing delegate properly? Is this kind of functionality even possible (can't seem to not since the delegate method was used by iOS community for a long time). Your help is much appreciated.
EDIT
I found that adding the following to my bridge manager file made the delegate to fire (thanks to this post)
- (dispatch_queue_t)methodQueue
{
return dispatch_get_main_queue();
}
However, even though this solves the problem, I'd like a more technical understanding on what is going on here since I can't seem to exactly grasp it. Thank you
I know this isn’t an an answer to the post but for the bit where you’ve asked for a more technical understanding - dispatch_get_main_queue(); puts the delegate method responses on to the main thread. Since JS is single threaded any process on the background thread won’t be visible to it.

Must AVAudioPlayer be instantiated from a ViewController class?

I'm hoping someone can help as I'm new to iOS / objective C and very puzzled. I'm trying to play a simple sound using AVAudioPlayer as follows:
NSString *path = [[NSBundle mainBundle] pathForResource:#"soundFile" ofType:#"wav"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: path];
self.player=[[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:NULL];
[self.player play];
I am using ARC so I also have in my .h file, the following reference to my player so that ARC does not deallocate my player prematurely:
#property (nonatomic, strong) AVAudioPlayer *player;
This code works just fine and plays my sound PROVIDED that I run this code from a ViewController or my application's AppDelegate.
However if I cut and paste this very same code, plus all the necessary #includes and the #property and add them into another class in the same application but which is not a ViewController, and call the code there then no error is raised but no sound is played.
It is exactly the same code just called on a different class??
Why would it not work?
I have looked and looked for a similar post but nowhere have I seem exactly this scenario addressed. Many thanks if you can help me- would be much appreciated.
To clarify the issue-- here is how I call this code on another class say a class I have named Audio Tester, I would write in AppDelate say
#import "AppDelegate.h"
#import "AudioTester.h"
#interface AppDelegate ()
#end
#implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
AudioTester * tester = [[AudioTester alloc]init];
[tester playAudio];
}
where AudioTester playAudio is defined as
#import "AudioTester.h"
#implementation AudioTester
-(void) playAudio {
NSString *path = [[NSBundle mainBundle] pathForResource:#"soundFile" ofType:#"wav"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: path];
self.player=[[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:NULL];
[self.player play];
}
#end
with AudioTester.h as follows
#import <Cocoa/Cocoa.h>
#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>
#interface AudioTester : NSObject
#property (nonatomic, strong) AVAudioPlayer *player;
-(void) playAudio;
#end
Stepping through this code, it gets called just fine but it does not play sound?
If you can help that would be much appreciated. I'm totally stumped.
a little conceptual explanation about your code:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
AudioTester * tester = [[AudioTester alloc]init];
[tester playAudio];
}
1.
if you use ARC then the instance won't be not kept alive after the scope runs out, therefore the tester object will be immediately released, so in your case the object is deallocated before it could do anything – that is the reason why you can't hear any noise or sound.
if you want to keep your tester instance alive independently from your current scope where you inited in, you need to create like e.g. a property which is outside of the scope; you could put that into a class extension for instance:
#interface AppDelegate ()
// ...
#property (nonatomic, strong, nullable) AVAudioPlayer * tester;
// ...
#end
2.
we don't put anything like this into the AppDelegate.m file, the app delegate class basically handles the app-related events globally like launch, terminate, etc... briefly, the global and major events of the app's life cycle in runtime.
you can read more about its purpose in the official docs.
3.
you may use the –applicationDidFinishLaunching: method deliberately for initing your app, but I feel necessary to mention you may want to put everything inside the method –application:didFinishLaunchingWithOptions: instead.
you can read more about the initial procedure as well in the same documentation.
TL;DR
the answer to your original concern: NO, a class can be inited and instantiated in any other instance of any type of classes in general, but you need to worry about keeping the object alive as long as you want to use it.

Cocoa Obj-C WebView how to display HTML string

I have a string of HTML that was parsed by libxml2.dylib that looks like:
Hello,<br />\n<br />\nThis is almost HTML.<br />\n<br />\n
I've unsuccessfully tried to display certain strings parsed from the XML in a WebView; I'm hoping there's a simple way to do it such as how an HTML page is displayed in my Cocoa application:
HTMLView.h
#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>
#interface htmlView : NSObject {
IBOutlet WebView * webview;
}
-(IBAction) showHTML:(id) sender;
#end
HTMLView.m
#import "HTMLView.h"
#implementation htmlView
-(IBAction) showHTML:(id) sender
{
[[webview mainFrame] loadRequest:
[NSURLRequest requestWithURL:
[NSURL URLWithString:#"http://www.example.com"]]];
NSString * string = #"<br>test</br>";
[self loadHTMLString:string baseURL:(NSURL *)baseURL];
}
-(void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL {
}
#end
try this... [[aWebView mainFrame] loadHTMLString:aString baseURL:nil];
I've unsuccessfully tried to display certain strings parsed from the XML in a WebView
How did you try to display the strings and what was the problem? I think
-(void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL
Might be what you need...!?
Edit:
You have to call the method on your webview!
What you did now is implementing your own loadHTMLString method in your viewController. Which would be fine if it did anything and did call loadHTMLString on the webview at some point.
[self.webView loadHTMLString....]
I think you have to familiarise yourself a bit more with objective-c.

How to access NSArray in another View

I have some problems with my XML parser, written in obj-c.
The parser itself is fine but I cant access the result Array in the DetailView.
I need the values of the array created by the Parser Class in my Master and DetailViewController.
In MasterViewController I do this:
MasterViewController.h
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
#import "TouchXML.h"
#import "Parser.h"
#class Parser;
#interface MasterViewController : UITableViewController{
Parser *theParser;
}
MasterViewController.m
- (void)viewDidLoad
{
theParser = [[Parser alloc] init];
[theParser startParser:xmlPath]; //Now the parser runs and parses all needed values into arrays
}
Then I push on click the DetailView and I want to access the values there too.
UniViewController.h
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
#import "Parser.h"
#interface UniViewController : UITableViewController{
Parser *theParser;
}
UniViewController.m
- (void)viewDidLoad
{
NSLog(#"Name: %#",[[theParser.listArray objectAtIndex: 0]valueForKey:#"name"]);
}
Here I want to access the the Array from the parser but I always get the value (null) ?
In the debugger I saw that theParser has 0x0000 which cant be right...
After I do theParser = [[Parser alloc] init]; here, it has an hex value but I still get the (null).
How can I access the Array values from the DetailView?
Would be really nice if someone could explain me the problem here.
Thanks.
Your detail view has to have a pointer to the array that your MasterView creates (via the parser), try this:
(void)viewDidLoad
{
aParser = [[Parser alloc] init];
[aParser startParser:xmlPath];
myUniView.theParser = aParser; //myUniView is just the detail view you're pushing
and then after this you can push your detail view.

Why is this dynamically generated HTML not being displayed in a UIWebView?

I have HTML I am generating in my app. I know it's valid because I ran it through W3Schools "edit and try it". It performs exactly as I designed it.
Here is the definition of the website in the header file:
#interface ReportViewController : UIViewController <UIWebViewDelegate> {
IBOutlet UIWebView *reportWebView;
}
#property (nonatomic, retain) UIWebView *reportWebView;
#property (nonatomic, retain) NSMutableString *html;
- (void) generateReport;
- (void) Alert: (NSString *) title andData: (NSString *) errorMsg;
#end
Here is where I finish creating the HTML and try to display it:
// now do the table
[html appendString:#"<div><table border class=\"boldtable\">"
"<tr BGCOLOR=\"ADDAFE\">"
"<th>STA </th><th>BS </th><th>HI </th><th>FS </th><th>ELEV </th><th>Desc</th>"
"</tr><p>"];
}
// finally, end the table, etc.
[html appendString:#"</table></div></body></html>"];
// UIWebView *reportWebView = [[UIWebView alloc] init];
//[reportWebView loadHTMLString: html baseURL:nil];
[self.reportWebView loadHTMLString:html baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
}
As you can see, I have tried several different variations of displaying the HTML string. What is the correct way to do this?
Iulius Caesar Jacques Cousteau gave the correct answer in a comment:
reportWebView is an IBOutlet, indicating that it's created in an xib. Is the outlet connected? Is the nib loaded? What's with the commented-out UIWebView *reportWebView = [[UIWebView alloc] init];? Is this your exact code? Are you creating a local variable reportWebView that's shadowing the ivar?
but never came back to set it, therefore I am answering it for him.