swift spritekit Facebook share button - ios

Hey there my app is almost ready for release but i want to add a Facebook share button. The thing is i have no idea how the communication between the scene and the viewcontroler works. i did my research but only found code in obj-c like this one
- (void)lkFaceBookShare {
NSString *serviceType = SLServiceTypeFacebook;
if (![SLComposeViewController isAvailableForServiceType:serviceType])
{
[self showUnavailableAlertForServiceType:serviceType];
}
else
{
SLComposeViewController *composeViewController = [SLComposeViewController composeViewControllerForServiceType:serviceType];
UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
CGRect rect = [keyWindow bounds];
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, 0.5f);
[self.view drawViewHierarchyInRect:rect afterScreenUpdates:YES];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[composeViewController addImage:viewImage];
NSString *initalTextString = [NSString stringWithFormat:#"Let's join together in the form of underground catch word go along with me!! Link: https://itunes.apple.com/us/app/uoi-hinh-bat-chu-gioi-duoi/id907330926?ls=1&mt=8"];
[composeViewController setInitialText:initalTextString];
UIViewController *vc = self.view.window.rootViewController;
[vc presentViewController:composeViewController animated:YES completion:nil];
}
}
- (void)showUnavailableAlertForServiceType:(NSString *)serviceType
{
NSString *serviceName = #"";
if (serviceType == SLServiceTypeFacebook)
{
serviceName = #"Facebook";
}
else if (serviceType == SLServiceTypeSinaWeibo)
{
serviceName = #"Sina Weibo";
}
else if (serviceType == SLServiceTypeTwitter)
{
serviceName = #"Twitter";
}
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:#"Account"
message:[NSString stringWithFormat:#"Please go to the device settings and add a %# account in order to share through that service", serviceName]
delegate:nil
cancelButtonTitle:#"Dismiss"
otherButtonTitles:nil];
[alertView show];
}
my experience and knowledge is too low to port this too swift so i need some help with this D:
Thanks

This is some code I did for twitter a while ago which still works in swift. I show you how to convert it to Facebook below. Put this your viewController:
func showTweetSheet() {
let tweetSheet = SLComposeViewController(forServiceType: SLServiceTypeTwitter)
tweetSheet.completionHandler = {
result in
switch result {
case SLComposeViewControllerResult.Cancelled:
//Add code to deal with it being cancelled
break
case SLComposeViewControllerResult.Done:
//Add code here to deal with it being completed
//Remember that dimissing the view is done for you, and sending the tweet to social media is automatic too. You could use this to give in game rewards?
break
}
}
tweetSheet.setInitialText("Test Twitter") //The default text in the tweet
tweetSheet.addImage(UIImage(named: "TestImage.png")) //Add an image if you like?
tweetSheet.addURL(NSURL(string: "http://twitter.com")) //A url which takes you into safari if tapped on
self.presentViewController(tweetSheet, animated: false, completion: {
//Optional completion statement
})
}
To convert it to Facebook, simply swap SLServiceTypeTwitter to SLServiceTypeFacebook and rename the variables for readability. If you want to call this method from the SKScene, you have to somehow alert the viewController that you want it to call a method.
My preferred way is to use NSNotificationCenter so that I post an alert from the scene and it is received by the viewController so that it fires a method. This is also incredibly easy to setup. In the scene, you need to put this line of code wherever you want to call the Facebook popup:
NSNotificationCenter.defaultCenter().postNotificationName("WhateverYouWantToCallTheNotification", object: nil)
This sends out a notification with a name. Now, in the viewController you need to subscribe to this alert by putting the following code in either viewDidLoad, viewDidAppear or something similar.
NSNotificationCenter.defaultCenter().addObserver(self, selector: "ThisIsTheMethodName", name: "WhateverYouCalledTheAlertInTheOtherLineOfCode", object: nil)
Now the scene will communicate with the viewController and you will be able to show the Facebook sheet. Remember to replace my strings with ones relative to your project. Hope this helps - sorry it was such a long answer!

