FacebookLikeView crashing all the time iOS - exception

I've been using the FacebookLikeView classes in a lot of apps to enable Facebook profile liking within apps. Recently the code has started crashing in every app I've put it in. The Facebook app is setup correctly and everything. This is the line it is giving an EXC_BAD_ACCESS on:
else if ([fbEvent isEqualToString:#"xfbml.render"] && [_delegate respondsToSelector:#selector(facebookLikeViewDidRender:)])
[_delegate facebookLikeViewDidRender:self];
And it's being implemented like so:
_facebook = [[Facebook alloc] initWithAppId:fbAppID andDelegate:self];
self.facebookLikeView = [[FacebookLikeView alloc]init];
[self.facebookLikeView setFrame:CGRectMake(117, 140, 82, 19)];
self.facebookLikeView.delegate = self;
self.facebookLikeView.href = [NSURL URLWithString:user];
self.facebookLikeView.layout = #"button_count";
self.facebookLikeView.showFaces = NO;
[self.facebookLikeView load];

You should set your facebookLikeView to nil in your dealloc method or viewdidunload method

Related

xcode interaction with a html website

I would like write a small program, which send a string to a Website und get the answer string back.
It's about the following site: http://www.pandorabots.com/pandora/talk?botid=ec8b3be33e34bf73
Programm send string to the text field on the website "Frage: ..."
Programm get back the string after "Brother B!:"
It is a cocoa application for OS X.
-(BOOL)getStringFromWebView:(NSString*)frage
{
//----------------Put the question (frage) into the text field on the web page
if (frage.length != 0) {
//create js strings
NSString *loadUsernameJS = [NSString stringWithFormat:#"var inputFields = document.querySelectorAll(\"input[type='text']\"); \
for (var i = inputFields.length >>> 0; i--;) { inputFields[i].value = '%#';}", frage];
//autofill the form
[self.webView stringByEvaluatingJavaScriptFromString: loadUsernameJS];
}
//-------------Press 'return' to send the message
//PROBLEM: Webview is not active. It do not work...
CGEventRef push = CGEventCreateKeyboardEvent(NULL, 0x24, true);
CGEventRef release = CGEventCreateKeyboardEvent(NULL, 0x24, false);
CGEventPost(kCGHIDEventTap, push);
CGEventPost(kCGHIDEventTap, release);
//I tried this to focus the web view:
//[self.webView stringByEvaluatingJavaScriptFromString:#"document.getElementById(\"input\").setFocus();"];
//----------get response from web page (work!):
WebFrame *frame = [self.webView mainFrame];
WebDataSource *source = [frame dataSource];
NSData *data = [source data];
NSString *str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
// NSLog(#"%#",str);
NSArray *woerter1 = [str componentsSeparatedByString: #"<I>Brother B!</I>: "];
NSArray *woerter2 = [woerter1[1] componentsSeparatedByString: #"<form method=\"POST\">"];
self.out3.stringValue = woerter2[0];
return true;
}
Actually i don't really know how to do this... Please help me!
you can load your web page in WebView/IUWebView object and interact with it by JavaScript bridge library. You can use this library link to send to JavaScript a message from native application.
Next you can use JavaScript at html-client-side to manage this message and execute the form.
Cheers

Share screenshot to facebook/twitter using cocos2d-x on IOS

I am porting my game from cocos2d (obj-c) to cocos2d-x 2.x (c++) but i am really struggling with getting the social features to work. Is it possible in any way to use the code below which works flawless in native objective-c and cocos2d.
Take screenshot
- (UIImage*) takeScreenShot
{
[CCDirector sharedDirector].nextDeltaTimeZero = YES;
CGSize winSize = [CCDirector sharedDirector].winSize;
CCLayerColor* blankLayer = [CCLayerColor layerWithColor:ccc4(255, 255, 255, 0) width:winSize.width height:winSize.height];
blankLayer.position = ccp(winSize.width/2, winSize.height/2);
CCRenderTexture* rtx = [CCRenderTexture renderTextureWithWidth:winSize.width height:winSize.height];
[rtx begin];
[blankLayer visit];
//[[[CCDirector sharedDirector] runningScene] visit];
[layer_main visit];
[rtx end];
return [rtx getUIImage];
}
Post on facebook
- (void)share_score_facebook: (id) sender{
if (SYSTEM_VERSION_LESS_THAN(#"6.0")){
[AppVariables showAlert:#"Outdated iOS" message:#"You must have atleast iOS 6.0 to use the social features.\nPlease update to a newer iOS."];
return;
}
[[CCDirector sharedDirector] pause];
[[CCDirector sharedDirector] stopAnimation];
AppController *app = (AppController*) [[UIApplication sharedApplication] delegate];
UIImage * screenshot = [self takeScreenShot];
SLComposeViewController *faceBookPost = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[faceBookPost setInitialText:[NSString stringWithFormat:#"Game on! Just scored %d on Game. Can you Beat it?",total_score]];
[faceBookPost addImage:screenshot];
[faceBookPost addURL:[NSURL URLWithString:#"URL_HERE"]];
[[app navController] presentViewController:faceBookPost animated:YES completion:nil];
faceBookPost.completionHandler = ^(SLComposeViewControllerResult result)
{
[[CCDirector sharedDirector] resume];
[[CCDirector sharedDirector] startAnimation];
[[app navController] dismissViewControllerAnimated:YES completion:nil];
};
}
You can compile any Objective-C code with cocos2d-x, but you need modify your code to adapt for cocos2d-x API. You want to take a screenshot and post it to Facebook, right? How about using the following instead of your Objective-C code?
Cocos2d-x v3.2-alpha0 Released! - Added utils::captureScreen to take screeshot
v3.2-alpha0 ccUtils.cpp seems it is able to work on earlier version of cocos2d-x
Screw - aims to bring native Facebook SDK functionalities to cocos2d-x

Reduce html file loading time in phonegap application ios?

iam working on a simple phonegap application in that it takes around 3 minute to load the file(for showing home page ) till that time it showing the splash image(launch screen) is there any way to make it fast???
Total "WWW" folder file size is 5.3MB
my app delegate application didFinishLaunchingWithOptions() show below
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
CGRect screenBounds = [[UIScreen mainScreen] bounds];
#if __has_feature(objc_arc)
self.window = [[UIWindow alloc] initWithFrame:screenBounds];
#else
self.window = [[[UIWindow alloc] initWithFrame:screenBounds] autorelease];
#endif
self.window.autoresizesSubviews = YES;
#if __has_feature(objc_arc)
self.viewController = [[MainViewController alloc] init];
#else
self.viewController = [[[MainViewController alloc] init] autorelease];
#endif
self.viewController.useSplashScreen = YES;
// Set your app's start page by setting the <content src='foo.html' /> tag in config.xml.
// If necessary, uncomment the line below to override it.
// self.viewController.startPage = #"index.html";
// NOTE: To customize the view's frame size (which defaults to full screen), override
// [self.viewController viewWillAppear:] in your view controller.
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
I've known other people with similar issues (but not 3 minute delays!), I'm not sure there is much you can do, I think we need to wait for the PhoneGap people to develop a system that makes it more efficient in loading.

How to get directions in iOS 6 on new Apple Maps?

I've an app builded for iOS < 6, and before it all the features work perfectly. Now I've used a switch that recognizes the iOS installed on a device and then my app opens the correct maps to get directions from the local position to another (some pins on the map). The problem is that before iOS 6, the app opens the native maps and show the route to the user. Now, If the device uses iOS 6, the app opens the Apple Maps but shows a pop-up with something like "Impossible to get direction between this 2 positions". Why? Can someone helps me to solve this problem?
Here how I pass the coordinate to maps from a disclosure button:
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
[self.navigationController pushViewController:[[UIViewController alloc] init] animated:YES];
if (SYSTEM_VERSION_LESS_THAN(#"6.0")) {
NSString* addr = [NSString stringWithFormat:#"http://maps.google.com/maps?daddr=%1.6f,%1.6f&saddr=Posizione attuale", view.annotation.coordinate.latitude,view.annotation.coordinate.longitude];
NSURL* url = [[NSURL alloc] initWithString:[addr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:url];
}
else {
NSString* addr = [NSString stringWithFormat:#"http://maps.apple.com/maps?daddr=%1.6f,%1.6f&saddr=Posizione attuale", view.annotation.coordinate.latitude,view.annotation.coordinate.longitude];
NSURL* url = [[NSURL alloc] initWithString:[addr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:url];
}
Whilst you can use the URL method for maps (http://maps.apple.com/...), I've found it not to be great when presenting specific locations. Your best bet would be to use MapKit objects on iOS 6 and above.
Example:
CLLocationCoordinate2D location;
location.latitude = ...;
location.longtitude = ...;
MKPlacemark* placemark = [[MKPlacemark alloc] initWithCoordinate:location addressDictionary:nil];
MKMapItem* item = [[MKMapItem alloc] initWithPlacemark:placemark];
//... Any Item customizations
NSDictionary* mapLaunchOptions = ...;
[item openInMapsWithLaunchOptions:mapLaunchOptions];
This will open Apple Maps directly, with the item you have provided.
In order to deal with directions, use the MKLaunchOptionsDirectionsModeKey launch option, and provide the start and end map items via:
[MKMapItem openMapsWithItems:mapItems launchOptions:launchOptions];
Just to clarify the other answer, you have two options when giving directions: driving or walking. Here are the ways to do each one:
MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:CLLocationCoordinate2DMake(coordinate.latitude,coordinate.longitude) addressDictionary:nil];
MKMapItem *destination = [[MKMapItem alloc] initWithPlacemark:placemark];
//This is for driving
[MKMapItem openMapsWithItems:#[destination] launchOptions:#{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving}];
//This is for walking
[MKMapItem openMapsWithItems:#[destination] launchOptions:#{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeWalking}];
More info from Apple's docs over here.

Injecting local files into UIWebView without breaking links

I am working on an iOS app that needs to display webpages from a server inside a UIWebView while injecting relevant local png and css files as needed in order to speed up load time. Here is the code I am using to try to do this:
NSData *myFileData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://www.example.com/index.html"]]];
NSString* myFileHtml = [[NSString alloc] initWithData:myFileData encoding:NSASCIIStringEncoding];
[myWebView loadHTMLString:myFileHtml baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
My problem is that some of the webpages have buttons in them that link to other webpages on the server, and because the UIWebView is only loading a string, the buttons when tapped don't cause the UIWebView to load the new webpage URL like it would if I had used the loadRequest method.
My question is how can I get the the UIWebView to behave like it is loading a request while still injecting local files from the baseurl?
Thanks
The relative links in the button cannot work, because the linked pages are on a remote server and not on the device's file system. However you can use a UIWebViewDelegate method to make it work:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
NSString *localRootPath = [NSString stringWithFormat:#"file://%#", [[NSBundle mainBundle] bundlePath]];
NSString *remoteRootPath = #"http://yourdomain.com";
NSString *remotePath = [[request.URL absoluteString] stringByReplacingOccurrencesOfString:localRootPath withString:remoteRootPath];
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:remotePath]]];
// or you can use your own loading mechanism here
return NO;
}
return YES;
}
This method intercepts all requests from your WebView. If the request was triggered by a user tap / click the URL gets modified from a relative URL to a absolute URL so it can be loaded from the server. Don't forget to set the delegate on the WebView or this method will not be called.
NSURLPRotocol is a handler for NSURLConnection and will give you the opportunity to intercept the calls to the server and substitute your own content.
1) Derive a class from NSURlProtocol
2) Call NSURLProtocol registerClass: in your application:didFinishLaunchingWithOption
3) Read the documentation on implement these methods as necessary:
initWithRequest:cachedResponse:client:,
startLoading,
URLProtocol:didReceiveResponse:cacheStoragePolicy:
URLProtocolDidFinishLoading: