سؤال

Hi I am new in development of iOS. I make class named socialClass.h

#import <Social/Social.h>
#import <Accounts/Accounts.h>

- (void)facebookSharingStatusCheckCall:(NSString *)url image:(NSString *)img status:(NSString *)text viewCont:(UIViewController *)uiVew;

- (void)facebookForIosSix:(NSString *)url image:(NSString *)img status:(NSString *)text viewCont:(UIViewController *)uiVew;

and in socialClass.m

    - (void)facebookSharingStatusCheckCall:(NSString *)url image:(NSString *)img status:(NSString *)text viewCont:(UIViewController *)uiVew 
{
    if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
        NSLog(@"service available");

     [self facebookForIosSix:url image:img status:text viewCont:uiVew];

    } else {
        // facebook for iOS 5 function will be called here
    }
}


-(void)facebookForIosSix:(NSString *)url image:(NSString *)img status:(NSString *)text viewCont:(UIViewController *)uiVew
{


if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {

        SLComposeViewController *fbcontroller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];

    SLComposeViewControllerCompletionHandler myBlock = ^(SLComposeViewControllerResult result){
        if (result == SLComposeViewControllerResultCancelled) {

                NSLog(@"Post Cancelled");

            } else

            {
                NSLog(@"Post Done");
            }

            [fbcontroller dismissViewControllerAnimated:YES completion:Nil];
        };
        fbcontroller.completionHandler =myBlock;

        //Adding the Text to the facebook post value from iOS
        [fbcontroller setInitialText:text];

        //Adding the URL to the facebook post value from iOS

        [fbcontroller addURL:[NSURL URLWithString:url]];

        //Adding the Image to the facebook post value from iOS

        [fbcontroller addImage:[UIImage imageNamed:img]];
        [uiVew presentViewController:fbcontroller animated:YES completion:nil];


    }
    else{
        UIAlertView *alertView = [[UIAlertView alloc]
                                  initWithTitle:@"Sorry"
                                  message:@"You can't Post a Status right now, make sure                                  your device has an internet connection and you have                                  at least one Twitter account setup"
                                  delegate:nil
                                  cancelButtonTitle:@"OK"
                                  otherButtonTitles:nil];
        [alertView show];

        NSLog(@"UnAvailable");
    }


}

Now I call in mainViewController.h

#import "socialClass.h"

and in mainViewController.m

- (IBAction)facebookShareBtn:(id)sender {

    [[socialClass facebookSharingStatusCheckCall:[[DataPassing sharedManager] pageURL ]
        image:[[DataPassing sharedManager]
        pageIMGurl]
        status:[[DataPassing sharedManager] pageTEXT]]
        viewCont:(UIViewController*)self];
}

When I use this below code with self it gives error. Definitely because there is no self view

[self presentViewController:fbcontroller animated:YES completion:nil]; so i put uiView in function and try to pass self from mainViewController. again error in mainview

How can I use this facebook from other calls function in mainviewcontroller. thx

[EDIT]

i declare socialClass.h in mainViewController and call the function in button [socialClass facebookForIosSix:...]

enter image description here

This error is comming... after updating to (UIViewController*)self

هل كانت مفيدة؟

المحلول

OK, a couple of things need to change in you function definition.

First you need a - at the front not a +. The plus denotes a class method whereas the - is for an instance method. You are using this like an instance method.

Second you need to include MainViewController.h in your SocialClass.h file. Then name the function like...

- (void)facebookForIosSix:(NSString *)url image:(NSString *)img status:(NSString *)text viewCont:(MainViewController *)mainViewController;

Then in mainViewController you can run...

[socialViewController facebookSharingStatusCheckCall:@"url"  image:@"hello.png" status:@"Posting from App" viewCont:self];

this should work fine.

EDIT

If you want to run the function fro multiple places then don't import MainViewController.h and define the function like...

- (void)facebookForIosSix:(NSString *)url image:(NSString *)img status:(NSString *)text viewCont:(UIViewController *)viewController;

then run it like...

[socialViewController facebookSharingStatusCheckCall:@"url"  image:@"hello.png" status:@"Posting from App" viewCont:(UIViewController*)self];

TBH, I don't think you'll need to cast it at all but just to be safe you can.

EDIT

You haven't balanced your brackets...

- (IBAction)facebookShareBtn:(id)sender {

    [socialClass facebookSharingStatusCheckCall:[[DataPassing sharedManager] pageURL ]
        image:[[DataPassing sharedManager] pageIMGurl]
        status:[[DataPassing sharedManager] pageTEXT]
        viewCont:(UIViewController*)self];
}

This will work.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top