func lkFaceBookShare() {
var serviceType: String = SLServiceTypeFacebook
if !SLComposeViewController.isAvailableForServiceType(serviceType) {
self.showUnavailableAlertForServiceType(serviceType)
}
else {
var composeViewController: SLComposeViewController = SLComposeViewController.composeViewControllerForServiceType(serviceType)
var keyWindow: UIWindow = UIApplication.sharedApplication().keyWindow
var rect: CGRect = keyWindow.bounds
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, false, 0.5)
self.view!.drawViewHierarchyInRect(rect, afterScreenUpdates: true)
var viewImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
composeViewController.addImage(viewImage)
var initalTextString: String = String(format: "Let's join together in the form of underground catch word go along with me!! Link: https://itunes.apple.com/us/app/uoi-hinh-bat-chu-gioi-duoi/id907330926?ls=1&mt=8")
composeViewController.initialText = initalTextString
var vc: UIViewController = self.view.window.rootViewController
vc.presentViewController(composeViewController, animated: true, completion: { _ in })
}
}
func showUnavailableAlertForServiceType(serviceType: String) {
var serviceName: String = ""
if serviceType == SLServiceTypeFacebook {
serviceName = "Facebook"
}
else if serviceType == SLServiceTypeSinaWeibo {
serviceName = "Sina Weibo"
}
else if serviceType == SLServiceTypeTwitter {
serviceName = "Twitter"
}
var alertView: UIAlertView = UIAlertView(title: "Account", message: "Please go to the device settings and add a \(serviceName) account in order to share through that service", delegate: nil, cancelButtonTitle: "Dismiss", otherButtonTitles: "")
alertView.show()
}
Swift Conversion of Obj-C answer posted by a very helpful user...... original post

Related

IMGLYMainEditorViewController completion block issue

I successfully implemented imglyKit in my Objective-C code, this library is made in swift language. Now i am opening a IMGLYMainEditorViewController from my viewcontroller.
My problem is that when the image is edited and when i press the done button i did not get the edited image. I checked the code and show that they set the completion block when they open IMGLYMainEditorViewController.
This is the code which is written in the library.
let editorViewController = IMGLYMainEditorViewController()
editorViewController.highResolutionImage = image
if let cameraController = cameraController {
editorViewController.initialFilterType = cameraController.effectFilter.filterType
editorViewController.initialFilterIntensity = cameraController.effectFilter.inputIntensity
}
editorViewController.completionBlock = editorCompletionBlock
private func editorCompletionBlock(result: IMGLYEditorResult, image: UIImage?) {
if let image = image where result == .Done {
UIImageWriteToSavedPhotosAlbum(image, self, "image:didFinishSavingWithError:contextInfo:", nil)
}
dismissViewControllerAnimated(true, completion: nil)
}
I wrote this code in my controller.
IMGLYMainEditorViewController *temp view = [[IMGLYMainEditorViewController alloc]init];
view.highResolutionImage = self.image_view.image;
[self.navigationController pushViewController:view animated:YES];
I want to set a block method here when i open a IMGLYMainEditorViewControlle so that i am able to get that edited image.
I did lots of try but not able to do that because i did not have a much knowledge about the block and how to deal with that. so please help me because i stuck here.
Remove the space between "temp view" instance name,
This is how you provide a completion block to an object:
IMGLYMainEditorViewController *tempView = [[IMGLYMainEditorViewController alloc] init];
tempView.completionBlock = ^(IMGLYEditorResult result, UIImage *image){
view.highResolutionImage = image;
};
[self.navigationController pushViewController:view animated:YES];

APNS to open a certain part of an application

I've just implemented a commenting feature in my app. Ideally when someone leaves a comment, I'd like all notified people be able to swipe the push notification and open the app on that post.
I assume you want to open the concerned page directly. There are many ways to go about this, and it depends on how your app is laid out.
If you want to open an inner page upon app launch, you can programmatically trigger the segues that the user would otherwise need to make manually. (this ensures the back/home buttons work as opposed to loading the desired page directly).
Here's an excerpt from one of my own code, your use case may not be the same, but this is all i can do unless you give us more details.
- (BOOL) navigateToRespectiveSectionforPushNot:(NSDictionary*)pushNot
{
id rootVC = self.window.rootViewController;
NSLog(#"ROOT CLASS : %#", [rootVC class]);
if ([rootVC isKindOfClass:[SWRevealViewController class]])
{
NSLog(#"Root Class looking good... mission Navigate!!");
SWRevealViewController *homeVC = (SWRevealViewController*) rootVC;
NSString *category = [[pushNot objectForKey:pushPayloadKeyaps] objectForKey:pushPayloadKeyCategory];
NSString *subCat = [[pushNot objectForKey:pushPayloadKeyaps] objectForKey:pushPayloadKeySubCategory];
NSLog(#"category : %# , subcat : %#",category,subCat);
//The code for the page to which i'm supposed to navigate to is contained in the push notification payload
if ([category isEqualToString:pushCategoryItemChat])
{
[homeVC.rearViewController performSegueWithIdentifier:#"chatPush" sender:nil];
UINavigationController *nc = (UINavigationController*)homeVC.frontViewController;
NSLog(#"FrontView Class : %#",[nc.viewControllers[0] class]);
UITableViewController *tvc = (UITableViewController*)nc.viewControllers[0];
NSDictionary *send = #{chatPushTargetUserId:subCat,chatPushTargetUserName:#"",chatPushTargetUserImage:#""};
[tvc performSegueWithIdentifier:#"seguePushDemoVC" sender:send];
return YES;
}
//communityPush historyPush
else if ([category isEqualToString:pushCategoryItemCommunity])
{
if ([subCat isEqualToString:pushSubCatItemNewRequest])
{
[homeVC.rearViewController performSegueWithIdentifier:#"communityPush" sender:nil];
return YES;
}
else if ([subCat isEqualToString:pushSubCatItemAccepted])
{
[homeVC.rearViewController performSegueWithIdentifier:#"communityPush" sender:nil];
return YES;
}
}
else if ([category isEqualToString:pushCategoryItemHistory])
{
[homeVC.rearViewController performSegueWithIdentifier:#"historyPush" sender:nil];
return YES;
}
}
else
{
UIAlertView *whoa = [[UIAlertView alloc] initWithTitle:#"WHOA!!" message:#" That wasn't supposed to happen. You are not even logged in. Call 911..." delegate:nil cancelButtonTitle:#"mmKay.." otherButtonTitles:nil, nil];
[whoa show];
}
return NO;
}
I hope the code is self explanatory. cheers

open appstore as model view opens blank page ios

I am tring to open appstore page as model view inside application using following code
[NSDictionary dictionaryWithObject:#"APPID" forKey:SKStoreProductParameterITunesItemIdentifier];
SKStoreProductViewController *productViewController = [[SKStoreProductViewController alloc] init];
[self presentViewController:productViewController animated:YES completion:nil];
but when appstore is open inside application, it is opening as blank page.
Please refer screenshoot attached
I dont understand why appstore page of my app is not opening. I am passing APPID in above code.
Is there any other way to rate application without closing app ?
basically, something like this could help on you, after you linked the StoreKit.framework to your project. please note, it may not be working on simulator; on real device it works well.
.h
#interface UIYourViewController : UIViewController <SKStoreProductViewControllerDelegate> { }
.m
- (void)myOwnCustomMethod {
SKStoreProductViewController *_controller = [[SKStoreProductViewController alloc] init];
[_controller setDelegate:self];
[_controller loadProductWithParameters:[NSDictionary dictionaryWithObjectsAndKeys:#"364709193", SKStoreProductParameterITunesItemIdentifier, nil] completionBlock:^(BOOL result, NSError *error) {
if (result) {
[self.navigationController presentViewController:_controller animated:TRUE completion:nil];
} else {
// you can handle the error here, if you'd like to.
}
}];
}
#pragma mark - <SKStoreProductViewControllerDelegate>
- (void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController {
[self dismissViewControllerAnimated:TRUE completion:nil];
}
In Swift 3,
import StoreKit
class DetailViewController: UIViewController {
#IBAction func onEditButton(_ sender: UIBarButtonItem) {
let vc = SKStoreProductViewController()
vc.delegate = self
present(vc, animated: true, completion: nil)
vc.loadProduct(withParameters: [SKStoreProductParameterITunesItemIdentifier: 351091731]) { (success, error) in
if !success {
print("\(error)")
}
}
}
}
extension DetailViewController: SKStoreProductViewControllerDelegate {
func productViewControllerDidFinish(_ viewController: SKStoreProductViewController) {
viewController.dismiss(animated: true, completion: nil)
}
}
Make sure SKStoreProductParameterITunesItemIdentifier's value is Number, as stated in its head file, though String value is currently OK.

Is it possible to give a phone Call action to a UILabel?

I am fetching a String from Parse.com into a UILabel programmatically in my app.
The String is a phone number. So my question is, is it possible to give an action to the UILabel on click to make a Call.. or do I have to fetch the data from a Button, or better yet, is this possible?
If it is possible, how would should I go about? Does anyone have an example or tutorial I could follow?
Any help would be appreciated.
Thanks!
You question is in two part :
Perform any kind on action when a UILabel is taped
Perform a phone call action (action used in the first part)
First, to perform an action when a label is taped, you have to add a tap gesture recognizer to this label :
phoneNumberLabel.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGesture =
[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(phoneNumberLabelTap)];
[phoneNumberLabel addGestureRecognizer:tapGesture];
Then you have to implement your phoneNumberLabelTap method :
-(void)phoneNumberLabelTap
{
NSURL *phoneUrl = [NSURL URLWithString:[NSString stringWithFormat:#"telprompt:%#",phoneNumberLabel.text]];
if ([[UIApplication sharedApplication] canOpenURL:phoneUrl]) {
[[UIApplication sharedApplication] openURL:phoneUrl];
} else {
UIAlertView * calert = [[UIAlertView alloc]initWithTitle:#"Alert" message:#"Call facility is not available!!!" delegate:nil cancelButtonTitle:#"ok" otherButtonTitles:nil, nil];
[calert show];
}
}
In swift 3.0
mobileLabel.isUserInteractionEnabled = true;
let tap = UITapGestureRecognizer(target: self, action:#selector(self.phoneNumberLabelTap))
tap.delegate = self
mobileLabel.addGestureRecognizer(tap)
func phoneNumberLabelTap()
{
let phoneUrl = URL(string: "telprompt:\(mobileLabel.text ?? "")")!
if(UIApplication.shared.canOpenURL(phoneUrl)) {
UIApplication.shared.openURL(phoneUrl)
}
else {
Constants.ShowAlertView(title: "Alert", message: "Cannot place call", viewController: self, isFailed: false)
}
}

How to cancel UIActivityItemProvider and don't show activity?

I'm using UIActivityItemProvider subclass to provide custom data. But sometimes getting data fails and I don't want to present activity (e.g. message composer). Tried [self cancel] and return nil; in item method, but message composer still shows (with empty message).
If you dismiss the UIActivityViewController before returning from -(id)item it will not present the users chosen activity.
To do this you first need to grab the activityViewController in activityViewControllerPlaceholderItem. In -(id)item run code in a dispatch_async to update progress and dismiss on complete / error which I'm doing using a promise lib.
In your subclass of UIActivityItemProvider do something similar to the example below.
-(id) activityViewControllerPlaceholderItem:(UIActivityViewController *)activityViewController
{ self.avc = activityViewController;
return NSURL;
}
-(id)item
{ __block BOOL fileProcDone = NO;
dispatch_async(dispatch_get_main_queue(), ^
{ self.pvc = [[ProgressAlertVC alloc] init];
[self.vc presentViewController:self.pvc animated:YES completion:nil];
[[[[self promiseMakeFile]
progressed:^(float progress)
{ self.pvc.progress = progress;
}]
fulfilled:^(id result)
{ [self.pvc dismissViewControllerAnimated:YES completion:^
{ fileProcDone = YES;
}];
}]
failed:^(NSError *error)
{ [self.pvc dismissViewControllerAnimated:YES completion:^
{ [self.vc dismissViewControllerAnimated:YES completion:^
{ fileProcDone = YES;
}];
}];
}];
});
while (!fileProcDone)
{ [NSThread sleepForTimeInterval:0.1];
};
return NSURL;
}
This will result in a console log message from activity extensions but as long as they deal correctly with errors things should be fine. If you return nil from -(id)activityViewController: itemForActivityType: you don't get console errors but will get the users chosen activity even if you dismiss the UIActivityViewController at this point.
You simply need to call the cancel method of UIActivityItemProvider. Since UIActivityItemProvider is an NSOperation, calling cancel will mark the operation cancelled.
At that point, you have a few options to actually stop the long running task, depending on the structure of your task. You could override the cancel method and do your cancellation there, just be sure to call [super cancel] as well. The second option is the check the value of isCancelled within the item method.
An example item provider
import UIKit
import Dispatch
class ItemProvider: UIActivityItemProvider {
override public var item: Any {
let semaphore = DispatchSemaphore(value: 0)
let message = "This will stop the entire share flow until you press OK. It represents a long running task."
let alert = UIAlertController.init(title: "Hello", message: message, preferredStyle: .alert)
let action = UIAlertAction.init(title: "OK", style: .default, handler:
{ action in
semaphore.signal()
})
let cancel = UIAlertAction.init(title: "CANCEL", style: .destructive, handler:
{ [weak self] action in
self?.cancel()
semaphore.signal()
})
alert.addAction(action)
alert.addAction(cancel)
//Truly, some hacking to for the purpose of demonstrating the solution
DispatchQueue.main.async {
UIApplication.shared.delegate?.window??.rootViewController?.presentedViewController!.present(alert, animated: true, completion: nil)
}
// We can block here, because our long running task is in another queue
semaphore.wait()
// Once the item is properly cancelled, it doesn't really matter what you return
return NSURL.init(string: "blah") as Any
}
}
In the view controller, start a share activity like this.
let provider = ItemProvider.init(placeholderItem: "SomeString")
let vc = UIActivityViewController.init(activityItems: [provider], applicationActivities: nil)
self.present(vc, animated: true, completion: nil)

Resources